fastai / fastcore

Python supercharged for the fastai library

Home Page:http://fastcore.fast.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

patch / patch_to doesn't work with inheritance

dsm-72 opened this issue · comments

commented

See example below

cell 1

from fastcore.basics import patch, patch_to
class KeywordArgumentsMixin:
    ...
    # NOTE: this works if uncommented
    # @classmethod
    # def get_class_keywords(cls:KeywordArgumentsMixin) -> list: 
    #     return inspect.getfullargspec(cls.__init__).kwonlyargs

cell 2

@dataclass
class Foo(KeywordArgumentsMixin):
    _: KW_ONLY
    a: int = 1
    b: int = 2
    c: int = 3

@dataclass
class Qux(KeywordArgumentsMixin):
    _: KW_ONLY
    q: str = -2
    u: str = -1
    x: int = 0

#| export
@dataclass
class Bar(Foo):
    _: KW_ONLY
    x: int = 7
    y: int = 8
    z: int = 9

@dataclass
class Baz(Bar, Qux):
    _: KW_ONLY
    a: int = 6
    b: int = 7
    q: int = 0

cell 3

@patch(cls_method=True)
def get_class_keywords(cls:KeywordArgumentsMixin) -> list: 
    return inspect.getfullargspec(cls.__init__).kwonlyargs

cell 4

list(c.get_class_keywords() for c in (Foo, Qux, Bar, Baz))
> [[], [], [], []]