Skip to content

Use functools.cached_property on 3.8+ #5907

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

Merged
merged 1 commit into from
Mar 12, 2022

Conversation

DanielNoord
Copy link
Collaborator

  • 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

Last PR to fix all deprecations as a result of astroid upgrade πŸŽ‰

@DanielNoord DanielNoord added the Maintenance Discussion or action around maintaining pylint or the dev workflow label Mar 12, 2022
@coveralls
Copy link

Pull Request Test Coverage Report for Build 1974339656

  • 30 of 30 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.007%) to 94.051%

Totals Coverage Status
Change from base Build 1974330895: 0.007%
Covered Lines: 15066
Relevant Lines: 16019

πŸ’› - 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.

πŸ‘Œ

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Mar 12, 2022
@Pierre-Sassoulas Pierre-Sassoulas merged commit 9d46885 into pylint-dev:main Mar 12, 2022
@DanielNoord DanielNoord deleted the cachedproperty branch March 12, 2022 22:05
Comment on lines +30 to +35
if sys.version_info >= (3, 8) or TYPE_CHECKING:
# pylint: disable-next=ungrouped-imports
from functools import cached_property
else:
# pylint: disable-next=ungrouped-imports
from astroid.decorators import cachedproperty as cached_property
Copy link
Member

Choose a reason for hiding this comment

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

I thought about this some more, especially since I wanted to see what it would take to resolve / hide the pylint error.

First, the reason it's emitted is that we don't check BoolOp nodes (in is_node_in_guarded_import_block) when we determine if ungrouped-imports should be emitted.
https://github.com/PyCQA/pylint/blob/dee07af04df93c4b5edeb2492eaa5c46ebcdb7b7/pylint/checkers/imports.py#L548-L550

The question then becomes if we should do that. To answer that it's worth taking a look at the condition itself and how cached_property is defined in typeshed. Basically typeshed also uses version guards, in particular if sys.version_info >= (3, 8). That means the or TYPE_CHECKING I suggested in astroid is basically useless. If we run mypy with --python-version 3.8 or above, the sys.version_info check is enough, whereas with 3.7 we just get an attr-defined error anyway.

Module "functools" has no attribute "cached_property"  [attr-defined]

In the end or TYPE_CHECKING is useless and causes the unnecessary errors. So it might be best / easiest if we remove them.

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.

5 participants