* Reparametrize managers without explicit type parameters
This extracts the reparametrization logic from #1030 in addition to
removing the codepath that copied methods from querysets to managers.
That code path seems to not be needed with this change.
* Use typevars from parent instead of base
* Use typevars from parent manager instead of base manager
This removes warnings when subclassing from something other than the
base manager class, where the typevar has been restricted.
* Remove unused imports
* Fix failed test
* Only reparametrize if generics are omitted
* Fix docstring
* Add test with disallow_any_generics=True
* Add an FAQ section and document disallow_any_generics behaviour
* Fix type of <fieldname>_id when using ForeignKey(to_field=)
Previously mypy_django_plugin would always use the field type of target
model's primary key, but `to_field` can refer to a different field type.
* Fixes
* More fixes
* Implement support for `<QuerySet>.as_manager()`
* fixup! Implement support for `<QuerySet>.as_manager()`
* fixup! fixup! Implement support for `<QuerySet>.as_manager()`
This fallback to value.__class__ seems to be doing more harm than
good; see #312 and #1162. Replace it with a clear error message that
suggests a way to fix the problem rather than incompletely papering
over it.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This fixes an error that occured during state serialization. Completely
unsure how to reproduce this in a test but it resolves a long-standing
prolem in our project at work at least.
* Broaden type annotation for verbose_name(_plural) to accept lazystr.
Fixes#1137.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Broaden type annotation for help_text to accept lazystr.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Broaden type annotation for ValidationError to accept lazystr.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Broaden type annotation for label to accept lazystr.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Add StrPromise and StrOrPromise aliases to django_stubs_ext.
We make StrPromise and StrOrPromise available via django_stubs_ext so
that conditional imports with TYPE_CHECKING is not required.
These aliases fall back to Promise or Union[str, Promise]
when not TYPE_CHECKING.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Avoid getting stuck in an invariance pit. I don't think it makes sense
to mix two tuple with named group elements in same choices sequence(?).
This also changes the outermost container type to `Sequence` as e.g.
both `tuple` and `list` are supported.
* Reflect the deprecation of get_response being None.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Type get_response with a callback protocol.
Otherwise, calling `self.get_response(request)` in a subclass of
`MiddlewareMixin` runs into `Invalid self argument` error.
This is a workaround for https://github.com/python/mypy/issues/5485.
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
The return type for calling `shorcuts.render` without providing a value
for the `permanent` kwarg was `HttpResponsePermanentRedirect`, while it
should be `HttpResponseRedirect`.
The reason is that the first two overloads of the type stub overlap for
the case of using the default argument. While `mypy` does issue an error
for this, it was previously ignored with the `# type: ignore` comment.
As the first overload annotates the function as having the return type
`HttpResponsePermanentRedirect`, this would make mypy assume that the
return type is that instead of `HttpResponseRedirect`.
Since calling `django.shortcuts.redirect` without providing an argument
for `permanent` is the same as calling it with a `Literal[False]`, as
the default value is a `False`, we can improve the stub by only
specifying the option to use the default argument (`= ...`) in the
second overload. This also removes the overlap in stub definitions,
meaning that the `# type: ignore` can now be removed.
This commit fixes#1138.
* 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>
* 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>