From f871744900f627f97e6215da5f7f63cea597d81b Mon Sep 17 00:00:00 2001 From: "Tim.Mitchell" Date: Tue, 5 Mar 2024 15:43:38 +1300 Subject: [PATCH] Extend and simplify bug fix. bump version --- pure_interface/__init__.py | 2 +- pure_interface/interface.py | 12 ++---------- tests/mypy_source.py | 2 +- tests/test_dataclass_support.py | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pure_interface/__init__.py b/pure_interface/__init__.py index ba53f4a..9a21b77 100644 --- a/pure_interface/__init__.py +++ b/pure_interface/__init__.py @@ -7,4 +7,4 @@ from .adaption import adapts, register_adapter, AdapterTracker, adapt_args from .delegation import Delegate -__version__ = '8.0.0' +__version__ = '8.0.1' diff --git a/pure_interface/interface.py b/pure_interface/interface.py index 4cce0cd..82bf6a7 100644 --- a/pure_interface/interface.py +++ b/pure_interface/interface.py @@ -402,20 +402,12 @@ def _ensure_everything_is_abstract(attributes): return namespace, functions, interface_method_signatures, interface_attribute_names -def _readonly_attribute(name, namespace): - # if the attribute is a property with a getter but no setter then it is read-only - prop = namespace.get(name) - if isinstance(prop, property): - if prop.fset is None: - return True - return False - - def _ensure_annotations(names, namespace): # annotations need to be kept in order, add base-class names first + # we only want dataclass annotations for attributes that don't already exist annotations = {} for name in names: - if name not in annotations and not _readonly_attribute(name, namespace): + if name not in annotations and name not in namespace: annotations[name] = Any annotations.update(namespace.get('__annotations__', {})) namespace['__annotations__'] = annotations diff --git a/tests/mypy_source.py b/tests/mypy_source.py index 236e008..a8cb3d3 100644 --- a/tests/mypy_source.py +++ b/tests/mypy_source.py @@ -23,7 +23,7 @@ class MyInterface(BaseInterface, pure_interface.Interface): @abc.abstractmethod def weight(self, arg: int) -> str: - pass + """weight of the thing.""" @property def height(self) -> float: diff --git a/tests/test_dataclass_support.py b/tests/test_dataclass_support.py index 5c27309..f18c60e 100644 --- a/tests/test_dataclass_support.py +++ b/tests/test_dataclass_support.py @@ -67,5 +67,19 @@ def foo(self): return 'a={}, b={}, c={}'.format(self.a, self.b, self.c) f = RoFoo(a=1, c=3) + self.assertEqual({'a', 'c'}, set(RoFoo.__annotations__.keys())) self.assertEqual(1, f.a) self.assertEqual('str', f.b) + + def test_attr_present(self): + @dataclass + class AFoo(IFoo): + a = 10 + + def foo(self): + return 'a={}, b={}, c={}'.format(self.a, self.b, self.c) + + f = AFoo(b='str') + self.assertEqual({'b'}, set(AFoo.__annotations__.keys())) + self.assertEqual(10, f.a) + self.assertEqual('str', f.b)