Skip to content

patch / patch_to doesn't work with inheritance #535

Description

@dsm-72

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))
> [[], [], [], []]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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