Commit Graph

971 Commits

Author SHA1 Message Date
Adam Johnson
11c587867d Add Q.__xor__ (#1133) 2022-08-28 11:14:03 +03:00
Joon Hwan 김준환
d0a9793917 Add async queryset for Django 4.1 (#1131)
* Add async queryset for Django 4.1

* dedup

* fix to async
2022-08-28 11:13:56 +03:00
PIG208
18a055124a Return Promise for lazy functions. (#689)
* Type the return value of lazy translation functions as Promise.

The return value of the lazy translation functions is a proxied
`Promise` object.
https://github.com/django/django/blob/3.2.6/django/utils/translation/__init__.py#L135-L221.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Mark unicode translation functions for deprecation.

https://docs.djangoproject.com/en/4.0/releases/4.0/#features-removed-in-4-0.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Add proxied functions for Promise.

Although there is nothing defined in `Promise` itself, the only
instances of `Promise` are created by the `lazy` function, with magic
methods defined on it.

https://github.com/django/django/blob/3.2.6/django/utils/functional.py#L84-L191.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Add _StrPromise as a special type for Promise objects for str.

This allows the user to access methods defined on lazy strings while
still letting mypy be aware of that they are not instances of `str`.

The definitions for some of the magic methods are pulled from typeshed. We need
those definitions in the stubs so that `_StrPromise` objects will work properly
with operators, as refining operator types is tricky with the mypy
plugins API.

The rest of the methods will be covered by an attribute hook.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Implement _StrPromise attribute hook.

This implements an attribute hook that provides type information for
methods that are available on `builtins.str` for `_StrPromise` except
the supported operators. This allows us to avoid copying stubs from the
builtins for all supported methods on `str`.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Allow message being a _StrPromise object for RegexValidator.

One intended usage of lazystr is to postpone the translation of the
error message of a validation error. It is possible that we pass a
Promise (specifically _StrPromise) and only evaluate it when a
ValidationError is raised.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Refactor _StrPromise attribtue hook with analyze_member_access.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-08-27 21:08:31 +03:00
PIG208
11ded9deaf Enhance stubs for csrf decorators. (#1130)
* Use inherited types from CsrfViewMiddleware.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Define decorators generated from middleware with TypeVars.

csrf_protect and etc. are created via the decorator_from_middleware
helper, which doesn't modify the original signature of the wrapped view
function. Using TypeVar helps preserve the original type of the
decorated callable.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-08-27 20:29:30 +03:00
Petter Friberg
8bd00ba25a Type samesite values as literals 'Lax', 'Strict' or 'None' (#1110)
* Type `samesite` values as literals 'Lax', 'Strict' or 'None'

- Adjusts supported `SESSION_COOKIE_SAMESITE` values
- Adjusts supported `CSRF_COOKIE_SAMESITE` values

* Update django-stubs/conf/global_settings.pyi

* Update response.pyi

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-08-27 02:02:47 +03:00
László Károlyi
6b39050d52 Updating deconstructible (#1116)
* Updating deconstructible

* Black

* Update deconstruct.pyi

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-08-27 01:43:32 +03:00
Adam Johnson
a01ab43cc6 Add HttpRequest current_app and LANGUAGE_CODE attrs (#1127)
* Add HttpRequest.current_app

* rearrange and add LANGUAGE_CODE
2022-08-26 17:23:50 +03:00
Adam Johnson
82695e8e1c Add db Field.flatchoices (#1128)
* Add db Field.flatchoices

* @property
2022-08-26 17:23:26 +03:00
Adam Johnson
0120fc531a Rewrite SessionBase properties to use @property (#1129) 2022-08-26 17:22:03 +03:00
Adam Johnson
de3e13ad92 Add database Field.non_db_attrs (#1125)
* Add database Field.non_db_attrs

* Update django-stubs/db/models/fields/__init__.pyi

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-08-26 14:56:56 +03:00
Nikita Sobolev
06359013c9 Run stubs self-check on all python versions (#1126) 2022-08-26 14:36:23 +03:00
Adam Johnson
63bee32d0d Add different signatures for form assertions from Django 4.1 (#1105) 2022-08-26 13:31:45 +03:00
Adam Johnson
1a310ef7f9 Add test client response.redirect_chain (#1124) 2022-08-26 13:31:17 +03:00
Adam Johnson
8b5509b2ab Improve type hints for test client response.context (#1100)
* Improve type hints for test client response.context

* fix test
2022-08-26 13:30:18 +03:00
Nikita Sobolev
0bb1182c42 Fix CI (#1108)
* Fix CI

* Fix CI

* Fix CI

* Fix CI

* APply black

* APply black

* Fix mypy

* Fix mypy errors in django-stubs

* Fix format

* Fix plugin

* Do not patch builtins by default

* Fix mypy

* Only run mypy on 3.10 for now

* Only run mypy on 3.10 for now

* WHAT THE HELL

* Enable strict mode in mypy

* Enable strict mode in mypy

* Fix tests

* Fix tests

* Debug

* Debug

* Fix tests

* Fix tests

* Add TYPE_CHECKING debug

* Caching maybe?

* Caching maybe?

* Try explicit `${{ matrix.python-version }}`

* Remove debug

* Fix typing

* Finally
2022-08-26 13:22:55 +03:00
Adam Johnson
d2bfd3710b Improve return type for BaseCommand.execute() (#1104) 2022-08-26 13:10:24 +03:00
Adam Johnson
cf16720f28 Improve types for DiscoverRunner (#1106)
* Improve types for DiscoverRunner

* add types for class-level attrs

* Add extra methods

* Add PDBDebugResult class

* black

* Update django-stubs/test/runner.pyi

* fix load_with_patterns return

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-08-26 13:09:41 +03:00
Adam Johnson
063a35f4d0 Expose HttpResponseBase in django.http (#1121)
* Expose HttpResponseBase in django.http

* isort
2022-08-26 12:45:22 +03:00
Adam Johnson
9ba4a55c93 Improve types for CheckMessage subclasses (#1107) 2022-08-26 12:44:18 +03:00
Adam Johnson
561140a584 Allow None to be passed to Paginator.get_page (#1101) 2022-08-26 12:40:53 +03:00
Adam Johnson
5b705d25da Add SimpleTestCase.assertURLEqual() (#1102) 2022-08-26 12:40:34 +03:00
Adam Johnson
f626448105 Add BaseDatabaseWrapper.operations (#1119) 2022-08-26 12:40:17 +03:00
Adam Johnson
63ea17744f Add HttpResponseBase.__contains__ (#1099) 2022-08-26 12:37:23 +03:00
Adam Johnson
f8fca3f94f Improve hints for BaseCache (#1117)
* Improve hints for BaseCache

* fix syntax error
2022-08-26 12:36:58 +03:00
Anders Kaseorg
041b66a6aa Prevent building an editable wheel with setuptools<64 (#1123)
This seems to be needed for passing CI now.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-08-26 11:55:41 +03:00
Alex Nathanail
3ac34f1108 Fix base_field type for SimpleArrayField (#1097)
* Fix base_field type for SimpleArrayField

* Add test which fails without this change
2022-08-10 16:16:54 +03:00
PIG208
a1445291fd Improve type annotation for RunSQL (#1090)
* Allow passing heterogeneous list or tuple to RunSQL.

The sqls to be executed do not necessarily need to be a homogeneous list
or tuple containing only lists or tuples or strs. It can be a mix of
everything.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Support passing dict as a sql param.

The 2-item tuple for `sql` can have a `dict` as the second item
for parameters. This behavior is the same as using
`cursor.execute` for backends except SQLite.

Relevant implementation:
5f76002500/django/db/migrations/operations/special.py (L119-L133)

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Add a test case for RunSQL.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-08-09 10:36:36 +03:00
PIG208
7b18e354f1 Add preset options list to makemessages command. (#1091)
These options can be found here:

0756c61f2a/django/core/management/commands/makemessages.py (L222-L225)

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-08-09 00:31:34 +03:00
PIG208
db1edeeabf Fix a broken test case after the Django 4.1 update. (#1093)
This fixes the CI starting occur on #1086 and following PRs due to the release
of Django 4.1 (https://docs.djangoproject.com/en/4.1/releases/4.1/) which
shipped the change

    # Even if this relation is not to pk, we require still pk value.
    # The wish is that the instance has been already saved to DB,
    # although having a pk value isn't a guarantee of that.
    if self.instance.pk is None:
        raise ValueError(
            f"{instance.__class__.__name__!r} instance needs to have a primary "
            f"key value before this relationship can be used."
        )

in https://github.com/django/django/pull/15318.
2022-08-09 00:18:08 +03:00
Anders Kaseorg
589f315926 Remove trailing whitespace in README to fix CI failure (#1084)
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-08-02 11:07:21 +03:00
Tony Narlock
5334a80c9b docs(README): Fix indentation in list and examples (#1083)
Run examples through black for uniformity, link `HttpModel`
2022-08-02 02:39:14 +03:00
Thibaut Decombe
966472994b Allow float for min_value and max_value of FloatField (#1081)
Co-authored-by: Thibaut <thibaut.d@hellowatt.fr>
2022-08-01 14:17:50 +03:00
Anders Kaseorg
41804f0e8d Fix OutputWrapper.style_func type (#1080)
style_func has a setter that converts None to lambda x: x, so the
getter cannot return None.

https://github.com/django/django/blob/4.0.6/django/core/management/base.py#L141-L150

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-07-30 12:58:41 +03:00
PIG208
0e9ebf838b Support Lookup generics. (#1079)
Custom `Lookup` implementation will run into
'Missing type parameters for generic type "Lookup"' without having
`Lookup` monkey-patched with django-stubs-ext.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-07-28 21:08:56 +03:00
github-actions[bot]
196e6743ec Auto-update pre-commit hooks (#1076)
Co-authored-by: sobolevn <sobolevn@users.noreply.github.com>
2022-07-27 10:06:55 +03:00
Adam Johnson
efc2d04ead Improve types for @require_http_methods (#1075) 2022-07-26 20:47:05 +03:00
PIG208
56f9300d0d Improve stubs for contrib.postgres.operations. (#1071)
* Improve stubs for contrib.postgres.operations.

This adds a bunch of missing operations in contrib.postgres.operations.

Documentation: https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/operations/#managing-collations-using-migrations
Source code: 2fac0a1808/django/contrib/postgres/operations.py

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Add migration_name_fragment to Operation.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Remove redefinition of methods in subclasses.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-07-24 10:40:13 +03:00
Anders Kaseorg
20f0702f86 Tighten management.get_commands type (#1074)
get_commands has not actually returned BaseCommand items since Django
1.4.  See https://github.com/django/django/pull/15876.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-07-24 10:38:32 +03:00
PIG208
f3f80164a8 Fix type annotation of message_dict. (#1073)
When message_dict is accessed, it either raises an `AttributeError` or
returns a `dict`. There is no way for it to be `Optional`.

caad462fea/django/core/exceptions.py (L170-L176)

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-07-23 16:55:58 +03:00
Anders Kaseorg
3d8d900487 Fix overloads and remove PathLike in finders (#1063)
* Fix overloads in finders

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

* Remove PathLike from finders

Django really requires these paths to be str.  For example, in
FileSystemFinder, .find(path) calls .find_location(…, path, …) which
evaluates path.startswith(prefix) and path[len(prefix) :]; these don’t
work on arbitrary PathLike objects.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-07-22 10:35:24 +03:00
PIG208
563e947402 Improve type annotation of DiscoverRunner. (#1069)
The return type of DiscoverRunner.get_resultclass should
be Optional[Type[TextTestResult]] given that it may also return PDBDebugResult:
0dd2920909/django/test/runner.py (L958-L962)

`DicoverRunner.run_tests` accepts `None` for `extra_tests` because
that's already the default value for it:
0dd2920909/django/test/runner.py (L1030)
2022-07-22 10:34:26 +03:00
github-actions[bot]
62c09c064c Auto-update pre-commit hooks (#1070)
* Auto-update pre-commit hooks

* Update pre-commit-autoupdate.yml

Co-authored-by: sobolevn <sobolevn@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-07-22 10:07:15 +03:00
Nikita Sobolev
89d0a10939 Create pre-commit-autoupdate.yml 2022-07-22 09:34:53 +03:00
Jordan Hayashi
0246f9fcf3 Use a more general type for django.core.mail.send_mass_mail's datatuple (#1062)
* Use a datatuple in django.core.mail.send_mass_mail

Even though the implementation works for both a List of tuples and a
tuple of tuples, the docs recommend using a tuple.

https://github.com/django/django/blob/main/django/core/mail/__init__.py#L112
https://docs.djangoproject.com/en/4.0/topics/email/#send-mass-mail

* Use a more general type for send_mass_mail's datatuple
2022-07-19 21:12:30 +03:00
Sigurd Ljødal
e7a89f73c4 Improve typing for unresolved managers (#1044)
* Improve typing for unresolved managers

This changes the logic when encountering an unresolvable manager class.
Instead of adding it as a `Manager` we create a subclass of `Manager`
that has `fallback_to_any=True` set. Similarly a `QuerySet` class is
created that also has fallbacks to `Any`. This allows calling custom
methods on the manager and querysets without getting type errors.

* Fix manager created and improve a test

* Fix row type of FallbackQuerySet

Because this inherits from _QuerySet, not QuerySet, it needs to have two
parameters
2022-07-18 09:13:46 +03:00
Sigurd Ljødal
830d74b493 Add support for inline from_queryset in model classes (#1045)
* Add support for inline from_queryset in model classes

This adds support for calling <Manager>.from_queryset(<QuerySet>)()
inline in models, for example like this:

    class MyModel(models.Model):
        objects = MyManager.from_queryset(MyQuerySet)()

This is done by inspecting the class body in the transform_class_hook

* Fix missing methods on copied manager

* Add test and other minor tweaks

* Always create manager at module level

When the manager is added at the class level, which happened when it was
created inline in the model body, it's not possible to retrieve the
manager again based on fullname. That lead to problems with inheritance
and the default manager.
2022-07-13 10:04:44 +03:00
David Smith
2e84c03632 Made BaseContext __setitem__ and __getitem__ consistant. (#1040)
* Made BaseContext __setitem__ and __getitem__ consistant.

* Standardised ContextKeys
2022-07-13 09:48:33 +03:00
PIG208
3648e10350 Use _AsSqlType for as_sql (#1052)
* Annotate the return type of as_sql for SpatialOperator.

Its subclasses inherits the type annotation from `SpatialOperator`, so
copying `as_sql` over is unnecessary.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Remove unnecessary as_sql definition.

`Query` inherits `as_sql` from `BaseExpression`, `GISLookup` inherits
`as_sql` from `Lookup`, and `BuiltinLookup` inherits `as_sql` from
`Lookup[_T]`.  None is required to be redefined.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* Unify return types of as_sql and friends as _AsSqlType.

`Tuple[str, _ParamsT]`, `Tuple[str, List[Union[str, int]]]` and other
similar type annotations are all replaced with the `_AsSqlType`
alias. Any as_sql definition that annotate the return type as `Any` is also
updated to use `_AsSqlType`.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-07-13 09:47:01 +03:00
dependabot[bot]
29f0762540 Bump pre-commit from 2.19.0 to 2.20.0 (#1057)
Bumps [pre-commit](https://github.com/pre-commit/pre-commit) from 2.19.0 to 2.20.0.
- [Release notes](https://github.com/pre-commit/pre-commit/releases)
- [Changelog](https://github.com/pre-commit/pre-commit/blob/main/CHANGELOG.md)
- [Commits](https://github.com/pre-commit/pre-commit/compare/v2.19.0...v2.20.0)

---
updated-dependencies:
- dependency-name: pre-commit
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-07-12 11:48:00 +03:00
Nick Pope
e8d52fe7da Monkeypatch some more generics. (#1050)
* Add generic monkeypatching for `FileProxyMixin`.

This fixes `TypeError: 'type' object is not subscriptable` for
`django.core.files.File` and `django.core.files.base.ContentFile`.

* Add generic monkeypatching for `ForeignKey`.

This matches the change coming in Django 4.1.

See https://github.com/django/django/pull/15571
2022-07-06 01:19:51 +03:00