Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

issue in patch decorator when worker with abstract classes #536

Open
@loayshaqir1

Description

@loayshaqir1

patch decorator is a little bit problematic when working with abstract classes,
consider the following code:

fromabcimportABC,abstractmethodfromfastcore.basicsimportpatchclassA(ABC):@abstractmethoddeffoo(self):passclassB(A):def__init__(self):pass@patchdeffoo(self :B):print("foo patched to B")a=B()Executingthecodeyieldsthiserror:
TypeError:Can'tinstantiateabstractclassBwithabstractmethodsfoo

The reason for that problem is that in Python each class that is inheriting from an abstract class he starts with an attribute calledabstractmethods which is a frozenset that contains a set of functions that should be implemented by the child class, this attribute gets updated only in the scope of the class definition and at the end the set should be empty i.e the child class has implemented all the abstractmethods, since we are using patch to separate functions into different cells, Python doesn't see that we actually implemented the required functions.

possible solution, that's the decorator that we used in our code (you can learn from it what you should do to fix on your end):

defpatch_method(func :Callable,*args,**kwargs)->None:"""    Applies fastcore's `patch` decorator and removes `func` from `cls.__abstractsmethods__` in case <br>    `func` is an `abstractmethods`    """cls=next(iter(get_type_hints(func).values()))try:abstracts_needed=set(cls.__abstractmethods__)abstracts_needed.discard(func.__name__)cls.__abstractmethods__=abstracts_neededexceptAttributeError:# If the class does not inherit from an abstract classpassfinally:# Apply the original `patch` decoratorpatch(*args,**kwargs)(func)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp