Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement @delegated decorator #77
- Loading branch information
1 parent
c9fad05
commit 916fd1f
Showing
7 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
def delegated(cls): | ||
class Delegated: | ||
"""Create decorator class in true sense of OOP.""" | ||
return cls | ||
|
||
def __init__(self, interface): | ||
self.interface = interface | ||
|
||
def __call__(self, cls): | ||
"""Create decorator class.""" | ||
created_methods = {"__init__": cls.__dict__["__init__"]} | ||
for name in self.interface.__dict__: | ||
if name.startswith("_"): | ||
continue | ||
created_methods[name] = _create_method(name) | ||
return type(cls.__name__, (), created_methods) | ||
|
||
|
||
def _create_method(name): | ||
def _method(self, *args, **kwargs): | ||
return getattr(self.user, name)(*args, **kwargs) | ||
|
||
return _method | ||
|
||
|
||
delegated = Delegated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SmartUser: | ||
"""User model decorator.""" | ||
|
||
def __init__(self, user): | ||
self.user = user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
"""Tests related to the @delegated decorator.""" | ||
from datetime import datetime | ||
|
||
from generics import delegated | ||
from generics import private | ||
|
||
|
||
delegated(None) | ||
def test_define_instance_methods(e, w): | ||
"""Provide access to not redefined methods.""" | ||
user_class = private(e.User) | ||
smart_user_class = delegated(user_class)(w.SmartUser) | ||
user = user_class(last_login=datetime(1999, 12, 31)) | ||
smart_user = smart_user_class(user) | ||
assert not smart_user.is_active() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
"""Tests related to inheritance from interface.""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from generics import delegated | ||
from generics import private | ||
|
||
|
||
pytestmark = pytest.mark.parametrize("f", [private, delegated]) | ||
def test_private_allow_inheritance_from_interface(s): | ||
"""Allow inheritance from interface.""" | ||
user_class = private(s.User) | ||
user = user_class(last_login=datetime(1999, 12, 31)) | ||
assert not user.is_active() | ||
|
||
|
||
def test_allow_inheritance_from_interface(f, s): | ||
def test_delegated_allow_inheritance_from_interface(s): | ||
"""Allow inheritance from interface.""" | ||
user_class = f(s.User) | ||
user_class = private(s.User) | ||
smart_user_class = delegated(s.UserType)(s.SmartUser) | ||
user = user_class(last_login=datetime(1999, 12, 31)) | ||
assert not user.is_active() | ||
smart_user = smart_user_class(user) | ||
assert not smart_user.is_active() |