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

Lint all files in a directory by expanding arguments #5682

Merged
merged 34 commits into from
Feb 6, 2022

Conversation

matusvalo
Copy link
Collaborator

Type of Changes

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

Description

This PR is alternative to #5676 trying to resolve sys.path adjustment issue mentioned in #352 (comment).

The main rationale of this PR is to introduce recursive discovery of python modules/packages transparently to rest of pylint components. This is done by
expanding arguments containing directories with all modules and packages inside this directory. This approach should be safe with #352 (comment) since this change is totally transparent to sys.path adjustments in pylint. Hence for following subtree:

- test
     `+- a.py
      +- b.py
      +- c
          `+- __init__.py
           +- d.py

Executing command pylint --recursive test is equal to executing command pylint test/a.py test/b.py test/c.

Closes #352

@matusvalo matusvalo marked this pull request as draft January 14, 2022 23:00
@matusvalo
Copy link
Collaborator Author

Marking as draft, since still missing polishing, tests and documentation. I would like to ask just for review of approach.

@coveralls
Copy link

coveralls commented Jan 14, 2022

Pull Request Test Coverage Report for Build 1799934875

  • 14 of 15 (93.33%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 93.935%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/lint/pylinter.py 14 15 93.33%
Totals Coverage Status
Change from base Build 1784689763: 0.02%
Covered Lines: 14854
Relevant Lines: 15813

πŸ’› - Coveralls

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.

Yeah, I think this is pretty clever. In fact this is what was done currently (in bash or pre-commit by each users) when launching pylint with a long list of files, so imo this is pretty safe too. We're probably missing some optimization of doing the sys path manipulation less internally but the alternative is a very complex change and waiting even longer for what has been the most wanted feature of the project for years. Goob job !

@DanielNoord
Copy link
Collaborator

I think this is also pretty clever!

I wonder if we could make it so that pylint . is equal to pylint . -r. I think most users would want us to do that as well. Perhaps a bit out of the scope for this PR, but I think if we want to go ahead and do that we should include a PR that does so in the same release so it works as intended from the start.

@Pierre-Sassoulas
Copy link
Member

I wonder if we could make it so that pylint . is equal to pylint . -r.

pylint should also be equal to pylint . -r as well. I also think -r should become the default option in 3.0.

@DanielNoord
Copy link
Collaborator

I wonder if we could make it so that pylint . is equal to pylint . -r.

pylint should also be equal to pylint . -r as well. I also think -r should become the default option in 3.0.

Agreed! But I was wondering if for 2.x we can already make pylint . work like -r was provided. Although I wonder how many projects use pylint . already... Hm perhaps we shouldn't do this and just add it to the 3.0 list.

@Pierre-Sassoulas
Copy link
Member

already make pylint . work like -r was provided.

The intent is pretty clear when doing that imo, so I'd say it's safe to put in 2.x.

In fact my first instinct was to also make recursive the default in 2.x. But there's probably projects somewhere that use this "non feature" to exclude files from the linting,

@matusvalo
Copy link
Collaborator Author

Thank you for your feedback. Let me finish the PR. I will close the previous one.

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Command line Related to command line interface labels Jan 15, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Jan 15, 2022
@matusvalo matusvalo marked this pull request as ready for review January 15, 2022 21:50
@matusvalo
Copy link
Collaborator Author

matusvalo commented Jan 15, 2022

Test added, code cleaned up, added type annotations and docstrings.

I have 2 questions:

  1. About --recursive option. Right with no option there is old behaviour and later we should have --recursive option as default. But how will user then force old behaviour after the switch? New option will be added or we want to use --recursive=y/n option (in this case we won't need new option after)

  2. Should this be documented in pylint userguide (running pylint chapter)? It is useful to have there but soon it will be default if I understand that correctly.

Note: I used only --recursive option. -r option is already used for other purposes (for reports)

@Pierre-Sassoulas
Copy link
Member

About --recursive option. Right with no option there is old behaviour and later we should have --recursive option as default. But how will user then force old behaviour after the switch? New option will be added or we want to use --recursive=y/n option (in this case we won't need new option after)

If this was a new project, I would add a --no-recursive option, but in pylint the --recursive=y/n is clearly the thing user would expect. I don't think we'll break this convention even if we switch to argparse and are forced to do some breaking changes for 3.0, what do you think @DanielNoord ?

@DanielNoord
Copy link
Collaborator

About --recursive option. Right with no option there is old behaviour and later we should have --recursive option as default. But how will user then force old behaviour after the switch? New option will be added or we want to use --recursive=y/n option (in this case we won't need new option after)

If this was a new project, I would add a --no-recursive option, but in pylint the --recursive=y/n is clearly the thing user would expect. I don't think we'll break this convention even if we switch to argparse and are forced to do some breaking changes for 3.0, what do you think @DanielNoord ?

Would using --recursive=y/n allow equating --recursive with --recursive=y? I think being able to just use --recursive would be a highly request feature otherwise. Perhaps something with making all of these go to the same dest and making the options mutually exclusive? Like you did for pydocstringformatter.

  1. Should this be documented in pylint userguide (running pylint chapter)? It is useful to have there but soon it will be default if I understand that correctly.

Yes, I would appreciate some documentation about it. If we make this default we can just update the documentation to reflect this!

matusvalo and others added 2 commits January 26, 2022 19:00
Co-authored-by: DaniΓ«l van Noord <13665637+DanielNoord@users.noreply.github.com>
@matusvalo
Copy link
Collaborator Author

matusvalo commented Jan 26, 2022

Tests are failing for TestRunTC.test_regression_recursive_current_dir but it is working on my local computer.

EDIT: it seems that the TC fails only when it is executed by command pytest tests. When file is executed directly pytest tests/test_self.py, the test is passing.

@DanielNoord
Copy link
Collaborator

@matusvalo Does this pass for you locally?

@@ -100,6 +100,24 @@ def _configure_lc_ctype(lc_ctype: str) -> Iterator:
os.environ[lc_ctype_env] = original_lctype


@contextlib.contextmanager
Copy link
Member

Choose a reason for hiding this comment

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

This could probably even go in tests/conftest.py to be used everywhere ? I don't know how frequent it is. But we might want to do that in another MR, this one need to be merged πŸ˜„

@matusvalo
Copy link
Collaborator Author

matusvalo commented Jan 29, 2022

@matusvalo Does this pass for you locally?

Yes and no. the tests are failing only when executed by pytest tests as mentioned before. When you execute the file directly (pytest tests/test_self.py), it is working fine.

The reason is because pytest in case of executing tests via command pytest tests is adding to the sys.path also /Users/matus/dev/pylint/tests/regrtest_data path. Having this path causes that here:
https://github.com/PyCQA/pylint/blob/40ef1bebe9ee47ef0beef9b3ec53628d954710fa/pylint/lint/expand_modules.py#L65-L70
exception is not raised and modname is used not from exception handler but directly from return value of function. This is causing that pylint is not raising error for TestRunTC.test_regression_recursive_current_dir and hence failing the unittest.

But as said before only in case of running pytest tests. I have pushed the possible fix in last commit.

Note: I am not sure whether the behaviour causing failing tests is expected or it is bug in Astroid. But I think it is not connected to this PR.

@matusvalo
Copy link
Collaborator Author

I have merged main to fix conflict. @DanielNoord is there anything pending in this PR?

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.

Sorry @matusvalo, was swarmed at work and didn't really have the time to look into this.

One final question (and some spelling fixes). πŸ˜„

doc/user_guide/run.rst Outdated Show resolved Hide resolved
pylint/lint/pylinter.py Show resolved Hide resolved
tests/test_self.py Outdated Show resolved Hide resolved
tests/test_self.py Outdated Show resolved Hide resolved
Co-authored-by: DaniΓ«l van Noord <13665637+DanielNoord@users.noreply.github.com>
@matusvalo
Copy link
Collaborator Author

All suggestions resolved. Only point is just Iterator vs. Generator question. Let me know about it.

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Daniel spotted these errors but was working just a bit quickly when suggesting the fix :-)

tests/test_self.py Outdated Show resolved Hide resolved
tests/test_self.py Outdated Show resolved Hide resolved
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@matusvalo
Copy link
Collaborator Author

Thank you @jacobtylerwalls !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 2c6eadc into pylint-dev:main Feb 6, 2022
@Pierre-Sassoulas
Copy link
Member

This is a huge improvement to pylint, thank you @matusvalo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command line Related to command line interface Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lint all files in a directory
5 participants