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

TypeVar constrained to Literal[True] and Literal[False] is resolved to bool #6917

Closed
Viicos opened this issue Jan 5, 2024 · 4 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Viicos
Copy link

Viicos commented Jan 5, 2024

Following #6914:

Code sample in pyright playground

from typing import TypeVar, Literal, Generic

T = TypeVar("T", Literal[True], Literal[False])


class A(Generic[T]):
    def __init__(self, null: T = False) -> None:
        pass


a = A(null=False)

reveal_type(a)  # Should be A[Literal[False]]
@Viicos Viicos added the bug Something isn't working label Jan 5, 2024
erictraut added a commit that referenced this issue Jan 6, 2024
…TypeVar includes literal types as constraints. This addresses #6917.
erictraut added a commit that referenced this issue Jan 6, 2024
…TypeVar includes literal types as constraints. This addresses #6917. (#6929)
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 6, 2024
@Viicos
Copy link
Author

Viicos commented Jan 7, 2024

Thanks for the fix! I see in the PR that you are special casing literals. Should this be added to the spec in some way? (for the record mypy is not inferring the case without an explicit null value provided correctly: playgroud)

@erictraut
Copy link
Collaborator

Should this be added to the spec in some way?

Maybe, although in this case I don't think there's any ambiguity because the constraints imposed on the TypeVar (in this case, value constraints) require that the solved type is a literal. The issue with mypy in this case is not because it doesn't solve T as a literal but because it doesn't support specifying a default argument for a generic parameter. I'm somewhat surprised that the maintainers of mypy haven't addressed this already since it's one of the most upvoted mypy feature requests.

@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.345, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants