Skip to content
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

Pyreverse: Use dashed lines for type-checking imports #8824

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
23 changes: 19 additions & 4 deletions pylint/pyreverse/diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import astroid
from astroid import nodes, util

from pylint.checkers.utils import decorated_with_property
from pylint.checkers.utils import decorated_with_property, in_type_checking_block
from pylint.pyreverse.utils import FilterMixIn


Expand Down Expand Up @@ -281,9 +281,17 @@
def add_from_depend(self, node: nodes.ImportFrom, from_module: str) -> None:
"""Add dependencies created by from-imports."""
mod_name = node.root().name
obj = self.module(mod_name)
if from_module not in obj.node.depends:
obj.node.depends.append(from_module)
package = self.module(mod_name).node

if not in_type_checking_block(node):
if from_module not in package.depends:
package.depends.append(from_module)
else:
if (

Check warning on line 290 in pylint/pyreverse/diagrams.py

View check run for this annotation

Codecov / codecov/patch

pylint/pyreverse/diagrams.py#L290

Added line #L290 was not covered by tests
from_module not in package.type_depends
and from_module not in package.depends
):
package.type_depends.append(from_module)

Check warning on line 294 in pylint/pyreverse/diagrams.py

View check run for this annotation

Codecov / codecov/patch

pylint/pyreverse/diagrams.py#L294

Added line #L294 was not covered by tests

def extract_relationships(self) -> None:
"""Extract relationships between nodes in the diagram."""
Expand All @@ -304,3 +312,10 @@
except KeyError:
continue
self.add_relationship(package_obj, dep, "depends")

for dep_name in package_obj.node.type_depends:
try:
dep = self.get_module(dep_name, package_obj.node)
except KeyError:
continue
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These two lines are uncovered, and I don't know how to cover them. Any ideas? They could also be cut, or just ignore the coverage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The check is definitely required. But I can't figure out exactly what triggers it.

self.add_relationship(package_obj, dep, "type_depends")

Check warning on line 321 in pylint/pyreverse/diagrams.py

View check run for this annotation

Codecov / codecov/patch

pylint/pyreverse/diagrams.py#L317-L321

Added lines #L317 - L321 were not covered by tests
1 change: 1 addition & 0 deletions pylint/pyreverse/dot_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class HTMLLabels(Enum):
"style": "solid",
},
EdgeType.USES: {"arrowtail": "none", "arrowhead": "open"},
EdgeType.TYPE_USES: {"arrowtail": "none", "arrowhead": "empty"},
}


Expand Down
1 change: 1 addition & 0 deletions pylint/pyreverse/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def visit_module(self, node: nodes.Module) -> None:
return
node.locals_type = collections.defaultdict(list)
node.depends = []
node.type_depends = []
if self.tag:
node.uid = self.generate_id()

Expand Down
1 change: 1 addition & 0 deletions pylint/pyreverse/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EdgeType(Enum):
ASSOCIATION = "association"
AGGREGATION = "aggregation"
USES = "uses"
TYPE_USES = "type_uses"
nickdrozd marked this conversation as resolved.
Show resolved Hide resolved


class Layout(Enum):
Expand Down
7 changes: 7 additions & 0 deletions pylint/pyreverse/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
type_=EdgeType.USES,
)

for rel in diagram.get_relationships("type_depends"):
self.printer.emit_edge(

Check warning on line 80 in pylint/pyreverse/writer.py

View check run for this annotation

Codecov / codecov/patch

pylint/pyreverse/writer.py#L80

Added line #L80 was not covered by tests
rel.from_object.fig_id,
rel.to_object.fig_id,
type_=EdgeType.TYPE_USES,
)

def write_classes(self, diagram: ClassDiagram) -> None:
"""Write a class diagram."""
# sorted to get predictable (hence testable) results
Expand Down
Loading