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

Change order of search path to fix inconsistency between pylint and astroid. #2589

Merged
merged 8 commits into from
Oct 8, 2024

Conversation

akamat10
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

While testing pylint-dev/pylint#9967, I noticed that the qualified package names are incorrect when linted files are outside of the source-roots specified. This PR changes the priority of the search path which then correctly picks up the right qualified module name. It makes sense to give higher priority to the additional search paths over sys.path. This is in line with priority order of expand_modules function in pylint. Hopefully the maintainers agree that this inconsistency needs to be fixed.

Refs pylint-dev/pylint#9955

@akamat10
Copy link
Contributor Author

Also, both expand_module in pylint and modpath_from_file_with_callback in astroid are taking sys.path for module discovery with expand_module calling modpath_from_file_with_callback. It may be makes sense to simplify such that it is only happening in one of these functions

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Bug πŸͺ³ and removed Enhancement ✨ Improvement to a component labels Sep 30, 2024
@Pierre-Sassoulas
Copy link
Member

Yeah let's make pylint rely on astroid's fonction or at least comment on the similarity if we want to reduce coupling.

Copy link

codecov bot commented Sep 30, 2024

Codecov Report

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

Project coverage is 93.25%. Comparing base (6679ec2) to head (826fe0c).
Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2589      +/-   ##
==========================================
+ Coverage   93.24%   93.25%   +0.01%     
==========================================
  Files          93       93              
  Lines       11049    11068      +19     
==========================================
+ Hits        10303    10322      +19     
  Misses        746      746              
Flag Coverage Ξ”
linux 93.14% <100.00%> (+0.01%) ⬆️
pypy 93.25% <100.00%> (+0.01%) ⬆️
windows 93.24% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Ξ”
astroid/modutils.py 89.47% <100.00%> (ΓΈ)
astroid/util.py 90.62% <100.00%> (+2.31%) ⬆️

@akamat10
Copy link
Contributor Author

Great! Let me add a test with this PR before this is merged.

modutils.modpath_from_file(f"{sub_dir}/{mod_name}.py", [sub_dir]),
[mod_name],
)
finally:
Copy link
Member

Choose a reason for hiding this comment

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

I think elsewhere we use a context manager for sys path modification in tests. Sorry I can't search as I'm on mobile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will check. Don't remember seeing one in astroid repo. There is one in pylint repo. Perhaps you are remembering that one? If there isn't one, you want me to create one similar to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't find it. Will create one.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I did not realize it was an astroid PR πŸ˜…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

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.

πŸ‘Œ

@Pierre-Sassoulas Pierre-Sassoulas merged commit f201120 into pylint-dev:main Oct 8, 2024
20 checks passed


@contextlib.contextmanager
def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we do a follow up where we move this into the test directory. By putting it here we add it to our global API, whereas it is only used in tests and we don't want to have to keep supporting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I saw that it is public for pylint and followed that option. However, what you are saying makes sense. Another option is to mark it private by pretending it with _. That would then be available for other modules in astroid. Open to either one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Based on the experience I had with deprecating stuff with astroid and pylint 2.x I'd say move it to tests. If we ever need it we can always move it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Will change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants