-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor pyerverse 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
Julfried
wants to merge
8
commits into
pylint-dev:main
Choose a base branch
from
Julfried:fix-agg-comp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
37b4d98
Correct the test output to follow UML semantics
Julfried f45bc2c
Introduce composition
Julfried 5632b32
Update all the printers to emit the right arrow types
Julfried a48f21d
Update docstring
Julfried 980e7ef
Remove type annotations from test output
Julfried ba28096
Avoid processing duplicate relationships
Julfried b2f5743
Update expected files again -> defaults to association
Julfried d0ea86e
change arrowhead for dot language for association relationsships
Julfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,9 @@ class Linker(IdGeneratorMixIn, utils.LocalsVisitor): | |
|
||
* aggregations_type | ||
as instance_attrs_type but for aggregations relationships | ||
|
||
* compositions_type | ||
as instance_attrs_type but for compositions relationships | ||
""" | ||
|
||
def __init__(self, project: Project, tag: bool = False) -> None: | ||
|
@@ -122,8 +125,14 @@ def __init__(self, project: Project, tag: bool = False) -> None: | |
self.tag = tag | ||
# visited project | ||
self.project = project | ||
self.associations_handler = AggregationsHandler() | ||
self.associations_handler.set_next(OtherAssociationsHandler()) | ||
|
||
# Chain: Composition β Aggregation β Association | ||
self.associations_handler = CompositionsHandler() | ||
aggregation_handler = AggregationsHandler() | ||
association_handler = AssociationsHandler() | ||
|
||
self.associations_handler.set_next(aggregation_handler) | ||
aggregation_handler.set_next(association_handler) | ||
|
||
def visit_project(self, node: Project) -> None: | ||
"""Visit a pyreverse.utils.Project node. | ||
|
@@ -167,6 +176,7 @@ def visit_classdef(self, node: nodes.ClassDef) -> None: | |
specializations.append(node) | ||
baseobj.specializations = specializations | ||
# resolve instance attributes | ||
node.compositions_type = collections.defaultdict(list) | ||
node.instance_attrs_type = collections.defaultdict(list) | ||
node.aggregations_type = collections.defaultdict(list) | ||
node.associations_type = collections.defaultdict(list) | ||
|
@@ -327,28 +337,50 @@ def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None: | |
self._next_handler.handle(node, parent) | ||
|
||
|
||
class CompositionsHandler(AbstractAssociationHandler): | ||
"""Handle composition relationships where parent creates child objects.""" | ||
|
||
def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None: | ||
if not isinstance(node.parent, (nodes.AnnAssign, nodes.Assign)): | ||
super().handle(node, parent) | ||
return | ||
|
||
value = node.parent.value | ||
|
||
# Composition: parent creates child (self.x = P()) | ||
if isinstance(value, nodes.Call): | ||
current = set(parent.compositions_type[node.attrname]) | ||
parent.compositions_type[node.attrname] = list( | ||
current | utils.infer_node(node) | ||
) | ||
return | ||
|
||
# Not a composition, pass to next handler | ||
super().handle(node, parent) | ||
|
||
|
||
class AggregationsHandler(AbstractAssociationHandler): | ||
"""Handle aggregation relationships where parent receives child objects.""" | ||
|
||
def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None: | ||
# Check if we're not in an assignment context | ||
if not isinstance(node.parent, (nodes.AnnAssign, nodes.Assign)): | ||
super().handle(node, parent) | ||
return | ||
|
||
value = node.parent.value | ||
|
||
# Handle direct name assignments | ||
# Aggregation: parent receives child (self.x = x) | ||
if isinstance(value, astroid.node_classes.Name): | ||
current = set(parent.aggregations_type[node.attrname]) | ||
parent.aggregations_type[node.attrname] = list( | ||
current | utils.infer_node(node) | ||
) | ||
return | ||
|
||
# Handle comprehensions | ||
# Aggregation: comprehensions (self.x = [P() for ...]) | ||
if isinstance( | ||
value, (nodes.ListComp, nodes.DictComp, nodes.SetComp, nodes.GeneratorExp) | ||
): | ||
# Determine the type of the element in the comprehension | ||
if isinstance(value, nodes.DictComp): | ||
element_type = safe_infer(value.value) | ||
else: | ||
|
@@ -358,12 +390,23 @@ def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None: | |
parent.aggregations_type[node.attrname] = list(current | {element_type}) | ||
return | ||
|
||
# Fallback to parent handler | ||
# Type annotation only (x: P) defaults to aggregation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the PR description the type annotation case would be association, not aggregation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, thank you. That case needs to move aswell |
||
if isinstance(node.parent, nodes.AnnAssign) and node.parent.value is None: | ||
current = set(parent.aggregations_type[node.attrname]) | ||
parent.aggregations_type[node.attrname] = list( | ||
current | utils.infer_node(node) | ||
) | ||
return | ||
|
||
# Not an aggregation, pass to next handler | ||
super().handle(node, parent) | ||
|
||
|
||
class OtherAssociationsHandler(AbstractAssociationHandler): | ||
class AssociationsHandler(AbstractAssociationHandler): | ||
"""Handle regular association relationships.""" | ||
|
||
def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None: | ||
# Everything else is a regular association | ||
current = set(parent.associations_type[node.attrname]) | ||
parent.associations_type[node.attrname] = list(current | utils.infer_node(node)) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,8 @@ classDiagram | |
} | ||
class P { | ||
} | ||
P --* A : x | ||
P --* C : x | ||
P --> A : x | ||
P --* D : x | ||
P --* E : x | ||
P --o B : x | ||
P --o C : x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be composition as well as in the example
P
is instantiated by the class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I would agree here. I think we need to differentiate here aswell (I need to handle list comprehensions in both the AggregationsHandler and the CompostionsHandler)