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
I recently tried and failed to get some code like this working:
fromtypingimportCollection, TypeVarT=TypeVar('T')
defcheck_contained(x: object, y: Collection[T]) ->T:
ifxnotiny:
raiseRuntimeErrorreturnx# Incompatible return value type (got "object", expected "T")
At first, I thought that this is definitely wrong, but then I realized: okay, in theory in is implemented based on __eq__ and we could be in a situation where x compares equal to a T-typed value in y without being T itself (for example, if T is bool, y is [True] and x is 1). The error message could definitely be more helpful in this case, of course...
But consider this case:
defcheck_str_contained(x: object, y: Collection[str]) ->str:
ifxnotiny:
raiseRuntimeErrorreturnx# Incompatible return value type (got "object", expected "str")
I really can't figure out a reasonable counterexample here. The only way we're going to have x in y is if x is either a str or a subclass of str. I suppose we might imagine that y contains str subclasses which implement strange __eq__ rules that return true on comparison to non-str values...
And indeed, this also fails:
defcheck_eq(x: object, y: str) ->str:
ifx!=y:
raiseRuntimeErrorreturnx# Incompatible return value type (got "object", expected "str")
and, taken to the extreme, this too:
defcheck_boolstr(x: object, y: Collection[Literal["yes"] |Literal["no"]]) ->str:
ifxnotiny:
raiseRuntimeErrorreturnx# Incompatible return value type (got "object", expected "str")
That's a shame. This would be a very useful feature, particularly on generics. Maybe we can figure out a way forward here...
The text was updated successfully, but these errors were encountered:
I recently tried and failed to get some code like this working:
At first, I thought that this is definitely wrong, but then I realized: okay, in theory
in
is implemented based on__eq__
and we could be in a situation wherex
compares equal to aT
-typed value iny
without beingT
itself (for example, ifT
isbool
,y
is[True]
andx
is1
). The error message could definitely be more helpful in this case, of course...But consider this case:
I really can't figure out a reasonable counterexample here. The only way we're going to have
x in y
is ifx
is either astr
or a subclass ofstr
. I suppose we might imagine thaty
containsstr
subclasses which implement strange__eq__
rules that return true on comparison to non-str
values...And indeed, this also fails:
and, taken to the extreme, this too:
That's a shame. This would be a very useful feature, particularly on generics. Maybe we can figure out a way forward here...
The text was updated successfully, but these errors were encountered: