Skip to content

Commit

Permalink
Remove hugr_config contextmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed May 1, 2024
1 parent c0aee6b commit 96080fa
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions hugr-py/src/hugr/serialization/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ def _json_custom_error_validator(

ExtensionId = str

global default_model_config
default_model_config = ConfigDict()
global current_model_config
current_model_config = default_model_config


class ConfiguredBaseModel(BaseModel):
Expand All @@ -54,20 +51,6 @@ def set_model_config(cls, config: ConfigDict):
cls.model_config = config


@contextmanager
def hugr_config(**kwargs):
global current_model_config
old_config = current_model_config
current_model_config = ConfigDict(default_model_config, **kwargs)
ConfiguredBaseModel.model_config = current_model_config
print(f"new model_config: {current_model_config}")
try:
yield None
finally:
current_model_config = old_config
ConfiguredBaseModel.model_config = old_config


class ExtensionSet(RootModel):
"""A set of extensions ids."""

Expand Down Expand Up @@ -363,21 +346,23 @@ class Signature(ConfiguredBaseModel):


# Now that all classes are defined, we need to update the ForwardRefs in all type
# annotations. We use some inspect magic to find all classes defined in this file.
# annotations. We use some inspect magic to find all classes defined in this file
# and call _model_rebuild()
classes = inspect.getmembers(
sys.modules[__name__],
lambda member: inspect.isclass(member) and member.__module__ == __name__,
)


def _model_rebuild(
classes: list[Tuple[Any, type]] = classes,
classes: list[Tuple[str, type]] = classes,
config: ConfigDict = ConfigDict(),
**kwargs,
):
new_config = ConfigDict(default_model_config, **config)
for _, c in classes:
if issubclass(c, ConfiguredBaseModel):
c.set_model_config(config)
c.set_model_config(new_config)
c.model_rebuild(**kwargs)


Expand Down

0 comments on commit 96080fa

Please sign in to comment.