-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for short-circuit evaluation #17253
Comments
Mypy is technically correct here; it could be a subclass of Token that overrides A bit arcane but I think this sort of pattern is better avoided in type-safe code. |
Consistent with Jelle's insight, the behavior changes if you mark the |
Indeed, thanks @erictraut: https://mypy-play.net/?mypy=latest&python=3.10&gist=c808a9a03bd113724835d66cccdd062e. With that I don't think there's anything left to do here; mypy's behavior is correct and |
I see, |
Okay, I have a little different circumstance, which gives the same error: from typing import final
from fastapi import Depends
from pydantic import BaseModel
@final
class Token(BaseModel):
username: str
exp: Optional[int] = None
user_type: int
OptionalAuthUser = Annotated[Token | None, Depends(get_optional_user_access)]
@router.get(
'',
summary='Get portfolio list',
status_code=status.HTTP_200_OK,
)
async def get_all_profiles_portfolios(
db: DbDependency,
auth: OptionalAuthUser,
):
builder = PortfolioQueryBuilder(user_username=auth and auth.username)
'''rest of the code''' How should I act here, considering Token if wrapped into Annotated? |
You use |
My bad, fixed it. I did that before, but then I changed the code to as shown, however mypy complains. It does not hinder my work at all, but I would still like to know how to handle this, if I were to use short-circuit. |
@mmzeynalli I don't see any error, maybe you could provide a more complete example? from typing import final, Optional, Annotated
from fastapi import Depends
from pydantic import BaseModel
@final
class Token(BaseModel):
username: str
exp: Optional[int] = None
user_type: int
def get_optional_user_access(): ...
OptionalAuthUser = Annotated[Token | None, Depends(get_optional_user_access)]
async def get_all_profiles_portfolios(
auth: OptionalAuthUser,
):
builder: str | None = auth and auth.username
'''rest of the code'''
|
Bug Report
Mypy fails to evaluate variable type in short-circuit evaluation.
To Reproduce
Expected Behavior
It should not give an error, as line
auth and auth.username
always returns eitherNone
or username string, because of short-circuit evaluation.Actual Behavior
When I run mypy, I get this error:
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: