Closed dcnieho closed 1 month ago
are you trying to create new type alias by t = typing.Literal[values]
? or did you mean to do t: typing.Literal[...]
?
for this
def get_values() -> tuple[int]:
return (1,2,3)
did you mean def get_values() -> tuple[int, ...]
?
same for t1 = typing.Literal[get_values()]
....
that said, see https://docs.python.org/3/library/typing.html#typing.Literal and https://peps.python.org/pep-0586/#legal-parameters-for-literal-at-type-check-time
at runtime, what you have might not error out, but it won't have any meaning at runtime?
see this At runtime, an arbitrary value is allowed as type argument to Literal[...], but type checkers may impose restrictions
Thanks for the quick reply! I do indeed mean t = typing.Literal[values]
, in my actual code i am constructing the literal type object with a set of accepted values only known at run time, and then use it for runtime type checking.
If the type check warnings are expected at type check time (I indeed do not see a tuple of arguments being flattened mentioned in the docs), then i guess this can be closed.
Could someone from the pylance team please transfer this to the pyright project? Since this is a value expression and not a type expression, rules for type expressions shouldn't be enforced.
This is addressed in pyright 1.1.376.
Environment data
Code Snippet
Repro Steps
put code in empty .py file, see Pylance analysis output
Expected behavior
No squiggles, Pylance correctly deduces t as typing.Literal['a','b','c'] and t1 as typing.Literal[1,2,3] (or, given that that is runtime, at least no squiggle)
Actual behavior
When run, code works correctly (you can instantiate a typing.Literal with a tuple of values), but Pylance provides yellow squiggles under
values
andget_values()
with the following errors:and
respectively