Django 3.2 introduced two new methods: `sign_object` and
`unsign_object` which can sign/unsign "complex data structures" such as
lists, tuples, dictionaries:
https://docs.djangoproject.com/en/3.2/topics/signing/#django.core.signing.TimestampSigner
Because the methods take an arbitrary serializer (a JSON serializer by
default, but not guaranteed), we cannot be sure of the type of `obj`.
* Correct the type of FileField.storage
This instance property can't be a callable. Although the FileField constructor
allows a callable, it is immediately resolved to an instance of Storage.
See: f5802a21c4/django/db/models/fields/files.py (L231-L235)
* Correct the type of FieldFile.storage
This instance property is copied directly from `FileField.storage` and should be
the same type.
See: f5802a21c4/django/db/models/fields/files.py (L21)
* Add failing test for relation to model inheriting `objects`
Fails with:
```
pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output:
Expected:
main:2: note: Revealed type is "myapp.models.MyUser*"
main:3: note: Revealed type is "myapp.models.MyUser*"
<45 (diff)
<45 (diff)
Actual:
main:2: note: Revealed type is "myapp.models.MyUser*"
main:3: note: Revealed type is "myapp.models.MyUser*"
main:6: error: "MyUser" has no attribute "book_set" (diff)
main:6: note: Revealed type is "Any" (diff)
main:7: error: "MyUser" has no attribute "article_set" (diff)
main:7: note: Revealed type is "Any" (diff)
```
* Make AddRelatedManagers look for "objects" on parent model
Previously, AddRelatedManagers would fail if a related model had inherited
its `objects` field from a parent class. This would result in missing
relation attributes. This is fixed by using `get()` instead of `names`;
the former searches the MRO for the symbol, whereas the latter only looks
for symbols declared directly on the class.
Needed, because `django-stubs-ext` generally has to specified as a
production dependency (to use the monkey patching). `django-stubs` does
have `typing-extensions` as a dependency, but it is often used only as a
dev dependency as it has not runtime functionality.
Resolves: https://github.com/typeddjango/django-stubs/issues/702
* Use `Sequence` instead of `Iterable` for `send_messages`.
According to the documentation
(https://docs.djangoproject.com/en/3.2/topics/email/#email-backends),
`email_messages` is a list. Using `Iterable` will make it hard for
subclasses to implement this method utilizing functions like `__len__`.
While this still allows subclasses to accept `Iterable`.
* Fix function signature of `authenticate` of `BaseBackend`.
1. BaseBackend no longer requires the username and password argument.
They were removed 3 years ago in the commit below when `BaseBackend` is added:
75337a6050
2. `request` is optional for `authenticate` method.
According to django documentation, the authenticate method does not
necessarily require the request object.
https://docs.djangoproject.com/en/3.2/topics/auth/default/#authenticating-users
* Tighten the type of `streaming_content` to `Iterator[bytes]`.
It is an iterator of a bytestring according to the documentation:
https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.StreamingHttpResponse.streaming_content
* Fix function signature of `django.contrib.staticfiles.serve`.
Since this `serve` function uses `django.views.static.serve` that
accepts `HttpRequest` as its first argument, it is more reasonable
to type it with `HttpRequest` instead of `WSGIRequest`.
Related:
https://github.com/django/django/blob/main/django/contrib/staticfiles/views.py#L39
* Fix `MyModel.objects.filter(...).my_method()`
* Fix regression: `MyModel.objects.filter(...).my_method()` no longer worked when using from_queryset
This also fixes the self-type of the copied-over methods of the manager generated by from_queryset.
Previously it was not parameterized by the model class, but used Any.
The handling of unbound types is not tested here as I have not been able to
find a way to create a test case for it. It has been manually tested
against an internal codebase.
* Remove unneeded defer.
This matches the actual implementation in Django, where it only attempts
to use the result of get_users() in a for loop, which allows for any
Iterable, and it provides a more flexible and idiomatic API for users
who subclass PasswordResetForm.
The `display` decorator is defined in `django.contrib.admin.decorators`, but
isn't included in `django.contrib.admin`, which is how the Django docs describe
its usage.
* Add accepts method to HttpRequest stubs
The accepts method (and dependencies) was missing in the stub for HttpRequest
* Change accepted_types to return correct type
* Fix Black formatting
* Add annotation for MediaType __init__ args
* Add datetime to set_type of DateTimeField
`DateTimeField` was missing `datetime` as a valid set type. But Django clearly accepts `datetime`.
* Fix test for DateTimeField type change
datetime is now a valid set type for DateTimeField
* QuerySet.annotate returns self-type. Attribute access falls back to Any.
- QuerySets that have an annotated model do not report errors during .filter() when called with invalid fields.
- QuerySets that have an annotated model return ordinary dict rather than TypedDict for .values()
- QuerySets that have an annotated model return Any rather than typed Tuple for .values_list()
* Fix .annotate so it reuses existing annotated types. Fixes error in typechecking Django testsuite.
* Fix self-typecheck error
* Fix flake8
* Fix case of .values/.values_list before .annotate.
* Extra ignores for Django 2.2 tests (false positives due to tests assuming QuerySet.first() won't return None)
Fix mypy self-check.
* More tests + more precise typing in case annotate called before values_list.
Cleanup tests.
* Test and fix annotate in combination with values/values_list with no params.
* Remove line that does nothing :)
* Formatting fixes
* Address code review
* Fix quoting in tests after mypy changed things
* Use Final
* Use typing_extensions.Final
* Fixes after ValuesQuerySet -> _ValuesQuerySet refactor. Still not passing tests yet.
* Fix inheritance of _ValuesQuerySet and remove unneeded type ignores.
This allows the test
"annotate_values_or_values_list_before_or_after_annotate_broadens_type"
to pass.
* Make it possible to annotate user code with "annotated models", using PEP 583 Annotated type.
* Add docs
* Make QuerySet[_T] an external alias to _QuerySet[_T, _T].
This currently has the drawback that error messages display the internal type _QuerySet, with both type arguments.
See also discussion on #661 and #608.
Fixes#635: QuerySet methods on Managers (like .all()) now return QuerySets rather than Managers.
Address code review by @sobolevn.
* Support passing TypedDicts to WithAnnotations
* Add an example of an error to README regarding WithAnnotations + TypedDict.
* Fix runtime behavior of ValuesQuerySet alias (you can't extend Any, for example).
Fix some edge case with from_queryset after QuerySet changed to be an
alias to _QuerySet. Can't make a minimal test case as this only occurred
on a large internal codebase.
* Fix issue when using from_queryset in some cases when having an argument with a type annotation on the QuerySet.
The mypy docstring on anal_type says not to call defer() after it.
* widen type of make_password's hasher
make_password will accept a subclass of BasePasswordHasher as a hasher
* widen get_hasher's algorithm type
get_hasher will accept (and immediately return) a BasePasswordHasher