Skip to content

Commit 54a1e89

Browse files
authored
Add FAQ entry regarding StrPromise to README (#1320)
* fix markdownlint violations in README * add FAQ entry regarding StrPromise
1 parent 6d883cd commit 54a1e89

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
[![Gitter](https://badges.gitter.im/mypy-django/Lobby.svg)](https://gitter.im/mypy-django/Lobby)
88
[![StackOverflow](https://shields.io/badge/ask-stackoverflow-orange?logo=stackoverflow)](https://stackoverflow.com/questions/tagged/django-stubs)
99

10-
1110
This package contains [type stubs](https://www.python.org/dev/peps/pep-0561/) and a custom mypy plugin to provide more precise static types and type inference for Django framework. Django uses some Python "magic" that makes having precise types for some code patterns problematic. This is why we need this project. The final goal is to be able to get precise types for most common patterns.
1211

13-
1412
## Installation
1513

1614
```bash
@@ -68,7 +66,6 @@ We rely on different `django` and `mypy` versions:
6866
| 1.1.0 | 0.720 | 2.2.x | ^3.6
6967
| 0.12.x | old semantic analyzer (<0.711), dmypy support | 2.1.x | ^3.6
7068

71-
7269
## FAQ
7370

7471
### Is this an official Django project?
@@ -123,11 +120,12 @@ This happens because these Django classes do not support [`__class_getitem__`](h
123120

124121
### How can I create a HttpRequest that's guaranteed to have an authenticated user?
125122

126-
Django's built in [`HttpRequest`](https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.HttpRequest) has the attribute `user` that resolves to the type
123+
Django's built in [`HttpRequest`](https://docs.djangoproject.com/en/4.1/ref/request-response/#django.http.HttpRequest) has the attribute `user` that resolves to the type
127124

128125
```python
129126
Union[User, AnonymousUser]
130127
```
128+
131129
where `User` is the user model specified by the `AUTH_USER_MODEL` setting.
132130

133131
If you want a `HttpRequest` that you can type-annotate with where you know that the user is authenticated you can subclass the normal `HttpRequest` class like so:
@@ -143,7 +141,6 @@ class AuthenticatedHttpRequest(HttpRequest):
143141

144142
And then use `AuthenticatedHttpRequest` instead of the standard `HttpRequest` for when you know that the user is authenticated. For example in views using the `@login_required` decorator.
145143

146-
147144
### My QuerySet methods are returning Any rather than my Model
148145

149146
If you are using `MyQuerySet.as_manager()`:
@@ -208,9 +205,7 @@ class MyManager(model.Manager):
208205

209206
will cause this error message:
210207

211-
```
212-
error: Return type "MyModel" of "create" incompatible with return type "_T" in supertype "BaseManager"
213-
```
208+
> error: Return type "MyModel" of "create" incompatible with return type "_T" in supertype "BaseManager"
214209
215210
This is happening because the `Manager` class is generic, but without
216211
specifying generics the built-in manager methods are expected to return the
@@ -296,6 +291,13 @@ def foo(obj: object) -> None:
296291
...
297292
```
298293

294+
### Why am I getting incompatible argument type mentioning `_StrPromise`?
295+
296+
The lazy translation functions of Django (such as `gettext_lazy`) return a `Promise` instead of `str`. These two types [cannot be used interchangeably](https://github.com/typeddjango/django-stubs/pull/1139#issuecomment-1232167698). The return type of these functions was therefore [changed](https://github.com/typeddjango/django-stubs/pull/689) to reflect that.
297+
298+
If you encounter this error in your own code, you can either cast the `Promise` to `str` (causing the translation to be evaluated), or use the `StrPromise` or `StrOrPromise` types from `django-stubs-ext` in type hints. Which solution to choose depends depends on the particular case. See [working with lazy translation objects](https://docs.djangoproject.com/en/4.1/topics/i18n/translation/#working-with-lazy-translation-objects) in the Django documentation for more information.
299+
300+
If this is reported on Django code, please report an issue or open a pull request to fix the type hints.
299301

300302
## Related projects
301303

@@ -304,8 +306,6 @@ def foo(obj: object) -> None:
304306
- [`pytest-mypy-plugins`](https://github.com/typeddjango/pytest-mypy-plugins) - `pytest` plugin that we use for testing `mypy` stubs and plugins.
305307
- [`wemake-django-template`](https://github.com/wemake-services/wemake-django-template) - Create new typed Django projects in seconds.
306308

307-
308-
309309
## To get help
310310

311311
We have Gitter here: <https://gitter.im/mypy-django/Lobby>

0 commit comments

Comments
 (0)