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 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
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
24 changes: 24 additions & 0 deletions tests/pyright/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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,
),
]