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
from __future__ importannotationsfromcollections.abcimportSizedfromtypingimportGeneric, TypeVarT=TypeVar("T")
classG(Generic[T]):
pass
I want to have a single method on this class that's only legal for a subset of T.
If I want to limit it to, say, int, it works:
classG(Generic[T]):
deftest(self: G[int]) ->G[int]:
returnselfG[int]().test() # Works, as it shouldG[str]().test() # Error, as it should
But if I want to limit it to, say, a protocol like Sized, I get a false negative:
S=TypeVar("S", bound=Sized)
classG(Generic[T]):
deftest(self: G[S]) ->G[S]:
returnselfG[int]().test() # False negative - works, but it shouldn't
This is a pity, since it'd unlock some cool features I was working on vis-a-vis using attrs attributes. Are there any workarounds? Would this be handled by intersection types maybe?
Your Environment
Mypy version used: mypy 1.0.0+dev.b8c03ab6809aab56928f3cd865edb44944a600a2
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
Python version used: Python 3.10.6
The text was updated successfully, but these errors were encountered:
Bug Report
Let's say I have a generic class
G
.I want to have a single method on this class that's only legal for a subset of
T
.If I want to limit it to, say,
int
, it works:But if I want to limit it to, say, a protocol like
Sized
, I get a false negative:This is a pity, since it'd unlock some cool features I was working on vis-a-vis using attrs attributes. Are there any workarounds? Would this be handled by intersection types maybe?
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: