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

Add checkers for typing.final for Python version 3.8 or later #5133

Merged
merged 6 commits into from
Oct 10, 2021

Conversation

mbyrnepr2
Copy link
Member

  • overridden-final-method
  • subclassed-final-class

Closes #3197

Type of Changes

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

Description

Add two new checkers Python versions of 3.8 and up:

  • overridden-final-method
  • subclassed-final-class

Closes #3197

- overridden-final-method
- subclassed-final-class

Closes pylint-dev#3197
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.

LGTM ! Thank you. I only have a minor remark regarding where to add the changes.

@@ -874,10 +885,11 @@ def _check_consistent_mro(self, node):
# Old style class, there's no mro so don't do anything.
pass

def _check_proper_bases(self, node):
def _check_proper_bases(self, node: nodes.ClassDef) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for adding the typing here. Could we create another function _check_typing_final instead, please ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely, this will also fix the failing test because of the lambda case. I'll put this back to the way it was and move it to its own thing.

I have one doubt aside from this - Is it best to include the changes mentioned in the issue for typing.Final in this merge-request, or would it be preferable to create a separate merge-request for that?

- Move checker logic to its own method
- tuple -> list to be consistent with the existing module style
- Doc formatting tweak

    Closes pylint-dev#3197
@coveralls
Copy link

coveralls commented Oct 9, 2021

Pull Request Test Coverage Report for Build 1325185515

  • 13 of 13 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.006%) to 93.205%

Totals Coverage Status
Change from base Build 1323882189: 0.006%
Covered Lines: 13552
Relevant Lines: 14540

πŸ’› - Coveralls

@mbyrnepr2 mbyrnepr2 marked this pull request as ready for review October 9, 2021 16:33
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.

Very nice addition to the code base, thank you. This will be available in 2.12 :)

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Oct 10, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.12.0 milestone Oct 10, 2021
@Pierre-Sassoulas Pierre-Sassoulas merged commit 087fe68 into pylint-dev:main Oct 10, 2021
@@ -898,6 +910,23 @@ def _check_proper_bases(self, node):
"useless-object-inheritance", args=node.name, node=node
)

def _check_typing_final(self, node: nodes.ClassDef) -> None:
"""Detect that a class does not subclass a class decorated with `typing.final`"""
if not PY38_PLUS:
Copy link
Collaborator

@DanielNoord DanielNoord Oct 10, 2021

Choose a reason for hiding this comment

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

@Pierre-Sassoulas Sorry for responding to this after this has been merged, but:

Tell me if I'm wrong, but I don't think this is how this check is intended right? Taking the consider-f-string example: if the python version is < 3.8 it shouldn't suggest to use an f-string as they are not available. The suggestion that the checker provides is nonsensical for 3.6 or 3.7.
This checker on the other hand should be used irrespective of the python: whenever somebody uses Final it should never be overwritten. There is room for a using-final-in-unsupported-version checker, which could check if Final is being used while the version is set to 3.6 or 3.7 but I don't think this checker should be stopped in that case.
Put differently, this line says that on 3.6 or 3.7 Final can be overwritten (which is not true) instead of warning that Final is being used in an unsupported version (which could be a checker of its own).
Please let me know if I misunderstood this setting, but if not I'm happy to provide a PR to remove these two lines.

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 there's no messages that can be emitted and we don't do any check for python < 3.8 (we exit before checking decorated_with(ancestor, typing.final"]) and just return for 3.6 and 3.7. So I don't think we have a false positive, but you're right that it would be better to check anyway and warn for using-final-in-unsupported-version. II don't know how often this mistake happen but I'd merge a check for that, as python 3.7 will be available for some time.

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

Successfully merging this pull request may close these issues.

Add support for typing.final
4 participants