Skip to content
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

type not narrowed within function when it's Final #12006

Open
DetachHead opened this issue Jan 18, 2022 · 3 comments
Open

type not narrowed within function when it's Final #12006

DetachHead opened this issue Jan 18, 2022 · 3 comments
Labels
bug mypy got something wrong topic-final PEP 591

Comments

@DetachHead DetachHead added the bug mypy got something wrong label Jan 18, 2022
@erictraut
Copy link

I think the type should be narrowed on assignment to Literal[1], so the if foo is not None conditional check shouldn't even be necessary in this example.

@DetachHead
Copy link
Contributor Author

i know typescript behaves that way however i'm not really a fan of that behavior, i think the explicit type annotation should widen it, for the same reason that i wouldn't expect this to be narrowed just because of the return value:

def foo() -> int | None:
    return 1

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jan 19, 2022

It can be frustrating in certain circumstances:

a: object = 1
print(a + 1)  # error: Unsupported operand types for + ("object" and "int")

a = "amoogus"
print(a + "SUS")

And forces you to write code that you normally wouldn't:

a: object 
a = 1
print(a + 1)  # no error: mypy happy now 😀

a = "amoogus"
print(a + "SUS")

@hauntsaninja hauntsaninja added the topic-final PEP 591 label Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-final PEP 591
Projects
None yet
Development

No branches or pull requests

4 participants