-
for t in [str, float]:
def f(x: str) -> t:
return t(x) + 1
print(f("3.14")) Pyright finds no errors in this. Is there different type syntax I should use or is this outside of Pyright's scope? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Yeah, this should be flagged as an error. The challenge for a type checker is in distinguishing between an implicit type alias and variables that are not meant to be type aliases. For example, this is legal and the use of u = str
for t in [str, float]:
def f(x: str) -> u:
return t(x) + 1
print(f("3.14")) Pyright uses some heuristics that attempt to distinguish between a valid (implicit) type alias and a normal variable. Those heuristics are failing in this case, resulting in a false negative. I'll clone this discussion as a bug report. |
Beta Was this translation helpful? Give feedback.
-
After the change, the following generates a type error t: type[str] | type[float]
for t in [str, float]:
def f(s: str) -> t: # error: Illegal type annotation...
return t(s)
print(f("3.14")) However, the following does not def outer(t: type[str] | type[float]):
def f(s: str) -> t:
return t(s)
print(f("3.14"))
t: type[str] | type[float]
for t in [str, float]:
outer(t) (And indeed if we have So @erictraut, should function arguments also be illegal or is this a valid use of types? |
Beta Was this translation helpful? Give feedback.
Yeah, this should be flagged as an error.
The challenge for a type checker is in distinguishing between an implicit type alias and variables that are not meant to be type aliases.
For example, this is legal and the use of
u
in this type annotation should not be flagged as an error.Pyright uses some heuristics that attempt to distinguish between a valid (implicit) type alias and a normal variable. Those heuristics are failing in this case, resulting in a false negative.
I'll clone this discussion as a bug report.