-
-
Notifications
You must be signed in to change notification settings - Fork 460
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
Resolve configured AUTH_USER_MODEL
with a get_type_analyze_hook
#2335
Merged
flaeppe
merged 8 commits into
typeddjango:master
from
flaeppe:fix/contrib-auth-authenticate
Aug 12, 2024
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab51f15
Return configured auth user in `django.contrib.auth.authenticate`
flaeppe beae9f5
fixup! Return configured auth user in `django.contrib.auth.authenticate`
flaeppe deeba94
fixup! fixup! Return configured auth user in `django.contrib.auth.aut…
flaeppe ed300fd
Resolve configured `AUTH_USER_MODEL` with a `get_type_analyze_hook`
flaeppe 9c6348c
fixup! Resolve configured `AUTH_USER_MODEL` with a `get_type_analyze_…
flaeppe 87b8b69
fixup! fixup! Resolve configured `AUTH_USER_MODEL` with a `get_type_a…
flaeppe a926dc6
fixup! fixup! fixup! Resolve configured `AUTH_USER_MODEL` with a `get…
flaeppe 4007c73
fixup! fixup! fixup! fixup! Resolve configured `AUTH_USER_MODEL` with…
flaeppe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from mypy.plugin import FunctionContext | ||
from mypy.types import Instance, NoneType, UnionType, get_proper_type | ||
from mypy.types import Type as MypyType | ||
from mypy.typevars import fill_typevars_with_any | ||
|
||
from mypy_django_plugin.django.context import DjangoContext | ||
from mypy_django_plugin.lib import helpers | ||
|
||
|
||
def update_authenticate_hook(ctx: FunctionContext, django_context: DjangoContext) -> MypyType: | ||
if not django_context.is_contrib_auth_installed: | ||
return ctx.default_return_type | ||
|
||
auth_user_model = django_context.settings.AUTH_USER_MODEL | ||
api = helpers.get_typechecker_api(ctx) | ||
model_info = helpers.resolve_lazy_reference( | ||
auth_user_model, api=api, django_context=django_context, ctx=ctx.context | ||
) | ||
if model_info is None: | ||
return ctx.default_return_type | ||
|
||
optional_model = UnionType([fill_typevars_with_any(model_info), NoneType()], ctx.context.line, ctx.context.column) | ||
default_return_type = get_proper_type(ctx.default_return_type) | ||
if isinstance(default_return_type, Instance) and default_return_type.type.fullname == "typing.Coroutine": | ||
if len(default_return_type.args) == 3: | ||
return default_return_type.copy_modified( | ||
args=[default_return_type.args[0], default_return_type.args[1], optional_model] | ||
) | ||
return ctx.default_return_type | ||
return optional_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
- case: test_authenticate_returns_configured_auth_user_model | ||
main: | | ||
from django.contrib.auth import authenticate, aauthenticate | ||
reveal_type(authenticate()) # N: Revealed type is "Union[myapp.models.MyUser, None]" | ||
async def f() -> None: | ||
reveal_type(await aauthenticate()) # N: Revealed type is "Union[myapp.models.MyUser, None]" | ||
custom_settings: | | ||
INSTALLED_APPS = ("django.contrib.contenttypes", "django.contrib.auth", "myapp") | ||
AUTH_USER_MODEL = "myapp.MyUser" | ||
files: | ||
- path: myapp/__init__.py | ||
- path: myapp/models.py | ||
content: | | ||
from django.db import models | ||
|
||
class MyUser(models.Model): | ||
... | ||
|
||
- case: test_authenticate_returns_stubs_types_when_contrib_auth_is_not_installed | ||
main: | | ||
from django.contrib.auth import authenticate, aauthenticate | ||
reveal_type(authenticate()) # N: Revealed type is "Union[django.contrib.auth.base_user.AbstractBaseUser, None]" | ||
async def f() -> None: | ||
reveal_type(await aauthenticate()) # N: Revealed type is "Union[django.contrib.auth.base_user.AbstractBaseUser, None]" | ||
custom_settings: | | ||
INSTALLED_APPS = ("django.contrib.contenttypes", "myapp") | ||
files: | ||
- path: myapp/__init__.py | ||
- path: myapp/models.py | ||
content: | | ||
from django.db import models | ||
|
||
class MyUser(models.Model): | ||
... | ||
|
||
- case: test_authenticate_returns_builtin_auth_user_per_default | ||
main: | | ||
from django.contrib.auth import authenticate, aauthenticate | ||
reveal_type(authenticate()) # N: Revealed type is "Union[django.contrib.auth.models.User, None]" | ||
async def f() -> None: | ||
reveal_type(await aauthenticate()) # N: Revealed type is "Union[django.contrib.auth.models.User, None]" | ||
custom_settings: | | ||
INSTALLED_APPS = ("django.contrib.contenttypes", "django.contrib.auth", "myapp") | ||
files: | ||
- path: myapp/__init__.py | ||
- path: myapp/models.py | ||
content: | | ||
from django.db import models | ||
|
||
class MyUser(models.Model): | ||
... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we get "implicit" cache tainting on
django.contrib.auth
, and thusauthenticate
and friends ifAUTH_USER_MODEL
changes.There might be other modules we want to add some extra config data to. Not sure though