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

Refactor various typing related issues #4940

Merged
merged 21 commits into from Sep 3, 2021

Conversation

DanielNoord
Copy link
Collaborator

@DanielNoord DanielNoord commented Aug 31, 2021

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Write a good description on what the PR does.

Type of Changes

Type
🔨 Refactoring

Description

This PR has two related aims:

  1. Add typing to all visit_... and leave_... calls in the codebase
  2. Fix some outstanding mypy issues
    As a result of doing the former the number of mypy issues increased so it seemed logical to take another look at some of the open issues (based on comments meant to ignore mypy). I did not add a changelog contribution.

Before merging there are a small number of calls for which I did not find documentation and therefore I could not add the type annotations. I think it would be best to add those before merging this, to make sure this PR catches all calls.
The missing annotations are for:
visit_exec
visit_default
visit_print
visit_repr
visit_package
visit_instance
visit_project
Does any of the maintainers know the nodes passed into these calls? And if so, can you give me a list or add them yourself?

This adds typing to most calls that visit nodes. All other changes are
due to mypy errors resulting from introduction of typing.
This removes some of the `type: ignore` comments in favour of
solving the mypy issues these comments were surpressing.
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.

Amazing work, there are a lot of removed type ignore ! Maybe we can even add the mypy option for unnecessary type ignore now ? Regarding the missing type I need to check on a computer later.

pylint/checkers/typecheck.py Outdated Show resolved Hide resolved
pylint/extensions/broad_try_clause.py Show resolved Hide resolved
pylint/checkers/stdlib.py Outdated Show resolved Hide resolved
Except for two references to node_classes in the changelog this should be the last of them
@DanielNoord
Copy link
Collaborator Author

I have updated the mypy configuration.

Outstanding issues:

  1. visit_print comes from Python3Checker. Is that checker even relevant after having dropped support for Python < 3.6? I saw a number of disabled messages in our own codebase from this checker which currently do nothing.
    Are we going to keep the checker in the code? And if so, should we remove disabled messages that do nothing currently?
  2. visit_repr, same as above.
  3. visit_package, docstring refers to astroid.Package node but I don't know what this is.
  4. visit_instance, this is a astroid.objects.ExceptionInstance. However, as astroid.objects is not imported into the main astroid file we can't access it before runtime and are therefore losing out on IntelliSense. Should we update astroid to include this in its main imports and therefore make it accessible to our typing?

@Pierre-Sassoulas
Copy link
Member

You're right about the python3 checker, I'm going to clean it up, if someone really need to port to python 3 in 2021 that someone can probably suffer to use an older pylint.

cdce8p
cdce8p previously requested changes Aug 31, 2021
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Awesome work you already did here @DanielNoord 🚀

A few points:

  • I don't really like the added pylint: disable=unused-argument comments. We should use _ instead.
  • I believe that in the strict mode for mypy even unused arguments should be typed. Those are by far most of my comments.
  • visit_print, visit_repr, and visit_exec should get called anymore. Those could be removed. I haven't looked at Remove the python3 porting mode from the codebase #4942 just yet, but will do so next.
  • We should probably avoid typing generic list, dict, ... . Better to use List[Any], ... instead.

doc/how_tos/custom_checkers.rst Outdated Show resolved Hide resolved
pylint/checkers/base.py Outdated Show resolved Hide resolved
pylint/checkers/base.py Outdated Show resolved Hide resolved
pylint/checkers/base.py Outdated Show resolved Hide resolved
pylint/checkers/base.py Outdated Show resolved Hide resolved
pylint/extensions/redefined_variable_type.py Outdated Show resolved Hide resolved
pylint/extensions/redefined_variable_type.py Outdated Show resolved Hide resolved
pylint/extensions/redefined_variable_type.py Outdated Show resolved Hide resolved
pylint/pyreverse/diadefslib.py Outdated Show resolved Hide resolved
pylint/pyreverse/inspector.py Outdated Show resolved Hide resolved
DanielNoord and others added 5 commits September 1, 2021 00:00
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
@DanielNoord
Copy link
Collaborator Author

DanielNoord commented Aug 31, 2021

Besides open questions from code review, following is needed for this to go forward:
1. Merge of #4942
2. Answer to: visit_instance, this is a astroid.objects.ExceptionInstance. However, as astroid.objects is not imported into the main astroid file we can't access it before runtime and are therefore losing out on IntelliSense. Should we update astroid to include this in its main imports and therefore make it accessible to our typing?
3. Typing for visit_package in pylint/pyreverse/inspector.py

@cdce8p cdce8p self-requested a review August 31, 2021 22:49
@coveralls
Copy link

coveralls commented Sep 1, 2021

Pull Request Test Coverage Report for Build 1193364184

  • 311 of 312 (99.68%) changed or added relevant lines in 40 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 93.098%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/pyreverse/inspector.py 26 27 96.3%
Totals Coverage Status
Change from base Build 1191709213: 0.01%
Covered Lines: 13233
Relevant Lines: 14214

💛 - 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.

Amazing work done here. I'm pretty sure being able to use proper typing will prevent crashes in the future.

doc/how_tos/custom_checkers.rst Outdated Show resolved Hide resolved
pylint/checkers/exceptions.py Outdated Show resolved Hide resolved
if is_method_call(func, types, methods) and not is_complex_format_str(
func.bound
if (
isinstance(func, astroid.BoundMethod)
Copy link
Member

Choose a reason for hiding this comment

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

Is this equivalent to what we had before ? Maybe this prevent crash we never saw yet ? Thank you, mypy ! 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it is actually redundant and is_method_call makes sure that func is a BoundMethod. However, this doesn't get recognised by mypy or any IDE so we need to add this redundant check here.
Perhaps removing the checks from the return statement in is_method_call and doing if ... return True might also work. That would remove a duplicate check. However, I am not sure.

@Pierre-Sassoulas
Copy link
Member

Regarding visit_package in pylint/pyreverse/inspector.py, this is never executed in the whole test suite, it miight be safe to remove the function (?)

@Pierre-Sassoulas Pierre-Sassoulas added the Maintenance Discussion or action around maintaining pylint or the dev workflow label Sep 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.11.x milestone Sep 1, 2021
DanielNoord and others added 2 commits September 1, 2021 09:37
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
doc/how_tos/custom_checkers.rst Show resolved Hide resolved
pylint/pyreverse/inspector.py Show resolved Hide resolved
pylint/checkers/typecheck.py Show resolved Hide resolved
pylint/checkers/utils.py Outdated Show resolved Hide resolved
@DanielNoord
Copy link
Collaborator Author

DanielNoord commented Sep 1, 2021

I have removed visit_package.

Left to-do:

  • Three open questions which have not been resolved.
  • Answer to: visit_instance, this is a astroid.objects.ExceptionInstance. However, as astroid.objects is not imported into the main astroid file we can't access it before runtime and are therefore losing out on IntelliSense. Should we update astroid to include this in its main imports and therefore make it accessible to our typing?

pylint/checkers/utils.py Show resolved Hide resolved
pylint/checkers/utils.py Show resolved Hide resolved
pylint/pyreverse/inspector.py Outdated Show resolved Hide resolved
DanielNoord added a commit to DanielNoord/astroid that referenced this pull request Sep 2, 2021
With typing added to parts of the code in ``pylint`` it has become useful 
to import this class so ``pylint`` can access it. See discussion in:
pylint-dev/pylint#4940
@DanielNoord
Copy link
Collaborator Author

I have opened pylint-dev/astroid#1160 so that the issue with the typing of visit_instance can be resolved.
After that merge all that's left is the outstanding question of Pierre and two from Marc, although I think those have been answered and can be resolved!
Rest is good to go I think!

@cdce8p
Copy link
Member

cdce8p commented Sep 2, 2021

I did take a look at

# old
ops = list(itertools.chain(*ops))

# new
iter_ops: Iterable[Any] = iter(ops)
ops = list(itertools.chain(*iter_ops))

Tbh I think this might be a mypy issue. We can probably leave the fix as you have it now.

--
Due to the large size of this PR, I was wondering if we should merge it sooner rather than later. Can the visit_instance issue be resolved in a followup as well? @Pierre-Sassoulas What do you think?

@cdce8p cdce8p mentioned this pull request Sep 2, 2021
4 tasks
@DanielNoord
Copy link
Collaborator Author

@Pierre-Sassoulas already approved my PR for astroid so visit_instance has been taken care of. I think we can merge this before astroid has been released as it does not affect functionality.

@Pierre-Sassoulas
Copy link
Member

Yeah, this is a huge change and need to be merged relatively quickly or we'll get conflict on a lot of other MR. May as well put it in 2.11.0 as I did not finish #4900 and we can't release it yet 😄

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.11.x, 2.11.0 Sep 2, 2021
@DanielNoord
Copy link
Collaborator Author

Is there anything blocking the merge of this PR? Or do you want to wait on the astroid PR?

@Pierre-Sassoulas Pierre-Sassoulas merged commit baaa81a into pylint-dev:main Sep 3, 2021
@DanielNoord DanielNoord deleted the typing branch September 3, 2021 11:49
Pierre-Sassoulas pushed a commit to pylint-dev/astroid that referenced this pull request Sep 4, 2021
With typing added to parts of the code in ``pylint`` it has become useful 
to import this class so ``pylint`` can access it. See discussion in:
pylint-dev/pylint#4940
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining pylint or the dev workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants