Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

tests: add testcase that shows that pyreverse will not extract the inheritance link for a flat folder as described in #7686#9693

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Hannoma wants to merge2 commits intopylint-dev:main
base:main
Choose a base branch
Loading
fromANTICIPATE-GmbH:tests/7686/no-inheritance-relationships-for-flat-folder

Conversation

Hannoma
Copy link

Type of Changes

Type
🐛 Bug fix

Description

As described in#7686, pyreverse does not extract the inheritance link for classes living in different files. In this PR I haved added the possibility to easily write testcases for pyreverse to generate a diagram for a complete directory / module. As far as I can tell this was not possible before just using the functional tests.

I also have a possible fix in mind for this issue, but am not sure, if I should add it in this PR or open another one. As@DudeNr33 suggested, I started with writing the test to reproduce the described issue.

Refs#7686

Gornoka reacted with thumbs up emoji
@HannomaHannoma requested a review fromDudeNr33 as acode ownerJune 4, 2024 13:40
@HannomaHannoma changed the titletests: add testcase that shows that pyreverse will not extract the inheritance link for a flat folder as described in https://github.com/pylint-dev/pylint/issues/7686tests: add testcase that shows that pyreverse will not extract the inheritance link for a flat folder as described in #7686Jun 4, 2024
@Hannoma
Copy link
Author

What I have found out while debugging:
When thenode.ancestors(recurs=False) function is called in diagrams.py lines 213-219

# inheritance linkforpar_nodeinnode.ancestors(recurs=False):try:par_obj=self.object_from_node(par_node)self.add_relationship(obj,par_obj,"specialization")exceptKeyError:continue

the ancestors are not extracted correctly as the cached InferenceContext maps the statement of the name of the base classes to "Uninferable"
image

@Hannoma
Copy link
Author

Hannoma commentedJun 4, 2024
edited
Loading

As I really do not know what is going on there in astroid, my quick and dirty solution would be to extract the correct base classes via their name:

# inheritance linksforpar_nodeinnode.bases:# Name nodes are used for simple types, e.g. class A(B):ifisinstance(par_node,astroid.Name):try:par_obj=self.classe(par_node.name)self.add_relationship(obj,par_obj,"specialization")exceptKeyError:continue# Subscript is used for generic types, e.g. class A(Generic[T]):ifisinstance(par_node,astroid.Subscript):try:par_obj=self.classe(par_node.value.name)self.add_relationship(obj,par_obj,"specialization")exceptKeyError:continue

I have pushed this simple fix tohttps://github.com/ANTICIPATE-GmbH/pylint/tree/fix/7686/extract-inheritance-relations-correctly. Note that with this fix, all test cases complete without issues.

@github-actionsGitHub Actions

This comment has been minimized.

@Pierre-SassoulasPierre-Sassoulas added pyreverseRelated to pyreverse component Needs decision 🔒Needs a decision before implemention or rejection Needs design proposal 🔒This is a huge feature, some discussion should happen before a PR is proposed and removed Needs design proposal 🔒This is a huge feature, some discussion should happen before a PR is proposed labelsJun 4, 2024
Copy link
Collaborator

@DudeNr33DudeNr33 left a comment

Choose a reason for hiding this comment

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

Thanks for taking this up. I have not found the time yet to check if the problem itself could/should be fixed inastroid rather thanpylint, so I will just focus on the testing extension for now:

I would indeed suggest to keep the testing extension and the concrete fix as two separate merge requests. I left some first thoughts.

It would be great if this could be used for functional tests of package diagrams as well, though this could be added later in a separate MR.

@github-actionsGitHub Actions
Copy link
Contributor

🤖 According to the primer, this change hasno effect on the checked open source code. 🤖🎉

This comment was generated for commitd99f984

@DudeNr33
Copy link
Collaborator

I tried it out locally and I quite like it.
As mentioned I would like to expand on this to also include a functional test harness for package diagrams.
There already exists some test data undertests/pyreverse/functional/package_diagrams, which however does not build upontest_pyreverse_functional.py to collect the test data.

With your change and some very minor modifications, we can move these files into the newtests/pyreverse/functional/packages directory (we can do that in a follow up PR, just want to note down what I did locally):

diff --git a/pylint/testutils/pyreverse.py b/pylint/testutils/pyreverse.pyindex 9e6647183..03840944e 100644--- a/pylint/testutils/pyreverse.py+++ b/pylint/testutils/pyreverse.py@@ -72,6 +72,7 @@ class TestFileOptions(TypedDict):     source_roots: list[str]     output_formats: list[str]     command_line_args: list[str]+    diagram_type: str   class FunctionalPyreverseTestfile(NamedTuple):@@ -150,4 +151,5 @@ def _read_config(config_file: Path) -> TestFileOptions:         "command_line_args": shlex.split(             config.get("testoptions", "command_line_args", fallback="")         ),+        "diagram_type": config.get("testoptions", "diagram_type", fallback="classes"),     }diff --git a/tests/pyreverse/test_pyreverse_functional.py b/tests/pyreverse/test_pyreverse_functional.pyindex beeb0330b..e02574463 100644--- a/tests/pyreverse/test_pyreverse_functional.py+++ b/tests/pyreverse/test_pyreverse_functional.py@@ -86,5 +86,5 @@ def test_packages(test_package: FunctionalPyreverseTestfile, tmp_path: Path) ->             Run(args)         assert sys_exit.value.code == 0         assert output_file.read_text(encoding="utf8") == (-            tmp_path / f"classes.{output_format}"+            tmp_path / f"{test_package.options['diagram_type']}.{output_format}"         ).read_text(encoding="utf8")

This essentially just adds an option to the .rc file so that the author can specify if his tests generate a class or a package diagram.

As a next step regarding this MR, I think we can simplify the code a bit. For example, theget_functional_test_files() andget_functional_test_packages() in thetestutils package share some common logic to handle the.rc files, and thetest_class_diagrams() andtest_packages() intest_pyreverse_functional as well.

For the suggested fix, I am hesitant to judge if this is the best way to fix it, as I currently don't find the time to work myself back intoastroid and debug this issue to understand what is going on. Maybe one of the other maintainers has time to take a look (maybe@nickdrozd?) , otherwise I have to ask for your patience until I can take the time to properly look at it.

@DanielNoordDanielNoord added Needs take over 🛎️Orignal implementer went away but the code could be salvaged. and removed Needs decision 🔒Needs a decision before implemention or rejection labelsMar 30, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@DudeNr33DudeNr33DudeNr33 left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
Needs take over 🛎️Orignal implementer went away but the code could be salvaged.pyreverseRelated to pyreverse component
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@Hannoma@DudeNr33@Pierre-Sassoulas@DanielNoord

[8]ページ先頭

©2009-2025 Movatter.jp