Skip to content

Commit 6596039

Browse files
committed
add FAQ entry regarding StrPromise
1 parent c84e7f0 commit 6596039

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ This happens because these Django classes do not support [`__class_getitem__`](h
120120

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

123-
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
124124

125125
```python
126126
Union[User, AnonymousUser]
@@ -291,6 +291,14 @@ def foo(obj: object) -> None:
291291
...
292292
```
293293

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.
301+
294302
## Related projects
295303

296304
- [`awesome-python-typing`](https://github.com/typeddjango/awesome-python-typing) - Awesome list of all typing-related things in Python.

0 commit comments

Comments
 (0)