Skip to content

Commit

Permalink
Extend and simplify bug fix.
Browse files Browse the repository at this point in the history
bump version
  • Loading branch information
tim-mitchell committed Mar 5, 2024
1 parent 3cbbd47 commit f871744
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pure_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
12 changes: 2 additions & 10 deletions pure_interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/mypy_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dataclass_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f871744

Please sign in to comment.