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

Reuse functools.cached_property definition instead of defining our own #1771

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 3 additions & 10 deletions django-stubs/utils/functional.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
from collections.abc import Callable, Sequence

# Mypy has special handling for functools.cached_property, reuse typeshed's definition instead of defining our own
from functools import cached_property as cached_property
from typing import Any, Generic, Protocol, SupportsIndex, TypeVar, overload

from django.db.models.base import Model
from typing_extensions import Self, TypeAlias

_T = TypeVar("_T")

class cached_property(Generic[_T]):
func: Callable[[Any], _T]
name: str | None
def __init__(self, func: Callable[[Any], _T], name: str | None = ...) -> None: ...
@overload
def __get__(self, instance: None, cls: type[Any] | None = ...) -> Self: ...
@overload
def __get__(self, instance: object, cls: type[Any] | None = ...) -> _T: ...
def __set_name__(self, owner: type[Any], name: str) -> None: ...

# Promise is only subclassed by a proxy class defined in the lazy function
# so it makes sense for it to have all the methods available in that proxy class
class Promise:
Expand Down
43 changes: 43 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,46 @@ django.middleware.csrf.REASON_BAD_TOKEN

# RemovedInDjango41
django.core.cache.backends.memcached.MemcachedCache

# We re-export `functools.cached_property` which has different semantics
django.utils.functional.cached_property.__class_getitem__
django.utils.functional.cached_property.__init__
django.utils.functional.cached_property.__set__
django.utils.functional.cached_property.name

# Ignore @cached_property error "cannot reconcile @property on stub with runtime object"
django.db.migrations.RenameField.new_name_lower
django.db.migrations.RenameField.old_name_lower
django.db.migrations.RenameIndex.new_name_lower
django.db.migrations.RenameIndex.old_name_lower
django.db.migrations.RenameModel.new_name_lower
django.db.migrations.RenameModel.old_name_lower
django.db.migrations.operations.RenameField.new_name_lower
django.db.migrations.operations.RenameField.old_name_lower
django.db.migrations.operations.RenameIndex.new_name_lower
django.db.migrations.operations.RenameIndex.old_name_lower
django.db.migrations.operations.RenameModel.new_name_lower
django.db.migrations.operations.RenameModel.old_name_lower
django.db.migrations.operations.fields.FieldOperation.model_name_lower
django.db.migrations.operations.fields.FieldOperation.name_lower
django.db.migrations.operations.fields.RenameField.new_name_lower
django.db.migrations.operations.fields.RenameField.old_name_lower
django.db.migrations.operations.models.AlterTogetherOptionOperation.option_value
django.db.migrations.operations.models.IndexOperation.model_name_lower
django.db.migrations.operations.models.ModelOperation.name_lower
django.db.migrations.operations.models.RenameIndex.new_name_lower
django.db.migrations.operations.models.RenameIndex.old_name_lower
django.db.migrations.operations.models.RenameModel.new_name_lower
django.db.migrations.operations.models.RenameModel.old_name_lower
django.db.migrations.state.ModelState.name_lower
django.db.migrations.state.ProjectState.apps
django.middleware.csrf.CsrfViewMiddleware.allowed_origin_subdomains
django.middleware.csrf.CsrfViewMiddleware.allowed_origins_exact
django.middleware.csrf.CsrfViewMiddleware.csrf_trusted_origins_hosts
django.urls.URLPattern.lookup_str
django.urls.URLResolver.url_patterns
django.urls.URLResolver.urlconf_module
django.urls.resolvers.URLPattern.lookup_str
django.urls.resolvers.URLResolver.url_patterns
django.urls.resolvers.URLResolver.urlconf_module
django.utils.connection.BaseConnectionHandler.settings
24 changes: 16 additions & 8 deletions tests/typecheck/utils/test_functional.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
- case: cached_property_class_vs_instance_attributes
main: |
from django.utils.functional import cached_property
from typing import List
from typing import List, ClassVar

class Foo:
@cached_property
def attr(self) -> List[str]: ...
@cached_property # E: Argument 1 to "cached_property" has incompatible type "Callable[[Foo, str], List[str]]"; expected "Callable[[Any], List[str]]" [arg-type]

@cached_property # E: Too many arguments for property [misc]
def attr2(self, arg2: str) -> List[str]: ...

reveal_type(attr) # N: Revealed type is "django.utils.functional.cached_property[builtins.list[builtins.str]]"
reveal_type(attr.name) # N: Revealed type is "Union[builtins.str, None]"
reveal_type(attr) # N: Revealed type is "def (self: main.Foo) -> builtins.list[builtins.str]"

reveal_type(Foo.attr) # N: Revealed type is "django.utils.functional.cached_property[builtins.list[builtins.str]]"
reveal_type(Foo.attr.func) # N: Revealed type is "def (Any) -> builtins.list[builtins.str]"
reveal_type(Foo.attr) # N: Revealed type is "def (self: main.Foo) -> builtins.list[builtins.str]"

f = Foo()
reveal_type(f.attr) # N: Revealed type is "builtins.list[builtins.str]"
f.attr.name # E: "List[str]" has no attribute "name" [attr-defined]
reveal_type(f.attr) # N: Revealed type is "builtins.list[builtins.str]"
f.attr.func # E: "List[str]" has no attribute "func" [attr-defined]

# May be overridden by @property
class Bar(Foo):
@property
def attr(self) -> List[str]: ...

# May be overridden by ClassVar
class Quux(Foo):
attr: ClassVar[List[str]] = []
intgr marked this conversation as resolved.
Show resolved Hide resolved

- case: str_promise_proxy
main: |
Expand Down