* 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>
* 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
* 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>
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.
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>