- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3k
Closed as not planned
Labels
bugmypy got something wrongmypy got something wrong
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
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong