Commit Graph

1013 Commits

Author SHA1 Message Date
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
PIG208
516deba2fa Improve stubs with minor fixes (#1038)
* Add a missing attribute to Jinja2.

https://github.com/django/django/blob/main/django/template/backends/jinja2.py#L35

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

* Make _QuerySet.extra's signature more generic.

This makes sure that we don't reject tuples, which is also valid
according to the implementation.

Relevant source code:
03eec9ff6c/django/db/models/sql/where.py (L271-L281)
03eec9ff6c/django/db/models/sql/query.py (L2307-L2308)

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

* Fix user_passes_test to use AUTH_USER_MODEL.

According to the documentation, `test_func` is a callable that takes a
`User` (possibly anonymous).

Relevant documentation:
https://docs.djangoproject.com/en/4.0/topics/auth/default/#django.contrib.auth.decorators.user_passes_test

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

* Add more accurate type annotations for dirs.

Though not documented, it's possible for `dirs` to contain
`pathlib.Path`.

`django.template.loaders.app_directories.Loader` is an example for this:
03eec9ff6c/django/template/loaders/app_directories.py
03eec9ff6c/django/template/utils.py (L97-L111)

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

* serve should return FileResponse.

There are several serve functions that should return a `FileResponse`.

Source code:
863aa7541d/django/views/static.py (L17-L53)
863aa7541d/django/contrib/staticfiles/views.py (L15-L39)
863aa7541d/django/contrib/staticfiles/handlers.py (L48-L50)
863aa7541d/django/test/testcases.py (L1680-L1687)

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2022-07-04 23:46:53 +03:00
Sigurd Ljødal
ae30525404 Fix type stubs for EmptyManager (#1039)
This should be parametrized based on the passed model. This also removes
the need for overriding the get_queryset method.
2022-07-04 12:43:58 +03:00
Petter Friberg
2a6f4647f0 Populate model argument for dynamically created managers (#1033)
* Populate model for dynamically created managers

* fixup! Populate model for dynamically created managers
2022-06-30 10:25:29 +03:00
Sigurd Ljødal
84eff75566 Resolve all queryset methods on managers as attributes (#1028)
* Add test case reproducing Sequence name not defined issue

* Resolve all manager methods as attribute

This changes to logic for resolving methods from the base QuerySet class
on managers from copying the methods to use the attribute approach
that's already used for methods from custom querysets. This resolves the
phantom type errors that stem from the copying.

* Disable cache in test case

Make sure the test will fail regardless of which mypy.ini file is being using.

Co-authored-by: Petter Friberg <petter@5monkeys.se>

* Update comments related to copying methods

* Use a predefined list of manager methods to update

The list of manager methods that returns a queryset, and thus need to
have it's return type changed, is small and well defined. Using a
predefined list of methods rather than trying to detect these at runtime
makes the code much more readable and probably faster as well.

Also add `extra()` to the methods tested in
from_queryset_includes_methods_returning_queryset, and sort the methods
alphabetically.

* Revert changes in .github/workflows/tests.yml

With cache_disable: true on the test case this is no longer needed to
reproduce the bug.

* Remove unsued imports and change type of constant

 - Remove unused imports left behind
 - Change MANAGER_METHODS_RETURNING_QUERYSET to Final[FrozenSet[str]]

* Import Final from typing_extensions

Was added in 3.8, we still support 3.7

* Sort imports properly

* Remove explicit typing of final frozenset

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>

* Add comment for test case

* Fix typo

* Rename variable

Co-authored-by: Petter Friberg <petter@5monkeys.se>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-06-30 10:19:33 +03:00
David Smith
f8cc99ca3b Improved return type of render_to_string(). (#1036) 2022-06-30 10:16:07 +03:00
dependabot[bot]
c27b38d6b9 Bump requests from 2.28.0 to 2.28.1 (#1035)
Bumps [requests](https://github.com/psf/requests) from 2.28.0 to 2.28.1.
- [Release notes](https://github.com/psf/requests/releases)
- [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md)
- [Commits](https://github.com/psf/requests/compare/v2.28.0...v2.28.1)

---
updated-dependencies:
- dependency-name: requests
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-30 10:14:45 +03:00
Anders Kaseorg
214b0c7439 Support cursor.execute(psycopg2.sql.Composable) (#1029)
In addition to str, PostgreSQL cursors accept the
psycopg2.sql.Composable type, which is useful for guarding against SQL
injections when building raw queries that can’t be parameterized in
the normal way (e.g. interpolating identifiers).

In order to avoid reintroducing a dependency on psycopg2, we define a
Protocol that matches psycopg2.sql.Composable.

Documentation: https://www.psycopg.org/docs/sql.html
Related: https://github.com/python/typeshed/pull/7494

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-06-28 23:44:20 +03:00
Nikita Sobolev
33d4dc7dae Fix logger type in all modules (#1031)
* Fix `logger` type in all modules

* Fix CI
2022-06-28 23:25:50 +03:00