Closed as not planned
Description
Bug Report
Mypy fails to evaluate variable type in short-circuit evaluation.
To Reproduce
class PortfolioQueryBuilder:
def __init__(self, user_username: Optional[str] = None) :
self.user_username = user_username
def build_query(self):
'''rest of the code/handling here'''
class Token:
def __init__(self, username: str):
self.username = username
auth = Token('username')
# auth = None
builder = PortfolioQueryBuilder(user_username=auth and auth.username)
Expected Behavior
It should not give an error, as line auth and auth.username
always returns either None
or username string, because of short-circuit evaluation.
Actual Behavior
When I run mypy, I get this error:
error: Argument "user_username" to "PortfolioQueryBuilder" has incompatible type "Token | str | None"; expected "str | None" [arg-type]
Your Environment
- Mypy version used: mypy 1.9.0 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files):
[tool.mypy]
plugins = ["sqlalchemy.ext.mypy.plugin", "pydantic.mypy"]
exclude = [
"^admin\\.py$"
]
- Python version used: 3.11.5