Skip to content

Commit cc06341

Browse files
committed
Add todo note because infering type in Aggregation comprehensions is unreliable for now
1 parent 431a977 commit cc06341

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

pylint/pyreverse/inspector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def handle(self, node: nodes.AssignAttr, parent: nodes.ClassDef) -> None:
396396
return
397397

398398
# Aggregation: comprehensions without object creation (self.x = [existing_obj for ...])
399+
# TODO: Currently inferring type of existing_obj is not reliable ==> improve once astroid supports it
399400
if isinstance(
400401
value, (nodes.ListComp, nodes.DictComp, nodes.SetComp, nodes.GeneratorExp)
401402
):

tests/pyreverse/functional/class_diagrams/associations/comprehensions.mmd

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ classDiagram
88
components_set : set[Component]
99
lazy_components : Generator[Component]
1010
}
11-
class AggregationContainer {
12-
components : list[Component]
13-
component_dict : dict[str, Component]
14-
components_set : set[Component]
15-
lazy_components : Generator[Component]
16-
}
1711
class CompositionContainer {
1812
components : list[Component]
1913
component_dict : dict[int, Component]
@@ -24,11 +18,7 @@ classDiagram
2418
Component --> AssociationContainer : component_dict
2519
Component --> AssociationContainer : components_set
2620
Component --> AssociationContainer : lazy_components
27-
Component --o AggregationContainer : components
28-
Component --o AggregationContainer : component_dict
29-
Component --o AggregationContainer : components_set
30-
Component --o AggregationContainer : lazy_components
3121
Component --* CompositionContainer : components
3222
Component --* CompositionContainer : component_dict
3323
Component --* CompositionContainer : components_set
34-
Component --* CompositionContainer : lazy_components
24+
Component --* CompositionContainer : lazy_components
Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
# Test for https://github.com/pylint-dev/pylint/issues/10236
22
from collections.abc import Generator
3+
from dataclasses import dataclass
34

45

56
class Component:
67
"""A component class."""
78
def __init__(self, name: str):
89
self.name = name
910

10-
1111
class AssociationContainer:
1212
"""Type hints only - no ownership."""
13-
# Association: just type hints, no actual assignment
14-
components: list[Component]
15-
component_dict: dict[int, Component]
16-
components_set: set[Component]
17-
lazy_components: Generator[Component]
18-
19-
20-
class AggregationContainer:
21-
"""Comprehensions using existing objects - aggregation."""
22-
def __init__(self, existing_components: list[Component]):
23-
# Aggregation: comprehensions using existing objects (not creating)
24-
self.components: list[Component] = [comp for comp in existing_components]
25-
self.component_dict: dict[str, Component] = {f"key_{i}": comp for i, comp in enumerate(existing_components)}
26-
self.components_set: set[Component] = {comp for comp in existing_components}
27-
self.lazy_components: Generator[Component] = (comp for comp in existing_components)
28-
13+
def __init__(self):
14+
# Association: just type hints, no actual assignment
15+
self.components: list[Component]
16+
self.component_dict: dict[int, Component]
17+
self.components_set: set[Component]
18+
self.lazy_components: Generator[Component]
2919

3020
class CompositionContainer:
3121
"""Comprehensions creating new objects - composition."""
@@ -34,4 +24,4 @@ def __init__(self):
3424
self.components: list[Component] = [Component(f"component_{i}") for i in range(3)]
3525
self.component_dict: dict[int, Component] = {i: Component(f"dict_component_{i}") for i in range(2)}
3626
self.components_set: set[Component] = {Component(f"set_component_{i}") for i in range(2)}
37-
self.lazy_components: Generator[Component] = (Component(f"lazy_{i}") for i in range(2))
27+
self.lazy_components: Generator[Component] = (Component(f"lazy_{i}") for i in range(2))

0 commit comments

Comments
 (0)