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

Make SomeModel._default_manager return a BaseManager[SomeModel] instead of BaseManager[Model] #817

Merged
merged 6 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion django-stubs/core/servers/basehttp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ServerHandler(simple_server.ServerHandler):

class WSGIRequestHandler(simple_server.WSGIRequestHandler):
close_connection: bool
connection: WSGIRequest # type: ignore[assignment]
connection: WSGIRequest
request: WSGIRequest
rfile: BytesIO
wfile: BytesIO
Expand Down
9 changes: 7 additions & 2 deletions django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.core.exceptions import MultipleObjectsReturned as BaseMultipleObject
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models.manager import BaseManager
from django.db.models.options import Options
from django.utils.functional import classproperty

_Self = TypeVar("_Self", bound="Model")

Expand All @@ -22,8 +23,12 @@ class Model(metaclass=ModelBase):
class MultipleObjectsReturned(BaseMultipleObjectsReturned): ...
class Meta: ...
_meta: Options[Any]
_default_manager: BaseManager[Model]
_base_manager: BaseManager[Model]
@classproperty
@classmethod
def _default_manager(cls: Type[_Self]) -> BaseManager[_Self]: ...
@classproperty
@classmethod
def _base_manager(cls: Type[_Self]) -> BaseManager[_Self]: ...
objects: BaseManager[Any]
pk: Any = ...
_state: ModelState
Expand Down
13 changes: 8 additions & 5 deletions django-stubs/utils/functional.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ def partition(
predicate: Callable, values: List[_PartitionMember]
) -> Tuple[List[_PartitionMember], List[_PartitionMember]]: ...

class classproperty:
fget: Optional[Callable] = ...
def __init__(self, method: Optional[Callable] = ...) -> None: ...
def __get__(self, instance: Any, cls: Optional[type] = ...) -> Any: ...
def getter(self, method: Callable) -> classproperty: ...
_Get = TypeVar("_Get")
_Self = TypeVar("_Self")

class classproperty(Generic[_Get]):
fget: Optional[Callable[[Type[_Self]], _Get]] = ...
def __init__(self, method: Optional[Callable[[Type[_Self]], _Get]] = ...) -> None: ...
def __get__(self, instance: Any, cls: Type[_Self] = ...) -> _Get: ...
Copy link
Member

Choose a reason for hiding this comment

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

From what I see __get__ is usually an overload of two functions.
You can take a look here: https://github.com/python/typeshed/

def getter(self, method: Callable[[Type[_Self]], _Get]) -> classproperty[_Get]: ...
2 changes: 1 addition & 1 deletion tests/typecheck/managers/test_managers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
class Base(Generic[_T]):
def __init__(self, model_cls: Type[_T]):
self.model_cls = model_cls
reveal_type(self.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.BaseManager[django.db.models.base.Model]"
reveal_type(self.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.BaseManager[_T`1]"
Copy link
Member

Choose a reason for hiding this comment

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

Let's also test ._base_manager of some Model, so we can ensure that _T is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm super unfamiliar with these types of tests. I hope the new test makes sense!

class MyModel(models.Model):
pass
class Child(Base[MyModel]):
Expand Down