diff options
| author | CoprDistGit <infra@openeuler.org> | 2023-05-05 11:21:58 +0000 |
|---|---|---|
| committer | CoprDistGit <infra@openeuler.org> | 2023-05-05 11:21:58 +0000 |
| commit | 5499105013d837030ab4e8f08b8c9773debc9327 (patch) | |
| tree | 4abe0ed90e75643bcd1b4547f15c744237c03fad | |
| parent | 5cbdd72e74c898368c0b254a30558da08b1e2993 (diff) | |
automatic import of python-django-typesopeneuler20.03
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | python-django-types.spec | 807 | ||||
| -rw-r--r-- | sources | 1 |
3 files changed, 809 insertions, 0 deletions
@@ -0,0 +1 @@ +/django-types-0.17.0.tar.gz diff --git a/python-django-types.spec b/python-django-types.spec new file mode 100644 index 0000000..31ed27f --- /dev/null +++ b/python-django-types.spec @@ -0,0 +1,807 @@ +%global _empty_manifest_terminate_build 0 +Name: python-django-types +Version: 0.17.0 +Release: 1 +Summary: Type stubs for Django +License: MIT +URL: https://github.com/sbdchd/django-types +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/85/87/f0f4f8387aee5cc3ca738f75a0f4c648dd45a19607e8d2b8aa7b47d2dd7e/django-types-0.17.0.tar.gz +BuildArch: noarch + + +%description +# django-types [](https://pypi.org/project/django-types/) + +Type stubs for [Django](https://www.djangoproject.com). + +> Note: this project was forked from +> <https://github.com/typeddjango/django-stubs> with the goal of removing the +> [`mypy`](https://github.com/python/mypy) plugin dependency so that `mypy` +> can't [crash due to Django +> config](https://github.com/typeddjango/django-stubs/issues/318), and that +> non-`mypy` type checkers like +> [`pyright`](https://github.com/microsoft/pyright) will work better with +> Django. + +## install + +```bash +pip install django-types +``` + +You'll need to monkey patch Django's `QuerySet`, `Manager` (not needed for Django 3.1+) and +`ForeignKey` (not needed for Django 4.1+) classes so we can index into them with a generic +argument. Add this to your settings.py: + +```python +# in settings.py +from django.db.models import ForeignKey +from django.db.models.manager import BaseManager +from django.db.models.query import QuerySet + +# NOTE: there are probably other items you'll need to monkey patch depending on +# your version. +for cls in [QuerySet, BaseManager, ForeignKey]: + cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) # type: ignore [attr-defined] +``` + +## usage + +### ForeignKey ids and related names as properties in ORM models + +When defining a Django ORM model with a foreign key, like so: + +```python +class User(models.Model): + team = models.ForeignKey( + "Team", + null=True, + on_delete=models.SET_NULL, + ) + role = models.ForeignKey( + "Role", + null=True, + on_delete=models.SET_NULL, + related_name="users", + ) +``` + +two properties are created, `team` as expected, and `team_id`. Also, a related +manager called `user_set` is created on `Team` for the reverse access. + +In order to properly add typing to the foreign key itself and also for the created ids you can do +something like this: + +```python +from typing import TYPE_CHECKING + +from someapp.models import Team +if TYPE_CHECKING: + # In this example Role cannot be imported due to circular import issues, + # but doing so inside TYPE_CHECKING will make sure that the typing below + # knows what "Role" means + from anotherapp.models import Role + + +class User(models.Model): + team_id: Optional[int] + team = models.ForeignKey( + Team, + null=True, + on_delete=models.SET_NULL, + ) + role_id: int + role = models.ForeignKey["Role"]( + "Role", + null=False, + on_delete=models.SET_NULL, + related_name="users", + ) + + +reveal_type(User().team) +# note: Revealed type is 'Optional[Team]' +reveal_type(User().role) +# note: Revealed type is 'Role' +``` + +This will make sure that `team_id` and `role_id` can be accessed. Also, `team` and `role` +will be typed to their right objects. + +To be able to access the related manager `Team` and `Role` you could do: + +```python +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + # This doesn't really exists on django so it always need to be imported this way + from django.db.models.manager import RelatedManager + from user.models import User + + +class Team(models.Model): + if TYPE_CHECKING: + user_set = RelatedManager["User"]() + + +class Role(models.Model): + if TYPE_CHECKING: + users = RelatedManager["User"]() + +reveal_type(Team().user_set) +# note: Revealed type is 'RelatedManager[User]' +reveal_type(Role().users) +# note: Revealed type is 'RelatedManager[User]' +``` + +An alternative is using annotations: + + + +```python +from __future__ import annotations # or just be in python 3.11 + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from django.db.models import Manager + from user.models import User + + +class Team(models.Model): + user_set: Manager[User] + + +class Role(models.Model): + users: Manager[User] + +reveal_type(Team().user_set) +# note: Revealed type is 'Manager[User]' +reveal_type(Role().users) +# note: Revealed type is 'Manager[User]' +``` + + +### `id Field` + +By default Django will create an `AutoField` for you if one doesn't exist. + +For type checkers to know about the `id` field you'll need to declare the +field explicitly. + +```python +# before +class Post(models.Model): + ... + +# after +class Post(models.Model): + id = models.AutoField(primary_key=True) + # OR + id: int +``` + +### `HttpRequest`'s `user` property + +The `HttpRequest`'s `user` property has a type of `Union[AbstractBaseUser, AnonymousUser]`, +but for most of your views you'll probably want either an authed user or an +`AnonymousUser`. + +So we can define a subclass for each case: + +```python +class AuthedHttpRequest(HttpRequest): + user: User # type: ignore [assignment] +``` + +And then you can use it in your views: + +```python +@auth.login_required +def activity(request: AuthedHttpRequest, team_id: str) -> HttpResponse: + ... +``` + +You can also get more strict with your `login_required` decorator so that the +first argument of the function it is decorating is `AuthedHttpRequest`: + +```python +from typing import Any, Union, TypeVar, cast +from django.http import HttpRequest, HttpResponse +from typing_extensions import Protocol +from functools import wraps + +class RequestHandler1(Protocol): + def __call__(self, request: AuthedHttpRequest) -> HttpResponse: + ... + + +class RequestHandler2(Protocol): + def __call__(self, request: AuthedHttpRequest, __arg1: Any) -> HttpResponse: + ... + + +RequestHandler = Union[RequestHandler1, RequestHandler2] + + +# Verbose bound arg due to limitations of Python typing. +# see: https://github.com/python/mypy/issues/5876 +_F = TypeVar("_F", bound=RequestHandler) + + +def login_required(view_func: _F) -> _F: + @wraps(view_func) + def wrapped_view( + request: AuthedHttpRequest, *args: object, **kwargs: object + ) -> HttpResponse: + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) # type: ignore [call-arg] + raise AuthenticationRequired + + return cast(_F, wrapped_view) +``` + +Then the following will type error: + +```python +@auth.login_required +def activity(request: HttpRequest, team_id: str) -> HttpResponse: + ... +``` + +## related + +- <https://github.com/sbdchd/djangorestframework-types> +- <https://github.com/sbdchd/celery-types> +- <https://github.com/sbdchd/mongo-types> +- <https://github.com/sbdchd/msgpack-types> + + +%package -n python3-django-types +Summary: Type stubs for Django +Provides: python-django-types +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-django-types +# django-types [](https://pypi.org/project/django-types/) + +Type stubs for [Django](https://www.djangoproject.com). + +> Note: this project was forked from +> <https://github.com/typeddjango/django-stubs> with the goal of removing the +> [`mypy`](https://github.com/python/mypy) plugin dependency so that `mypy` +> can't [crash due to Django +> config](https://github.com/typeddjango/django-stubs/issues/318), and that +> non-`mypy` type checkers like +> [`pyright`](https://github.com/microsoft/pyright) will work better with +> Django. + +## install + +```bash +pip install django-types +``` + +You'll need to monkey patch Django's `QuerySet`, `Manager` (not needed for Django 3.1+) and +`ForeignKey` (not needed for Django 4.1+) classes so we can index into them with a generic +argument. Add this to your settings.py: + +```python +# in settings.py +from django.db.models import ForeignKey +from django.db.models.manager import BaseManager +from django.db.models.query import QuerySet + +# NOTE: there are probably other items you'll need to monkey patch depending on +# your version. +for cls in [QuerySet, BaseManager, ForeignKey]: + cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) # type: ignore [attr-defined] +``` + +## usage + +### ForeignKey ids and related names as properties in ORM models + +When defining a Django ORM model with a foreign key, like so: + +```python +class User(models.Model): + team = models.ForeignKey( + "Team", + null=True, + on_delete=models.SET_NULL, + ) + role = models.ForeignKey( + "Role", + null=True, + on_delete=models.SET_NULL, + related_name="users", + ) +``` + +two properties are created, `team` as expected, and `team_id`. Also, a related +manager called `user_set` is created on `Team` for the reverse access. + +In order to properly add typing to the foreign key itself and also for the created ids you can do +something like this: + +```python +from typing import TYPE_CHECKING + +from someapp.models import Team +if TYPE_CHECKING: + # In this example Role cannot be imported due to circular import issues, + # but doing so inside TYPE_CHECKING will make sure that the typing below + # knows what "Role" means + from anotherapp.models import Role + + +class User(models.Model): + team_id: Optional[int] + team = models.ForeignKey( + Team, + null=True, + on_delete=models.SET_NULL, + ) + role_id: int + role = models.ForeignKey["Role"]( + "Role", + null=False, + on_delete=models.SET_NULL, + related_name="users", + ) + + +reveal_type(User().team) +# note: Revealed type is 'Optional[Team]' +reveal_type(User().role) +# note: Revealed type is 'Role' +``` + +This will make sure that `team_id` and `role_id` can be accessed. Also, `team` and `role` +will be typed to their right objects. + +To be able to access the related manager `Team` and `Role` you could do: + +```python +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + # This doesn't really exists on django so it always need to be imported this way + from django.db.models.manager import RelatedManager + from user.models import User + + +class Team(models.Model): + if TYPE_CHECKING: + user_set = RelatedManager["User"]() + + +class Role(models.Model): + if TYPE_CHECKING: + users = RelatedManager["User"]() + +reveal_type(Team().user_set) +# note: Revealed type is 'RelatedManager[User]' +reveal_type(Role().users) +# note: Revealed type is 'RelatedManager[User]' +``` + +An alternative is using annotations: + + + +```python +from __future__ import annotations # or just be in python 3.11 + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from django.db.models import Manager + from user.models import User + + +class Team(models.Model): + user_set: Manager[User] + + +class Role(models.Model): + users: Manager[User] + +reveal_type(Team().user_set) +# note: Revealed type is 'Manager[User]' +reveal_type(Role().users) +# note: Revealed type is 'Manager[User]' +``` + + +### `id Field` + +By default Django will create an `AutoField` for you if one doesn't exist. + +For type checkers to know about the `id` field you'll need to declare the +field explicitly. + +```python +# before +class Post(models.Model): + ... + +# after +class Post(models.Model): + id = models.AutoField(primary_key=True) + # OR + id: int +``` + +### `HttpRequest`'s `user` property + +The `HttpRequest`'s `user` property has a type of `Union[AbstractBaseUser, AnonymousUser]`, +but for most of your views you'll probably want either an authed user or an +`AnonymousUser`. + +So we can define a subclass for each case: + +```python +class AuthedHttpRequest(HttpRequest): + user: User # type: ignore [assignment] +``` + +And then you can use it in your views: + +```python +@auth.login_required +def activity(request: AuthedHttpRequest, team_id: str) -> HttpResponse: + ... +``` + +You can also get more strict with your `login_required` decorator so that the +first argument of the function it is decorating is `AuthedHttpRequest`: + +```python +from typing import Any, Union, TypeVar, cast +from django.http import HttpRequest, HttpResponse +from typing_extensions import Protocol +from functools import wraps + +class RequestHandler1(Protocol): + def __call__(self, request: AuthedHttpRequest) -> HttpResponse: + ... + + +class RequestHandler2(Protocol): + def __call__(self, request: AuthedHttpRequest, __arg1: Any) -> HttpResponse: + ... + + +RequestHandler = Union[RequestHandler1, RequestHandler2] + + +# Verbose bound arg due to limitations of Python typing. +# see: https://github.com/python/mypy/issues/5876 +_F = TypeVar("_F", bound=RequestHandler) + + +def login_required(view_func: _F) -> _F: + @wraps(view_func) + def wrapped_view( + request: AuthedHttpRequest, *args: object, **kwargs: object + ) -> HttpResponse: + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) # type: ignore [call-arg] + raise AuthenticationRequired + + return cast(_F, wrapped_view) +``` + +Then the following will type error: + +```python +@auth.login_required +def activity(request: HttpRequest, team_id: str) -> HttpResponse: + ... +``` + +## related + +- <https://github.com/sbdchd/djangorestframework-types> +- <https://github.com/sbdchd/celery-types> +- <https://github.com/sbdchd/mongo-types> +- <https://github.com/sbdchd/msgpack-types> + + +%package help +Summary: Development documents and examples for django-types +Provides: python3-django-types-doc +%description help +# django-types [](https://pypi.org/project/django-types/) + +Type stubs for [Django](https://www.djangoproject.com). + +> Note: this project was forked from +> <https://github.com/typeddjango/django-stubs> with the goal of removing the +> [`mypy`](https://github.com/python/mypy) plugin dependency so that `mypy` +> can't [crash due to Django +> config](https://github.com/typeddjango/django-stubs/issues/318), and that +> non-`mypy` type checkers like +> [`pyright`](https://github.com/microsoft/pyright) will work better with +> Django. + +## install + +```bash +pip install django-types +``` + +You'll need to monkey patch Django's `QuerySet`, `Manager` (not needed for Django 3.1+) and +`ForeignKey` (not needed for Django 4.1+) classes so we can index into them with a generic +argument. Add this to your settings.py: + +```python +# in settings.py +from django.db.models import ForeignKey +from django.db.models.manager import BaseManager +from django.db.models.query import QuerySet + +# NOTE: there are probably other items you'll need to monkey patch depending on +# your version. +for cls in [QuerySet, BaseManager, ForeignKey]: + cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) # type: ignore [attr-defined] +``` + +## usage + +### ForeignKey ids and related names as properties in ORM models + +When defining a Django ORM model with a foreign key, like so: + +```python +class User(models.Model): + team = models.ForeignKey( + "Team", + null=True, + on_delete=models.SET_NULL, + ) + role = models.ForeignKey( + "Role", + null=True, + on_delete=models.SET_NULL, + related_name="users", + ) +``` + +two properties are created, `team` as expected, and `team_id`. Also, a related +manager called `user_set` is created on `Team` for the reverse access. + +In order to properly add typing to the foreign key itself and also for the created ids you can do +something like this: + +```python +from typing import TYPE_CHECKING + +from someapp.models import Team +if TYPE_CHECKING: + # In this example Role cannot be imported due to circular import issues, + # but doing so inside TYPE_CHECKING will make sure that the typing below + # knows what "Role" means + from anotherapp.models import Role + + +class User(models.Model): + team_id: Optional[int] + team = models.ForeignKey( + Team, + null=True, + on_delete=models.SET_NULL, + ) + role_id: int + role = models.ForeignKey["Role"]( + "Role", + null=False, + on_delete=models.SET_NULL, + related_name="users", + ) + + +reveal_type(User().team) +# note: Revealed type is 'Optional[Team]' +reveal_type(User().role) +# note: Revealed type is 'Role' +``` + +This will make sure that `team_id` and `role_id` can be accessed. Also, `team` and `role` +will be typed to their right objects. + +To be able to access the related manager `Team` and `Role` you could do: + +```python +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + # This doesn't really exists on django so it always need to be imported this way + from django.db.models.manager import RelatedManager + from user.models import User + + +class Team(models.Model): + if TYPE_CHECKING: + user_set = RelatedManager["User"]() + + +class Role(models.Model): + if TYPE_CHECKING: + users = RelatedManager["User"]() + +reveal_type(Team().user_set) +# note: Revealed type is 'RelatedManager[User]' +reveal_type(Role().users) +# note: Revealed type is 'RelatedManager[User]' +``` + +An alternative is using annotations: + + + +```python +from __future__ import annotations # or just be in python 3.11 + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from django.db.models import Manager + from user.models import User + + +class Team(models.Model): + user_set: Manager[User] + + +class Role(models.Model): + users: Manager[User] + +reveal_type(Team().user_set) +# note: Revealed type is 'Manager[User]' +reveal_type(Role().users) +# note: Revealed type is 'Manager[User]' +``` + + +### `id Field` + +By default Django will create an `AutoField` for you if one doesn't exist. + +For type checkers to know about the `id` field you'll need to declare the +field explicitly. + +```python +# before +class Post(models.Model): + ... + +# after +class Post(models.Model): + id = models.AutoField(primary_key=True) + # OR + id: int +``` + +### `HttpRequest`'s `user` property + +The `HttpRequest`'s `user` property has a type of `Union[AbstractBaseUser, AnonymousUser]`, +but for most of your views you'll probably want either an authed user or an +`AnonymousUser`. + +So we can define a subclass for each case: + +```python +class AuthedHttpRequest(HttpRequest): + user: User # type: ignore [assignment] +``` + +And then you can use it in your views: + +```python +@auth.login_required +def activity(request: AuthedHttpRequest, team_id: str) -> HttpResponse: + ... +``` + +You can also get more strict with your `login_required` decorator so that the +first argument of the function it is decorating is `AuthedHttpRequest`: + +```python +from typing import Any, Union, TypeVar, cast +from django.http import HttpRequest, HttpResponse +from typing_extensions import Protocol +from functools import wraps + +class RequestHandler1(Protocol): + def __call__(self, request: AuthedHttpRequest) -> HttpResponse: + ... + + +class RequestHandler2(Protocol): + def __call__(self, request: AuthedHttpRequest, __arg1: Any) -> HttpResponse: + ... + + +RequestHandler = Union[RequestHandler1, RequestHandler2] + + +# Verbose bound arg due to limitations of Python typing. +# see: https://github.com/python/mypy/issues/5876 +_F = TypeVar("_F", bound=RequestHandler) + + +def login_required(view_func: _F) -> _F: + @wraps(view_func) + def wrapped_view( + request: AuthedHttpRequest, *args: object, **kwargs: object + ) -> HttpResponse: + if request.user.is_authenticated: + return view_func(request, *args, **kwargs) # type: ignore [call-arg] + raise AuthenticationRequired + + return cast(_F, wrapped_view) +``` + +Then the following will type error: + +```python +@auth.login_required +def activity(request: HttpRequest, team_id: str) -> HttpResponse: + ... +``` + +## related + +- <https://github.com/sbdchd/djangorestframework-types> +- <https://github.com/sbdchd/celery-types> +- <https://github.com/sbdchd/mongo-types> +- <https://github.com/sbdchd/msgpack-types> + + +%prep +%autosetup -n django-types-0.17.0 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-django-types -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 0.17.0-1 +- Package Spec generated @@ -0,0 +1 @@ +ebaa190e66edde8c04d8fc9da20d0886 django-types-0.17.0.tar.gz |
