from typing import Literal
def predict(window: Literal[24, 48]):
pass
for window in [24, 48]:
predict(window=window)
Let's say I have the above code. I'm trying to type annotate my code. However Pylance says it's incorrect:
Argument of type "int" cannot be assigned to parameter "prediction_range" of type "Literal[24, 48, 168]" in function "predictor" Type "int" cannot be assigned to type "Literal[24, 48]" "int" cannot be assigned to type "Literal[24]" "int" cannot be assigned to type "Literal[48]"
I don't understand why the int 24 cannot be assigned to Literal int with the same value. What is the correct way to annotate this code?
CodePudding user response:
[24, 48]
is deduced as list[int]
. The information about specific values is lost.
If you want to specify a different type, you can:
from typing import Literal
def predict(window: Literal[24, 48]):
pass
windows: list[Literal[24, 48]] = [24, 48]
for window in windows:
predict(window=window)