Skip to content

Commit 20aa7ab

Browse files
author
Eddie Hebert
committed
ENH: Add property as a supported type.
Enables definitions such as: ``` class FooInterface(Interface): @Property def bar(self): pass class Foo(implements(FooInterface)): @Property def bar(self): return 'baz' ``` If the implementation is missing the `property` decorator, such as: ``` class FooInterface(Interface): @Property def bar(self): pass class Foo(implements(FooInterface)): def bar(self): return 'baz' ``` Then an error is raised showing that the method has the wrong type: ``` The following methods of FooInterface were implemented with incorrect types: - bar: 'function' is not a subtype of expected type 'property' ```
1 parent 31c453d commit 20aa7ab

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

interface/tests/test_interface.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,65 @@ class Impl failed to implement interface I:
386386
- foo: 'function' is not a subtype of expected type 'classmethod'"""
387387
)
388388
assert expected == str(e.value)
389+
390+
391+
def test_property():
392+
393+
class I(Interface):
394+
@property
395+
def foo(self): # pragma: nocover
396+
pass
397+
398+
class my_property(property):
399+
pass
400+
401+
class Impl(implements(I)):
402+
@my_property
403+
def foo(self): # pragma: nocover
404+
pass
405+
406+
with pytest.raises(IncompleteImplementation) as e:
407+
class Impl(implements(I)):
408+
def foo(self): # pragma: nocover
409+
pass
410+
411+
expected = dedent(
412+
"""
413+
class Impl failed to implement interface I:
414+
415+
The following methods of I were implemented with incorrect types:
416+
- foo: 'function' is not a subtype of expected type 'property'"""
417+
)
418+
assert expected == str(e.value)
419+
420+
with pytest.raises(IncompleteImplementation) as e:
421+
class Impl(implements(I)):
422+
@property
423+
def foo(self, a, b): # pragma: nocover
424+
pass
425+
426+
expected = dedent(
427+
"""
428+
class Impl failed to implement interface I:
429+
430+
The following methods of I were implemented with invalid signatures:
431+
- foo(self, a, b) != foo(self)"""
432+
)
433+
assert expected == str(e.value)
434+
435+
with pytest.raises(IncompleteImplementation) as e:
436+
class Impl(implements(I)):
437+
def foo(self, a, b): # pragma: nocover
438+
pass
439+
440+
expected = dedent(
441+
"""
442+
class Impl failed to implement interface I:
443+
444+
The following methods of I were implemented with incorrect types:
445+
- foo: 'function' is not a subtype of expected type 'property'
446+
447+
The following methods of I were implemented with invalid signatures:
448+
- foo(self, a, b) != foo(self)"""
449+
)
450+
assert expected == str(e.value)

interface/typed_signature.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def __init__(self, obj):
2020
self._type = type(obj)
2121
if isinstance(obj, (classmethod, staticmethod)):
2222
self._signature = signature(obj.__func__)
23+
elif isinstance(obj, property):
24+
self._signature = signature(obj.fget)
2325
else:
2426
self._signature = signature(obj)
2527

0 commit comments

Comments
 (0)