Skip to content

Commit 3460d8f

Browse files
committed
add class inheritance
1 parent f80137c commit 3460d8f

5 files changed

Lines changed: 33 additions & 1 deletion

File tree

py_codegen/test_fixtures/class_with_methods.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class ParentClassA:
2+
from_parent_a: int
3+
4+
5+
class ParentClassB:
6+
from_parent_b: str
7+
8+
9+
class ChildClass(ParentClassA, ParentClassB):
10+
b: str
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from py_codegen.test_fixtures.classes_with_inheritance import ChildClass
2+
3+
from py_codegen.type_extractor.nodes.BaseNodeType import BaseOption
4+
from py_codegen.type_extractor.nodes.ClassFound import ClassFound
5+
from py_codegen.type_extractor.nodes.TypeOR import TypeOR
6+
from py_codegen.type_extractor.__tests__.utils import traverse, cleanup
7+
from py_codegen.type_extractor.type_extractor import TypeExtractor
8+
9+
10+
def test_classes_with_inheritance():
11+
type_collector = TypeExtractor()
12+
13+
type_collector.add(None)(ChildClass)
14+
15+
print(type_collector)

py_codegen/type_extractor/middlewares/class_found.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def class_found_middleware(
3030
return duplicate
3131

3232
_data_class = dataclass(_class)
33+
base_classes = [
34+
type_extractor.rawtype_to_node(base_cls)
35+
for base_cls in list(_class.__bases__)
36+
if base_cls is not object and base_cls is not tuple
37+
]
3338
argspec = inspect.getfullargspec(_data_class)
3439
module = inspect.getmodule(_class)
3540
filename = module and module.__file__
@@ -38,6 +43,7 @@ def class_found_middleware(
3843
name=_class.__name__,
3944
class_raw=_class,
4045
filePath=filename,
46+
base_classes=base_classes,
4147
raw_fields=argspec.annotations,
4248
fields=fields,
4349
doc=_class.__doc__,

py_codegen/type_extractor/nodes/ClassFound.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Dict,
55
Any,
66
Callable,
7-
Set)
7+
Set, List)
88

99
from py_codegen.type_extractor.nodes.BaseNodeType import BaseNodeType, NodeType, BaseOption
1010

@@ -16,6 +16,7 @@ class ClassFound(BaseNodeType): # type: ignore
1616
filePath: str = field(default='')
1717
raw_fields: Dict[str, Any] = field(default_factory=dict)
1818
doc: str = field(default='')
19+
base_classes: List['ClassFound'] = field(default_factory=list)
1920
class_raw: Optional[type] = None
2021
INTERNAL_fields_extra: Optional[Dict[str, Any]] = None
2122
options: Set[BaseOption] = field(default_factory=set)

0 commit comments

Comments
 (0)