-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make pyright recognise narrowing down a type using a in-place set #3022
Comments
This won't work (at least not without some really fragile special-case logic) because, unlike tuples, the inferred type of a set doesn't preserve literals. x1 = (1, 3)
reveal_type(x1) # tuple[Literal[1], Literal[3]]
x2 = {1, 3}
reveal_type(x2) # set[int] If you want to use this type narrowing pattern in your code, you'd need to switch from sets to tuples. |
Is that by design? Or can a proposal be made to retain the literal types within a set? The second example is also non-relevant I assume? |
That's by design. You really wouldn't want a type checker to infer the type of If you really want to use a set here for some reason, you could do the following: def func_two(param: int) -> Literal[1, 3]:
x: set[Literal[1, 3]] = {1, 3}
assert param in x
reveal_type(param)
return param
`` |
Hmm okay. I ran into this since I can see how sets defined anywhere else shouldn't be inferred to |
Is your feature request related to a problem? Please describe.
Describe the solution you'd like
Please correct me if I'm wrong, but shouldm't the type also be narrowed down to
Literal[1,3]
in the second function?Additional context
Perhaps this is trickier because it is a tuple with a single value and there can be some edge-cases I can't immediately think of, but shouldn't this also narrow down to
Literal[1]
likefunc
does?The text was updated successfully, but these errors were encountered: