Skip to content

Commit

Permalink
Fix super() on methods defined in base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeGak committed Aug 22, 2024
1 parent 5c54844 commit 4b5b8c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/indico_patcher/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
PatchWrapper: TypeAlias = ClassWrapper | EnumWrapper # noqa: UP040

# Annotations for extra attributes in patched classes
class PatchedClass:
class PatchedClass(type):
__patches__: list[type]
__unpatched__: dict[str, dict[str, Any]]

Expand Down
6 changes: 3 additions & 3 deletions src/indico_patcher/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ def _store_unpatched(orig_class: PatchedClass, member_name: str, category: str)
:param orig_class: The class to store the reference in
:param member_name: The name of the member to store the reference for
"""
orig_members = get_members(orig_class)
# None can be a valid value for the member, so we need to check if the member is in the class dict
if member_name in orig_class.__dict__:
if member_name in orig_members:
# TODO: Log warning if member is already patched
member = orig_class.__dict__[member_name]
orig_class.__unpatched__[category][member_name] = member
orig_class.__unpatched__[category][member_name] = orig_members[member_name]


def _inject_super_proxy(func: FunctionType, orig_class: PatchedClass) -> FunctionType:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ def test_store_unpatched_member(member_name, category, Fool):
assert Fool.__unpatched__[category][member_name] == Fool.__dict__[member_name]


@pytest.mark.parametrize(("member_name", "category"), [
("attr", "attributes"),
("prop", "properties"),
("hprop", "hybrid_properties"),
("meth", "methods"),
("cmeth", "classmethods"),
("smeth", "staticmethods"),
])
def test_store_unpatched_member_from_parent(member_name, category, Fool):
class Magician(Fool):
def spell(self):
pass

_store_unpatched(Magician, member_name, category)
assert Fool.__unpatched__[category][member_name] == Fool.__dict__[member_name]


# -- inject super proxy --------------------------------------------------------

def test_inject_super_proxy(Fool):
Expand Down

0 comments on commit 4b5b8c0

Please sign in to comment.