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