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

Fix source root not recognized #10036

Merged
merged 6 commits into from
Oct 27, 2024
Merged

Conversation

Julfried
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix
✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Closes #10026

This PR fixes a bug in the discover_package_path function in pylint/pyreverse. The function was incorrectly handling cases where the source root is a child of the module directory, causing issues when using pyreverse with certain project structures, particularly those with a src layout.

The fix modifies the condition for identifying the correct source root, allowing it to handle both cases where the source root is a parent or a child of the module directory.

Changes made:

  • Updated the condition in discover_package_path function to correctly identify the source root:
    if os.path.commonpath([source_root, dirname]) in [dirname, source_root]:
        return source_root

Copy link

codecov bot commented Oct 20, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.80%. Comparing base (2e0c41f) to head (93f980b).
Report is 20 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10036   +/-   ##
=======================================
  Coverage   95.80%   95.80%           
=======================================
  Files         174      174           
  Lines       18937    18937           
=======================================
  Hits        18143    18143           
  Misses        794      794           
Files with missing lines Coverage Ξ”
pylint/lint/expand_modules.py 95.29% <100.00%> (ΓΈ)

This comment has been minimized.

@DanielNoord
Copy link
Collaborator

Thanks for the fix! Are you able to add a test for this as well?

@jacobtylerwalls jacobtylerwalls added this to the 4.0.0 milestone Oct 21, 2024
@Julfried
Copy link
Contributor Author

Thank you, I will take a look into it this week!

@Pierre-Sassoulas Pierre-Sassoulas added Bug πŸͺ² pyreverse Related to pyreverse component labels Oct 21, 2024
@Julfried Julfried requested a review from DudeNr33 as a code owner October 22, 2024 08:11

This comment has been minimized.

@DanielNoord
Copy link
Collaborator

Could you fix CI for the style and changeling checks? I think the PR itself looks really good already, thanks! πŸ˜„

@Julfried
Copy link
Contributor Author

Thank you, typo should be fixed

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

This comment has been minimized.

This comment has been minimized.

@Julfried
Copy link
Contributor Author

Thank you. The fragment should be added!

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Thanks!

@DanielNoord DanielNoord enabled auto-merge (squash) October 27, 2024 20:46
@DanielNoord DanielNoord merged commit be53085 into pylint-dev:main Oct 27, 2024
44 checks passed
Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 93f980b

@Julfried Julfried deleted the fix-source-root branch October 29, 2024 21:49
@echoix
Copy link

echoix commented Nov 17, 2024

Hi!

I stumbled upon the same issue that this PR solves. Using a pylint version installed with a git url+hash through pip makes using source-roots work. Since it is not published yet, do you have any guidance on how source-roots should have been used originally? I can't find any usage pattern that would make it work at all according to the couple sentences present in the docs. I tried for hours now stepping through "/usr/local/python/3.12.7/lib/python3.12/site-packages/pylint/lint/expand_modules.py:discover_package_path()" with different combinations of invocations trying to have one that would match the one when using the developpement version (pip install git+https://github.com/pylint-dev/pylint@9b272cca71d0e1733c0aa75dc05d293ec99d44c0, uninstalling pylint before changing between released and git versions.)

If there's absolutely no workaround, can you consider backporting this specific fix to a version that would be released sooner?

github-actions bot pushed a commit that referenced this pull request Nov 17, 2024
@Pierre-Sassoulas
Copy link
Member

Hi @echoix I've created #10083 to backport, waiting for the opinion of other maintainers before merging because this is a part that is hard to test and historically caused a lot of problem (maybe not well suited to a patch release). While waiting for the release you can use an install using git for example: pip install git+git://http://github.com/pylint-dev/pylint.git@main#egg=pylint -U

@echoix
Copy link

echoix commented Nov 19, 2024

Thanks @Pierre-Sassoulas! Indeed the git version could be used, but at some point to finish off my PR I should need a release at one point.
Our codebase is stuck with a very old Pylint 2 version, and the most I was able to get it updated before breaking other jobs in the workflow was 2.12.2, but the Python version support is becoming limited. I worked against starting off from scratch with a complete new config, with Pylint 3.x.
What's hard with implementing it gradually is that contrary to flake8 and ruff, there's no way to have in a single place a list of files that ignores one or multiple rules each. That pattern is quite useful, as we can then fix gradually the issues (and we do!). For example, if an certain type of issue is in every __init__.py, we can specify it there until ready to fix, without disabling the rule for everywhere else, preventing introducing new problems. There's some plugin that seem to work in this idea, but I'm not that successful yet.

@Pierre-Sassoulas
Copy link
Member

What I did in a project where I had this issue recently was to modify the python path with an init hook to add the source root to it init-hook='import sys; sys.path.append("/path/to/sourceroot")'. (I did not actually check if this PR fix my issue, the problem with this hack is that it ruined my motivation to actually investigate the root cause in pylint... :/ ).

@echoix
Copy link

echoix commented Nov 19, 2024

What I did in a project where I had this issue recently was to modify the python path with an init hook to add the source root to it init-hook='import sys; sys.path.append("/path/to/sourceroot")'. (I did not actually check if this PR fix my issue, the problem with this hack is that it ruined my motivation to actually investigate the root cause in pylint... :/ ).

Ours is quite sensitive to paths, as it uses some installed library and already have difficulty making normal imports work correctly... (we're working on it). The old way called Pylint 3 times, and had to be in different directories, with settings for each, not really easy to use for every contributor.

@Pierre-Sassoulas
Copy link
Member

What's hard with implementing it gradually is that contrary to flake8 and ruff, there's no way to have in a single place a list of files that ignores one or multiple rules each. That pattern is quite useful, as we can then fix gradually the issues (and we do!). For example, if an certain type of issue is in every init.py, we can specify it there until ready to fix, without disabling the rule for everywhere else, preventing introducing new problems. There's some plugin that seem to work in this idea, but I'm not that successful yet.

You can list disable in the pylintrc, or at the top of files and progressively delete them. Not ideal, I know. There's #5403

Pierre-Sassoulas pushed a commit that referenced this pull request Nov 20, 2024
(cherry picked from commit be53085)

Co-authored-by: Julfried <[email protected]>
webknjaz added a commit to webknjaz/pylint-dev--pylint that referenced this pull request Jan 8, 2025
This patch modifies the `test_discover_package_path_source_root_as_*`
tests to also run against directory layouts with no exlicitly existing
`__init__.py` file.

They were added in pylint-dev#10036 and seem to be insufficient, not covering
PEP 420 implicit namespaces.
Pierre-Sassoulas pushed a commit that referenced this pull request Jan 8, 2025
This patch modifies the `test_discover_package_path_source_root_as_*`
tests to also run against directory layouts with no exlicitly existing
`__init__.py` file.

They were added in #10036 and seem to be insufficient, not covering
PEP 420 implicit namespaces.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Source root not correctly used
5 participants