Skip to content

Commit

Permalink
dsl: fix python 3.11 dataclasses error and mypy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aszs committed Oct 25, 2023
1 parent 2c04994 commit 4938a2f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/configurators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 5 additions & 1 deletion tosca-package/tosca/_tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tosca-package/tosca/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4938a2f

Please sign in to comment.