Closed
Description
Bug report
it appears that the _StrOrPromiseT
in capfirst
decays to Sequence[str]
-- probably because mypy infers the union of str | _StrPromise
to be Sequence[str]
(because str
is also a Sequence[str]
)
What's wrong
the original error comes from this line -- though I've isolated the bug to a standalone script: https://github.com/getsentry/sentry/blob/c19f762c70afa9145efb10e8a524498316dcea4e/src/sentry/web/forms/accounts.py#L79
here's the standalone version:
from __future__ import annotations
from typing import TYPE_CHECKING
from django.utils.translation import gettext_lazy
from django.utils.text import capfirst
if TYPE_CHECKING:
from django.utils.functional import _StrPromise
s: _StrPromise | str = gettext_lazy('example')
t = capfirst(s)
reveal_type(t)
$ mypy t2.py
t2.py:10: error: Value of type variable "_StrOrPromiseT" of "capfirst" cannot be "Sequence[str]" [type-var]
t2.py:11: note: Revealed type is "Union[typing.Sequence[builtins.str], None]"
Found 1 error in 1 file (checked 1 source file)
How is that should be
if I change this instead to an overload it appears to work correctly:
from __future__ import annotations
from typing import TYPE_CHECKING, overload
from django.utils.translation import gettext_lazy
from django.utils.text import capfirst
if TYPE_CHECKING:
from django.utils.functional import _StrPromise
@overload
def capfirst2(x: None) -> None: ...
@overload
def capfirst2(x: str) -> str: ...
@overload
def capfirst2(x: _StrPromise) -> _StrPromise: ...
def capfirst2(x: str | _StrPromise | None) -> str | _StrPromise | None: return x
s: _StrPromise | str = gettext_lazy('example')
t = capfirst2(s)
reveal_type(t)
$ mypy t.py
t.py:21: note: Revealed type is "Union[django.utils.functional._StrPromise, builtins.str]"
Success: no issues found in 1 source file
let me know if you'd be interested in a patch for this or if there's some other ideas on how to fix this
System information
- OS:
python
version: 3.8.16django
version: (old, lol)mypy
version: 1.4.1django-stubs
version: 4.2.3django-stubs-ext
version: 4.2.2