Skip to content

Commit 29dea7a

Browse files
committed
Fix bug when class attributes are accidentally inherited
Skip type annotations that are accidentally inherited from the parent class since __annotations__ attribute of a class may inadvertently return the annotations dict of a base class.
1 parent aacf497 commit 29dea7a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

py2puml/inspection/inspectclass.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,21 @@ def inspect_static_attributes(
5454
# investigate_domain_definition(class_type)
5555

5656
type_annotations = getattr(class_type, '__annotations__', None)
57+
parent_class_type = getattr(class_type, '__bases__', None)[0]
58+
parent_type_annotations = getattr(parent_class_type, '__annotations__', None)
59+
5760
if type_annotations is not None:
5861
# stores only once the compositions towards the same class
5962
relations_by_target_fqdn: Dict[str: UmlRelation] = {}
6063
# utility which outputs the fully-qualified name of the attribute types
6164
module_resolver = ModuleResolver(import_module(class_type.__module__))
6265

63-
# builds the definitions of the class attrbutes and their relationships by iterating over the type annotations
66+
# builds the definitions of the class attributes and their relationships by iterating over the type annotations
6467
for attr_name, attr_class in type_annotations.items():
68+
# Skip class attributes accidentally inherited from parent class
69+
if parent_type_annotations and attr_name in parent_type_annotations.keys():
70+
continue
71+
6572
attr_raw_type = str(attr_class)
6673
concrete_type_match = CONCRETE_TYPE_PATTERN.search(attr_raw_type)
6774
# basic type

0 commit comments

Comments
 (0)