8000 Add special support for `@django.cached_property` needed in `django-s… · python/mypy@a3aac71 · GitHub
[go: up one dir, main page]

Skip to content

Commit a3aac71

Browse files
authored
Add special support for @django.cached_property needed in django-stubs (#18959)
Hi! We, in `django-stubs`, have a lot of usages of `@cached_property` decorator that is a part of `django`: https://docs.djangoproject.com/en/5.2/ref/utils/#django.utils.functional.cached_property All usages of it we have to add to `subtest/allowlist.txt`, which is not great. In typing we reuse `@functools.cached_property` to have all the benefits of its inference: https://github.com/typeddjango/django-stubs/blob/ee8e8b11c37866969ff0406be20591a067dfa983/django-stubs/utils/functional.pyi#L3-L4 But, `stubtest` is not happy with this move: because in runtime objects have `django.utils.functional.cached_property` type and we see the following error: ``` error: django.http.response.HttpResponse.text is inconsistent, cannot reconcile @Property on stub with runtime object Stub: in file /home/runner/work/django-stubs/django-stubs/django-stubs/http/response.pyi:106 def (self: django.http.response.HttpResponse) -> builtins.str Runtime: <django.utils.functional.cached_property object at 0x7f7ce7704920> ``` So, we add all `@django.utils.functional.cached_property` usages to our `allowlist.txt`. There are LOTS of entries there: https://github.com/typeddjango/django-stubs/blob/ee8e8b11c37866969ff0406be20591a067dfa983/scripts/stubtest/allowlist.txt#L158-L425 Moreover, we have to always tell about this problem to new contributors on review :( That's why I propose to special case this as we do with other `property`-likes. I've tested locally and it works perfectly. I don't want to complicate the CI with `django` installation and special tests. So, I added `# pragma: no cover` to indicate that it is not tested.
1 parent 501a07b commit a3aac71

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

mypy/stubtest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,19 @@ def verify_paramspecexpr(
12561256
return
12571257

12581258

1259+
def _is_django_cached_property(runtime: Any) -> bool: # pragma: no cover
1260+
# This is a special case for
1261+
# https://docs.djangoproject.com/en/5.2/ref/utils/#django.utils.functional.cached_property
1262+
# This is needed in `django-stubs` project:
1263+
# https://github.com/typeddjango/django-stubs
1264+
if type(runtime).__name__ != "cached_property":
1265+
return False
1266+
try:
1267+
return bool(runtime.func)
1268+
except Exception:
1269+
return False
1270+< 8000 /span>
1271+
12591272
def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
12601273
assert stub.func.is_property
12611274
if isinstance(runtime, property):
@@ -1264,6 +1277,9 @@ def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[s
12641277
if isinstance(runtime, functools.cached_property):
12651278
yield from _verify_final_method(stub.func, runtime.func, MISSING)
12661279
return
1280+
if _is_django_cached_property(runtime):
1281+
yield from _verify_final_method(stub.func, runtime.func, MISSING)
1282+
return
12671283
if inspect.isdatadescriptor(runtime):
12681284
# It's enough like a property...
12691285
return

0 commit comments

Comments
 (0)
0