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

Literal[False] + Literal[True] does not full cover bool type? #8135

Closed
Holt59 opened this issue Jun 13, 2024 · 2 comments
Closed

Literal[False] + Literal[True] does not full cover bool type? #8135

Holt59 opened this issue Jun 13, 2024 · 2 comments
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@Holt59
Copy link

Holt59 commented Jun 13, 2024

Describe the bug

When a function has two overloads, one taking Literal[True] and the other Literal[False], pyright throws an error when trying to call it with a bool value or even with a Literal[True, False] value.

Changing bool in the implementation of f1 below to Literal[True, False] does not change the behavior. Using non-bool literals does not produce the issue.

Code or Screenshots

from typing import Literal, overload

@overload
def f1(c: Literal[False] = ...) -> str: ...

@overload
def f1(c: Literal[True] = ...) -> int: ...

def f1(c: bool = False) -> str | int: ...

def r_bool() -> bool: ...
def r_literal() -> Literal[True, False]: ...

f1(r_bool())  # no overload matches for f1
f1(r_literal())  # no overload matches for f1

VS Code extension or command-line

Both, pyright CLI is 1.1.367

I do not remember seeing that problem before so maybe it is a recent update.

@Holt59 Holt59 added the bug Something isn't working label Jun 13, 2024
@erictraut
Copy link
Collaborator

This is a duplicate of #5230.

@erictraut erictraut added the as designed Not a bug, working as intended label Jun 13, 2024
@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Jun 13, 2024
@Holt59
Copy link
Author

Holt59 commented Jun 13, 2024

This still raises an error and there is no use of bool

from typing import Literal, overload

def f1(c: Literal[False] = ...) -> str: ...
@overload
def f1(c: Literal[True] = ...) -> int: ...
def f1(c: Literal[True, False] = ...) -> str | int: ...

def r_literal() -> Literal[True, False]: ...

f1(r_literal())

In the linked issue you said

Pyright doesn't expand bool into Literal[True, False] in this case. That's an intentional decision, as expansion of this sort would be computationally expensive in a hot path within the type analyzer.

Which I understand, but if you do not expand bool into Literal[True, False], why collapse Literal[True, False] into bool (in the above exxample r_literal() is deduced to return bool)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants