Skip to content

Commit

Permalink
Fixed inheritance (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
couling authored Feb 13, 2024
1 parent 1fcb926 commit 51c26cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions dataclass_click/dataclass_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import dataclasses
import functools
import inspect
import operator
import types
import typing
Expand Down Expand Up @@ -225,7 +224,7 @@ def _collect_click_annotations(arg_class: typing.Type[Arg]) -> dict[str, _Delaye
:param arg_class: Dataclass to analyze
:return: A dictionary _DelayedCall keyed by attribute names"""
annotations: dict[str, _DelayedCall] = {}
for key, value in inspect.get_annotations(arg_class).items():
for key, value in typing.get_type_hints(arg_class, include_extras=True).items():
if typing.get_origin(value) is typing.Annotated:
for annotation in typing.get_args(value):
if isinstance(annotation, _DelayedCall):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,23 @@ def main(*args, **kwargs):
results: list[CallRecord] = []
quick_run(main)
assert results == [((), {"foo": Config(bar=None)})]


def test_inheritance():

@dataclass()
class Parent:
foo: Annotated[int | None, option()]

@dataclass
class Config(Parent):
bar: Annotated[int | None, option()]

@click.command()
@dataclass_click(Config)
def main(*args, **kwargs):
results.append((args, kwargs))

results: list[CallRecord] = []
quick_run(main, "--foo", "10", "--bar", "20")
assert results == [((Config(foo=10, bar=20), ), {})]

0 comments on commit 51c26cd

Please sign in to comment.