Skip to content

Add special support for @django.cached_property needed in django-stubs #18959

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,19 @@ def verify_paramspecexpr(
return


def _is_django_cached_property(runtime: Any) -> bool: # pragma: no cover
# This is a special case for
# https://docs.djangoproject.com/en/5.2/ref/utils/#django.utils.functional.cached_property
# This is needed in `django-stubs` project:
# https://github.com/typeddjango/django-stubs
if repr(type(runtime)) != "<class 'django.utils.functional.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.

What do you think of using type(runtime).__name__ == "cached_property" as a heuristic? That way we're not as specific to one user.

Also out of curiosity, why can't django use @functools.cached_property directly?

return False
try:
return bool(runtime.func)
except Exception:
return False


def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
assert stub.func.is_property
if isinstance(runtime, property):
Expand All @@ -1264,6 +1277,9 @@ def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[s
if isinstance(runtime, functools.cached_property):
yield from _verify_final_method(stub.func, runtime.func, MISSING)
return
if _is_django_cached_property(runtime):
yield from _verify_final_method(stub.func, runtime.func, MISSING)
return
if inspect.isdatadescriptor(runtime):
# It's enough like a property...
return
Expand Down