-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bmtcril/dockerhub_login
- Loading branch information
Showing
15 changed files
with
130 additions
and
39 deletions.
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
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,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 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 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 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
Oops, something went wrong.