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

Use functools.cached_property on 3.8+ and for TYPE_CHECKING #1417

Merged
merged 8 commits into from
Mar 2, 2022

Conversation

DanielNoord
Copy link
Collaborator

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

This closes #1410. I guess the deprecation introduced in #1243 can wait a bit.

@cdce8p This is what I meant with redefining cachedproperty. I'm not sure if this is something you would want to do, but I tested and this does help mypy recognise the types of these properties correctly.

Type of Changes

Type
✨ New feature
🔨 Refactoring

Related Issue

@DanielNoord DanielNoord added the Maintenance Discussion or action around maintaining astroid or the dev workflow label Mar 1, 2022
@DanielNoord DanielNoord requested a review from cdce8p March 1, 2022 18:33
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.11.0 milestone Mar 1, 2022

After first usage, the <property_name> becomes part of the object's
__dict__. Doing:
cachedproperty = 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.

Not exactly what I had in mind. That assignment will make it more not less difficult to understand that cachedproperty does the same thing functools.cached_property does.

I would suggest the replacement in the modules where it's used instead but the other way round. That should also make deprecating cachedproperty much easier.

if sys.version_info >= (3, 8) or TYPE_CHECKING:
    from functools import cached_property
else:
    from astroid.decorators import cachedproperty as cached_property

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fine with me! Just wanted to show what I was thinking of.

I'll update the PR.

astroid/decorators.py Outdated Show resolved Hide resolved
@DanielNoord DanielNoord requested a review from cdce8p March 2, 2022 07:33
astroid/decorators.py Outdated Show resolved Hide resolved
ChangeLog Outdated Show resolved Hide resolved
Comment on lines 75 to 77
"The cachedproperty class is functionally the same as functools.cached_property "
"and will be removed after support for Python < 3.8 has been dropped. "
"Consider importing the functools variant on >= 3.8.",
Copy link
Member

Choose a reason for hiding this comment

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

I would actually suggest to remove it with astroid 3.0 (at least for Python 3.8+). We can define it conditionally after that point.

if sys.version_info < (3, 8):
    class cachedproperty:
        ...

My suggestion

cachedproperty has been deprecated and will be removed in astroid 3.0 for Python 3.8+.
Use functools.cached_property instead.

--
As a side note, personally I do consider cachedproperty to be internal. Thus the deprecation note is more for us than anyone else. It might even make sense to add a TODO along the lines: Remove completely when support for 3.7 is dropped.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't know if 3.0 would be 3.8+ so I didn't mention a specific version. I'll update.

@@ -80,6 +80,11 @@
from astroid import nodes
from astroid.nodes import LocalsDictNodeNG

if sys.version_info >= (3, 8) or TYPE_CHECKING:
from functools import cached_property # pylint: disable=ungrouped-imports
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from functools import cached_property # pylint: disable=ungrouped-imports
# pylint: disable-next=ungrouped-imports
from functools import cached_property

This looks like something we should fix in pylint -> ungrouped-imports 🤔

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 something weird is going on here..

@@ -38,6 +38,11 @@
else:
from typing_extensions import Literal

if sys.version_info >= (3, 8) or TYPE_CHECKING:
from functools import cached_property # pylint: disable=ungrouped-imports
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from functools import cached_property # pylint: disable=ungrouped-imports
# pylint: disable-next=ungrouped-imports
from functools import cached_property

Comment on lines +40 to +43
if sys.version_info >= (3, 8) or TYPE_CHECKING:
from functools import cached_property
else:
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.

Strange that pylint: disable-next=ungrouped-imports is needed in some cases but not all 🤔

@@ -97,3 +99,18 @@ def test_deprecated_default_argument_values_ok(recwarn: WarningsRecorder) -> Non
instance = SomeClass(name="some_name")
instance.func(name="", var=42)
assert len(recwarn) == 0


@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python 3.8 or higher")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python 3.8 or higher")
@pytest.mark.skipif(not PY38_PLUS, reason="Requires Python 3.8 or higher")

For tests it isn't necessary to use sys.version_info (for typing).

@DanielNoord DanielNoord requested a review from cdce8p March 2, 2022 11:19
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.

Looks good! Thanks @DanielNoord 🐬

ChangeLog Show resolved Hide resolved
@cdce8p cdce8p merged commit 80b67a9 into pylint-dev:main Mar 2, 2022
@DanielNoord DanielNoord deleted the cachedproperty branch March 3, 2022 07:29
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 astroid or the dev workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace cachedproperty with functools.cached_property (>= 3.8)
3 participants