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 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6d4c487
Correct the test output to follow UML semantics
Julfried May 18, 2025
54fd26b
Introduce composition
Julfried May 18, 2025
19ae424
Update all the printers to emit the right arrow types
Julfried May 18, 2025
9f48c87
Update docstring
Julfried May 18, 2025
a561cee
Remove type annotations from test output
Julfried May 18, 2025
b526764
Avoid processing duplicate relationships
Julfried May 18, 2025
d5938bf
Update expected files again -> defaults to association
Julfried May 18, 2025
ff9d93d
change arrowhead for dot language for association relationsships
Julfried May 18, 2025
ec949f2
Update comment
Julfried Jun 14, 2025
74ebf7f
Update comments in test file
Julfried Jun 28, 2025
e7bd7ca
rename test folders for better clarity
Julfried Jun 28, 2025
4216545
Enhance composition and aggregation handling in AST node processing s…
Julfried Jun 28, 2025
6ee8117
Update relationship extraction to avoid duplicate entries
Julfried Jun 28, 2025
9677666
Correctly infer the node type for Composition
Julfried Jun 28, 2025
f3e7c23
Update the functional test for comprehensions aswell
Julfried Jun 28, 2025
02e2024
Instead of checking not call node check for name node ==> more explicit
Julfried Jun 28, 2025
2d157d8
Remove redundant checks in AggregationHandler
Julfried Jun 29, 2025
195a6f7
Add todo note because infering type in Aggregation comprehensions is …
Julfried Jun 29, 2025
31d93eb
Enhance type resolution in AssociationsHandler and add utility functi…
Julfried Jun 29, 2025
892c02e
Fix order so that tests pass
Julfried Jun 29, 2025
bc8b028
Update the functional test files for attribute annotation, because no…
Julfried Jun 29, 2025
36fe534
Use the new utility function for the other handlers aswell
Julfried Jun 29, 2025
780740b
Fix regression that did not correctly detect Composition in fields.py
Julfried Jun 29, 2025
aef07ca
Revert functional test for comprehension ==> this now works with new …
Julfried Jun 29, 2025
87d308f
Add correct arrow type for dot printer
Julfried Jun 29, 2025
8f59271
Update functional tests to include all file formats
Julfried Jun 29, 2025
4362e85
Fix diadefs tests (DoNothing now is correctly detected as Composition…
Julfried Jun 29, 2025
6e35d4b
Remove TODO since this now works
Julfried Jun 29, 2025
d1fd43c
Rename functional test files to relationships for better clarity
Julfried Jun 29, 2025
c21e64c
rename to compositionshandler for better clarity
Julfried Jun 29, 2025
5852435
Rename association-related classes to relationship aswell for improve…
Julfried Jun 29, 2025
70492f6
Try to fix the failing tests by processing instance attributes and cl…
Julfried Jun 29, 2025
f3f4364
Fix diadefs_test again (cls_member is now also correctly identified a…
Julfried Jun 29, 2025
e62636f
Also consider composition when when filtering for --no-standalone ==>…
Julfried Jun 29, 2025
9a3382f
Add newsfragment
Julfried Jun 29, 2025
86c92e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 29, 2025
c727e14
Add functional tests
Julfried Jun 30, 2025
07cda64
Update newsfragement
Julfried Jun 30, 2025
d275c2f
[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
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9045.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you acccidentally fixed multiple issues, you can close more then one :D

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