Skip to content

Support for short-circuit evaluation #17253

Closed as not planned
Closed as not planned
@mmzeynalli

Description

@mmzeynalli

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

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions