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

[stubgen] Add @functools.cached_property support #14981

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ def process_name_expr_decorator(self, expr: NameExpr, context: Decorator) -> tup
self.add_decorator("property")
self.add_decorator("abc.abstractmethod")
is_abstract = True
elif self.refers_to_fullname(name, "functools.cached_property"):
self.import_tracker.require_name(name)
self.add_decorator(name)
elif self.refers_to_fullname(name, OVERLOAD_NAMES):
self.add_decorator(name)
self.add_typing_import("overload")
Expand Down Expand Up @@ -894,6 +897,14 @@ def process_member_expr_decorator(
self.import_tracker.require_name(expr.expr.name)
self.add_decorator(f"{expr.expr.name}.{expr.name}")
is_abstract = True
elif expr.name == "cached_property" and isinstance(expr.expr, NameExpr):
explicit_name = expr.expr.name
reverse = self.import_tracker.reverse_alias.get(explicit_name)
if reverse == "functools" or (reverse is None and explicit_name == "functools"):
if reverse is not None:
self.import_tracker.add_import(reverse, alias=explicit_name)
self.import_tracker.require_name(explicit_name)
self.add_decorator(f"{explicit_name}.{expr.name}")
elif expr.name == "coroutine":
if (
isinstance(expr.expr, MemberExpr)
Expand Down
56 changes: 56 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,62 @@ class A:
def f(self, x) -> None: ...
def h(self) -> None: ...

[case testFunctoolsCachedProperty]
import functools

class A:
@functools.cached_property
def x(self):
return 'x'
[out]
import functools

class A:
@functools.cached_property
def x(self): ...

[case testFunctoolsCachedPropertyAlias]
import functools as ft

class A:
@ft.cached_property
def x(self):
return 'x'
[out]
import functools as ft

class A:
@ft.cached_property
def x(self): ...

[case testCachedProperty]
from functools import cached_property

class A:
@cached_property
def x(self):
return 'x'
[out]
from functools import cached_property

class A:
@cached_property
def x(self): ...

[case testCachedPropertyAlias]
from functools import cached_property as cp

class A:
@cp
def x(self):
return 'x'
[out]
from functools import cached_property as cp

class A:
@cp
def x(self): ...

[case testStaticMethod]
class A:
@staticmethod
Expand Down