Commit Graph

980 Commits

Author SHA1 Message Date
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
dependabot[bot]
fe2d2287a1 Bump black from 22.3.0 to 22.6.0 (#1027)
Bumps [black](https://github.com/psf/black) from 22.3.0 to 22.6.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](https://github.com/psf/black/compare/22.3.0...22.6.0)

---
updated-dependencies:
- dependency-name: black
  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-06-28 09:42:09 +03:00
David Smith
926661ab4a Improved type of template.loader.get_template() (#1011)
* Improved type of template.loader.get_template()

* Removed Optional from return type.

* Edits.
2022-06-27 00:48:10 +03:00
Sigurd Ljødal
1a29ad4f97 Fix nullability of blank charfields in values and values_list (#1020)
Char fields with blank=True set should not be considered nullable in the
context of values() and values_list() querysets.

I'm also not a huge fan of the way these fields are made optional in the
constructur to the model classes, it feels like it would be better to
mark the arguments as having a default value, rather than allow sending
in None, but I'd rather keep this fix small and look at the overall
problem at a later point.
2022-06-25 14:37:25 +03:00
Jakub Hrabec
dd0fe02367 Add missing type for TestCase.captureOnCommitCallbacks (#1016)
* add missing type for TestCase.captureOnCommitCallbacks

* change default value
2022-06-25 13:41:29 +03:00
David Smith
44157a4b03 BaseContext.__copy__ (#1012)
* Improved BaseContext.__copy__()

* Update django-stubs/template/context.pyi

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

* Updated __copy__ to use private name.

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-06-25 11:57:04 +03:00
Jakub Hrabec
c020349f2e Update RunSQL type to include parametrized sql (#1015)
* update RunSQL type to include parametrized sql

* fix Black formatting
2022-06-24 10:08:17 +03:00
w0rp
644052148a Set generic type of SimpleCookie (#1018)
Pyright complains about `response.cookies` because the generic type isn't known. `str` may or may not be the correct type to use here. Something should be set here.
2022-06-23 13:52:31 +03:00
sobolevn
f4407155a5 Do not generate release notes for django-stubs-ext 2022-06-17 18:14:44 +03:00
sobolevn
d6a9a4ef03 Fixing release pipeline 2022-06-17 18:09:00 +03:00
sobolevn
9a23ff864b Fixing release pipeline 2022-06-17 17:54:02 +03:00
sobolevn
4ea9b874f5 Add explicit test for .from_quertset Any fallback 2022-06-17 17:30:27 +03:00
Petter Friberg
023106fe45 Emit error and set fallback type for managers that can't be resolved (#999)
* Emit error and set fallback type for managers that can't be resolved

* fixup! Emit error and set fallback type for managers that can't be resolved
2022-06-17 17:19:42 +03:00
sobolevn
719cd3a6bc Fix release pipeline 2022-06-17 17:18:15 +03:00
sobolevn
4cf3b445bd Fix release pipeline 2022-06-17 17:15:28 +03:00
sobolevn
f4378112bb Release django-stubs and django-stubs-ext 2022-06-17 17:02:26 +03:00
Adam Birds
86c007d83f Add Feed to monkeypatch to fix generic issue. (#1002)
Fixes #1001
2022-06-17 16:39:11 +03:00
Nikita Sobolev
633030585d Update README.md 2022-06-17 16:33:12 +03:00
Petter Friberg
8d8b8cd1fc Set type of default django.core.cache.cache to BaseCache (#998)
* Set type of default `django.core.cache` to `BaseCache`

- The previous type `ConnectionProxy` is just a proxy class, thus
  revealing `Any` for _all_ cache methods

* fixup! Set type of default `django.core.cache` to `BaseCache`
2022-06-16 23:33:37 +03:00
Marti Raudsepp
e45ecd633e Improve hints in migration Operation class (#996) 2022-06-16 09:15:23 +03:00
Aleksander Vognild Burkow
9044a354cb Add fallback related manager in final iteration of AddRelatedManagers (#993)
If a django model has a Manager class that cannot be resolved statically
(if it is generated in a way where we cannot import it, like `objects =
my_manager_factory()`), we fallback to the default related manager, so
you at least get a base level of working type checking.
2022-06-15 18:50:53 +03:00
sterliakov
32e13c37a6 Fix manager types scope (#991)
* Fix manager types scope

* Restore incremental mode and mention in developer docs

* Separate dev mypy config and regular one

* Document config files usage
2022-06-14 20:30:13 +03:00
Adam Johnson
ae5b1a4edf Improve admin filter lookups() return type (#992) 2022-06-13 18:45:55 +03:00
dependabot[bot]
6e84c103af Bump actions/setup-python from 3 to 4 (#989)
Bumps [actions/setup-python](https://github.com/actions/setup-python) from 3 to 4.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](https://github.com/actions/setup-python/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-major
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-11 09:43:41 +03:00
dependabot[bot]
b6cd673e4c Bump requests from 2.27.1 to 2.28.0 (#990)
Bumps [requests](https://github.com/psf/requests) from 2.27.1 to 2.28.0.
- [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.27.1...v2.28.0)

---
updated-dependencies:
- dependency-name: requests
  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-06-11 09:43:33 +03:00
Terence Honles
1ce113d743 Enable GitHub auto generated release notes for GitHub releases (#988) 2022-06-11 09:43:24 +03:00
Zac Miller
f7ccf04082 Remove coreapi from requirements.txt (#987) 2022-06-09 00:04:16 +03:00
Terence Honles
a526ba75be remove unneeded submodule (removed in #485) (#982) 2022-06-07 23:07:15 +03:00
Adam Johnson
13c09921f8 Improve admin filter types (#984) 2022-06-07 12:29:44 +03:00
dependabot[bot]
195aaad1fe Bump mypy from 0.960 to 0.961 (#983)
Bumps [mypy](https://github.com/python/mypy) from 0.960 to 0.961.
- [Release notes](https://github.com/python/mypy/releases)
- [Commits](https://github.com/python/mypy/compare/v0.960...v0.961)

---
updated-dependencies:
- dependency-name: mypy
  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-06-07 12:23:20 +03:00
Terence Honles
42321cabe6 Add GitHub release action to upload to PyPI & create GitHub release (#980)
* Add GitHub release action to upload to PyPI & create GitHub release

* Update .github/workflows/release.yml

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-06-07 12:23:03 +03:00
Anders Kaseorg
24a3b22c60 Move mypy version upper bound to a [compatible-mypy] extra (#979)
* Move mypy version upper bound to a [compatible-mypy] extra

Due to a bug in mypy 0.940 (#870), we made two changes in #871:

• pinned mypy==0.931 in requirements.txt (for running our tests);
• bounded mypy<0.940 in setup.py (for downstream users).

After the mypy bug was quickly fixed upstream in 0.941, our setup.py
bound has been repeatedly raised but not removed (#886, #939, #973).
The only changes in those commits have been to the precise wording of
error messages expected in our tests.  Those wording changes don’t
impact compatibility for downstream users, so it should be safe to go
back to allowing them to upgrade mypy independently.

Since mypy doesn’t yet guarantee backwards compatibility in the plugin
API (although in practice it has rarely been an issue), add a
django-stubs[compatible-mypy] extra for users who prefer a known-good
version of mypy even if it’s a little out of date.

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

* Update setup.py

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-06-05 00:08:00 +03:00
Daniel Hahler
43b082850c Fix BaseModelFormSet.save_m2m: accept self (#970)
* Fix BaseModelFormSet.save_m2m: accept `self`

Before it would result in the following when calling it:

> Attribute function "save_m2m" with type "Callable[[], None]" does not accept self argument  [misc]

* fixup! Fix BaseModelFormSet.save_m2m: accept `self`
2022-05-27 13:42:45 +03:00
Ceesjan Luiten
42d8e18bf8 Support content attribute for a Testing Response. (#968)
`content` is a documented attribute of a testing response which isn't part
of the parent HttpResponseBase:

https://docs.djangoproject.com/en/4.0/topics/testing/tools/#testing-responses
2022-05-26 13:17:22 +03:00