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

User.objects is UserManager[Self] #177

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if sys.version_info < (3, 8):
else:
from typing import Literal

if sys.version_info < (3, 11):
from typing_extensions import Self
else:
from typing import Self

_AnyUser = Union[Model, "AnonymousUser"]

def update_last_login(
Expand Down Expand Up @@ -104,7 +109,7 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
) -> None: ...

class User(AbstractUser):
objects: UserManager[User]
objects: UserManager[Self]

class AnonymousUser:
id: Any = ...
Expand Down
5 changes: 4 additions & 1 deletion django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class Model(metaclass=ModelBase):
self, using: Any = ..., keep_parents: bool = ...
) -> Tuple[int, Dict[str, int]]: ...
def full_clean(
self, exclude: Optional[Collection[str]] = ..., validate_unique: bool = True, validate_constraints: bool = True
self,
exclude: Optional[Collection[str]] = ...,
validate_unique: bool = True,
validate_constraints: bool = True,
) -> None: ...
def clean(self) -> None: ...
def clean_fields(self, exclude: Optional[Collection[str]] = ...) -> None: ...
Expand Down
8 changes: 6 additions & 2 deletions django-stubs/db/transaction.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ _C = TypeVar("_C", bound=Callable[..., Any])
class Atomic:
using: Optional[str] = ...
savepoint: bool = ...
def __init__(self, using: Optional[str], savepoint: bool, durable: bool = ...) -> None: ...
def __init__(
self, using: Optional[str], savepoint: bool, durable: bool = ...
) -> None: ...
# When decorating, return the decorated function as-is, rather than clobbering it as ContextDecorator does.
def __call__(self, func: _C) -> _C: ...
def __enter__(self) -> None: ...
Expand All @@ -40,7 +42,9 @@ def atomic(using: _C) -> _C: ...

# Decorator or context-manager with parameters
@overload
def atomic(using: Optional[str] = ..., savepoint: bool = ..., durable: bool = ...) -> Atomic: ...
def atomic(
using: Optional[str] = ..., savepoint: bool = ..., durable: bool = ...
) -> Atomic: ...

# Bare decorator
@overload
Expand Down
25 changes: 25 additions & 0 deletions tests/pyright/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .base import Result, run_pyright


def test_user_manager_specialises_to_self() -> None:
results = run_pyright(
"""\
from django.db import models
from django.contrib.auth import models as auth_models

class MyUser(auth_models.User):
age = models.IntegerField()

u = MyUser()
reveal_type(u.objects)
"""
)

assert results == [
Result(
type="information",
message='Type of "u.objects" is "UserManager[MyUser]"',
line=8,
column=13,
),
]