You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type checker may use context to infer an invalid type variable value for a type variable such as AnyStr:
from typing import AnyStr
def f(x: AnyStr) -> AnyStr:
if isinstance(x, str):
return 'foo'
else:
return b'zar'
print(repr(f(''))) # Type argument 1 of "f" has incompatible value "object"
We should probably not use type context in cases like this when the type variable is not used within a generic type.
The text was updated successfully, but these errors were encountered:
The type variable AnyStr is restricted to str or bytes. So, it seems to me that the problem here is that the return type of f is inferred to be object, which is wider than AnyStr.
I think the context should be used to restrict the possible values of the type var, instead of replacing it like seems to happen now.
So in the above example, repr expects an object, so that would not restrict AnyStr. However, in this continuation of the example:
defg(s: str): passg(f(''))
The context (str) would be used to restrict (and uniquely determine in this case) AnyStr to str.
The type checker may use context to infer an invalid type variable value for a type variable such as AnyStr:
We should probably not use type context in cases like this when the type variable is not used within a generic type.
The text was updated successfully, but these errors were encountered: