From 4938a2f5afbaf8e01d4b412735891a90291ff363 Mon Sep 17 00:00:00 2001 From: Adam Souzis Date: Tue, 24 Oct 2023 19:39:25 -0700 Subject: [PATCH] dsl: fix python 3.11 dataclasses error and mypy fixes --- docs/configurators.rst | 2 +- tosca-package/tosca/_tosca.py | 6 +++++- tosca-package/tosca/loader.py | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/configurators.rst b/docs/configurators.rst index 1d12d21c..f774e3f8 100644 --- a/docs/configurators.rst +++ b/docs/configurators.rst @@ -2,7 +2,7 @@ Configurators =============== -To use a configurator, set it as the ``implementation`` field of an `operation` +To use a configurator, set it in the ``implementation`` field of an :std:ref:`Operation` and set its inputs as documented below. Configurator names are case-sensitive; if a configurator name isn't found it is treated as an external command. diff --git a/tosca-package/tosca/_tosca.py b/tosca-package/tosca/_tosca.py index 13f609d1..9fab6272 100644 --- a/tosca-package/tosca/_tosca.py +++ b/tosca-package/tosca/_tosca.py @@ -919,6 +919,10 @@ def __repr__(self): def to_yaml(self, dict_cls=dict): return self.expr + # note: we need this to prevent dataclasses error on 3.11+: mutable default for field + def __hash__(self) -> int: + return hash(str(self.expr)) + def __eq__(self, __value: object) -> bool: if isinstance(__value, type(self.expr)): return self.expr == __value @@ -966,7 +970,7 @@ class FieldProjection(_Ref): def __init__(self, field: _Tosca_Field, parent: Optional["FieldProjection"] = None): # currently don't support projections that are requirements expr = field.as_ref_expr() - if parent: + if parent and parent.expr: # XXX map to tosca name but we can't do this now because it might be too early to resolve the attribute's type expr = parent.expr["eval"] + "::" + expr super().__init__(dict(eval=expr)) diff --git a/tosca-package/tosca/loader.py b/tosca-package/tosca/loader.py index 2c146f7e..fd6151b5 100644 --- a/tosca-package/tosca/loader.py +++ b/tosca-package/tosca/loader.py @@ -584,9 +584,9 @@ def restricted_exec( namespace["__package__"] = package policy = SafeToscaDslNodeTransformer if safe_mode else ToscaDslNodeTransformer result = compile_restricted_exec(python_src, policy=policy) - if PRINT_AST_SRC: + if PRINT_AST_SRC and sys.version_info.minor >= 9: c_ast = result.used_names[":top"] - print(ast.unparse(c_ast)) + print(ast.unparse(c_ast)) # type: ignore if result.errors: raise SyntaxError("\n".join(result.errors)) temp_module = None