-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Upgrade django-stubs again, unpin mypy, and add more annotations #36072
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
Merged
kdmccormick
merged 3 commits into
openedx:master
from
kdmccormick:kdmccormick/unpin-mypy
Jan 9, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
| """ | ||
| Typing utilities for the HTTP requests, responses, etc. | ||
|
|
||
| Includes utilties to work with both vanilla django as well as djangorestframework. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import django.contrib.auth.models # pylint: disable=imported-auth-user | ||
| import django.http | ||
| import rest_framework.request | ||
|
|
||
| import openedx.core.types.user | ||
| from openedx.core.types.meta import type_annotation_only | ||
|
|
||
|
|
||
| @type_annotation_only | ||
| class HttpRequest(django.http.HttpRequest): | ||
| """ | ||
| A request which either has a concrete User (from django.contrib.auth) or is anonymous. | ||
| """ | ||
| user: openedx.core.types.User | ||
|
|
||
|
|
||
| @type_annotation_only | ||
| class AuthenticatedHttpRequest(HttpRequest): | ||
| """ | ||
| A request which is guaranteed to have a concrete User (from django.contrib.auth). | ||
| """ | ||
| user: django.contrib.auth.models.User | ||
|
|
||
|
|
||
| @type_annotation_only | ||
| class RestRequest(rest_framework.request.Request): | ||
| """ | ||
| Same as HttpRequest, but extended for rest_framework views. | ||
| """ | ||
| user: openedx.core.types.User | ||
|
|
||
|
|
||
| @type_annotation_only | ||
| class AuthenticatedRestRequest(RestRequest): | ||
| """ | ||
| Same as AuthenticatedHttpRequest, but extended for rest_framework views. | ||
| """ | ||
| user: django.contrib.auth.models.User |
This file contains hidden or 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,37 @@ | ||
| """ | ||
| Typing utilities for use on other typing utilities. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import typing as t | ||
|
|
||
|
|
||
| def type_annotation_only(cls: type) -> type: | ||
| """ | ||
| Decorates class which should only be used in type annotations. | ||
|
|
||
| This is useful when you want to enhance an existing 3rd-party concrete class with | ||
| type annotations for its members, but don't want the enhanced class to ever actually | ||
| be instantiated. For examples, see openedx.core.types.http. | ||
| """ | ||
| if t.TYPE_CHECKING: | ||
| return cls | ||
| return _forbid_init(cls) | ||
|
|
||
|
|
||
| def _forbid_init(forbidden: type) -> type: | ||
| """ | ||
| Return a class which refuses to be instantiated. | ||
| """ | ||
| class _ForbidInit: | ||
| """ | ||
| The resulting class. | ||
| """ | ||
| def __init__(self, *args, **kwargs): | ||
| raise NotImplementedError( | ||
| f"Class {forbidden.__module__}:{forbidden.__name__} " | ||
| "cannot be instantiated. You may use it as a type annotation, but objects " | ||
| "can only be created from its concrete superclasses." | ||
| ) | ||
|
|
||
| return _ForbidInit | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| """ | ||
| Typing utilities for the User models. | ||
| """ | ||
| from typing import Union | ||
| from __future__ import annotations | ||
|
|
||
| import typing as t | ||
|
|
||
| import django.contrib.auth.models | ||
|
|
||
| User = Union[django.contrib.auth.models.User, django.contrib.auth.models.AnonymousUser] | ||
| User: t.TypeAlias = django.contrib.auth.models.User | django.contrib.auth.models.AnonymousUser |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.