Skip to content

Refactor pyreverse Association Logic #10397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c555bf4
Correct the test output to follow UML semantics
Julfried May 18, 2025
2f5bc4e
Introduce composition
Julfried May 18, 2025
884ef49
Update all the printers to emit the right arrow types
Julfried May 18, 2025
d4804af
Update docstring
Julfried May 18, 2025
3b84300
Remove type annotations from test output
Julfried May 18, 2025
1899f46
Avoid processing duplicate relationships
Julfried May 18, 2025
4b3eacd
Update expected files again -> defaults to association
Julfried May 18, 2025
7dd8acb
change arrowhead for dot language for association relationsships
Julfried May 18, 2025
cbecf36
Update comment
Julfried Jun 14, 2025
0a4488d
Update comments in test file
Julfried Jun 28, 2025
376d1b5
rename test folders for better clarity
Julfried Jun 28, 2025
d3677ea
Enhance composition and aggregation handling in AST node processing s…
Julfried Jun 28, 2025
bedf32f
Update relationship extraction to avoid duplicate entries
Julfried Jun 28, 2025
e6e0daf
Correctly infer the node type for Composition
Julfried Jun 28, 2025
107f5a1
Update the functional test for comprehensions aswell
Julfried Jun 28, 2025
d62e8d5
Instead of checking not call node check for name node ==> more explicit
Julfried Jun 28, 2025
431a977
Remove redundant checks in AggregationHandler
Julfried Jun 29, 2025
cc06341
Add todo note because infering type in Aggregation comprehensions is …
Julfried Jun 29, 2025
3f108cf
Enhance type resolution in AssociationsHandler and add utility functi…
Julfried Jun 29, 2025
47ab016
Fix order so that tests pass
Julfried Jun 29, 2025
4f92aff
Update the functional test files for attribute annotation, because no…
Julfried Jun 29, 2025
b5222e5
Use the new utility function for the other handlers aswell
Julfried Jun 29, 2025
54942be
Fix regression that did not correctly detect Composition in fields.py
Julfried Jun 29, 2025
0068717
Revert functional test for comprehension ==> this now works with new …
Julfried Jun 29, 2025
7dbfd7b
Add correct arrow type for dot printer
Julfried Jun 29, 2025
5e2954c
Update functional tests to include all file formats
Julfried Jun 29, 2025
2521686
Fix diadefs tests (DoNothing now is correctly detected as Composition…
Julfried Jun 29, 2025
0cce815
Remove TODO since this now works
Julfried Jun 29, 2025
148f2fb
Rename functional test files to relationships for better clarity
Julfried Jun 29, 2025
aa46267
rename to compositionshandler for better clarity
Julfried Jun 29, 2025
ba56957
Rename association-related classes to relationship aswell for improve…
Julfried Jun 29, 2025
5d4ab20
Try to fix the failing tests by processing instance attributes and cl…
Julfried Jun 29, 2025
d162fd0
Fix diadefs_test again (cls_member is now also correctly identified a…
Julfried Jun 29, 2025
72888e8
Also consider composition when when filtering for --no-standalone ==>…
Julfried Jun 29, 2025
d31c956
Add newsfragment
Julfried Jun 29, 2025
2c46d17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 29, 2025
d1578a3
Add functional tests
Julfried Jun 30, 2025
4e753d1
Update newsfragement
Julfried Jun 30, 2025
04f4b2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9045.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Enhanced pyreverse to properly distinguish between UML relationship types (association, aggregation, composition) based on object ownership semantics. Type annotations without assignment are now treated as associations, parameter assignments as aggregations, and object instantiation as compositions.

Closes #9045
Closes #9267
31 changes: 23 additions & 8 deletions pylint/pyreverse/diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def extract_relationships(self) -> None:
obj.attrs = self.get_attrs(node)
obj.methods = self.get_methods(node)
obj.shape = "class"

# inheritance link
for par_node in node.ancestors(recurs=False):
try:
Expand All @@ -234,27 +235,41 @@ def extract_relationships(self) -> None:
except KeyError:
continue

# associations & aggregations links
for name, values in list(node.aggregations_type.items()):
# Track processed attributes to avoid duplicates
processed_attrs = set()

# Process in priority order: Composition > Aggregation > Association

# 1. Composition links (highest priority)
for name, values in list(node.compositions_type.items()):
if not self.show_attr(name):
continue
for value in values:
if not self.show_attr(name):
continue
self.assign_association_relationship(
value, obj, name, "composition"
)
processed_attrs.add(name)

# 2. Aggregation links (medium priority)
for name, values in list(node.aggregations_type.items()):
if not self.show_attr(name) or name in processed_attrs:
continue
for value in values:
self.assign_association_relationship(
value, obj, name, "aggregation"
)
processed_attrs.add(name)

# 3. Association links (lowest priority)
associations = node.associations_type.copy()

for name, values in node.locals_type.items():
if name not in associations:
associations[name] = values

for name, values in associations.items():
if not self.show_attr(name) or name in processed_attrs:
continue
for value in values:
if not self.show_attr(name):
continue

self.assign_association_relationship(
value, obj, name, "association"
)
Expand Down
8 changes: 7 additions & 1 deletion pylint/pyreverse/dot_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ class HTMLLabels(Enum):
# pylint: disable-next=consider-using-namedtuple-or-dataclass
ARROWS: dict[EdgeType, dict[str, str]] = {
EdgeType.INHERITS: {"arrowtail": "none", "arrowhead": "empty"},
EdgeType.ASSOCIATION: {
EdgeType.COMPOSITION: {
"fontcolor": "green",
"arrowtail": "none",
"arrowhead": "diamond",
"style": "solid",
},
EdgeType.ASSOCIATION: {
"fontcolor": "green",
"arrowtail": "none",
"arrowhead": "vee",
"style": "solid",
},
EdgeType.AGGREGATION: {
"fontcolor": "green",
"arrowtail": "none",
Expand Down
Loading
Loading