- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 515
 
Closed
Labels
bugSomething isn't workingSomething isn't workingstubsIssues in stubs files (.pyi)Issues in stubs files (.pyi)
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 filelet 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:
 pythonversion: 3.8.16djangoversion: (old, lol)mypyversion: 1.4.1django-stubsversion: 4.2.3django-stubs-extversion: 4.2.2
intgr
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingstubsIssues in stubs files (.pyi)Issues in stubs files (.pyi)