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
{{ message }}
Repository navigation
attrs plugin error: "The erased type of self '...*' is not a supertype of its class" on class definition with a generic using a constrained type variable, but then seems to work fine after definition #5542
from typing import TypeVar, Generic
import attr
from dataclasses import dataclass
class V:
pass
class W:
pass
T = TypeVar("T", V, W)
@dataclass(order=True)
class A(Generic[T]):
v: T
reveal_type(A(W()))
OK, the problem is here and here where we unconditionally grab id=1 for the self-type type variable thus causing a type variable clash (the same probably happens for attrs). I think the solution would be to check somehow the next available id instead.
Also btw by agreement ids for generic functions/methods should have negative numbers.
Would the solution just be to make the id -1? It seems to me like -1 should never be used at this point because negative numbers are used for methods/functions and I'm at this point creating it right? Or are the id's global in scope?
After some thinking it seems to me just using -1 should be fine since the methods are auto-generated anyway. Btw, I have found that senamal_namedtuple also uses 1 instead of -1. Could you please try replacing it by -1 in both plugins and in named tuple code and play with it trying to break (try some nested classes within generic functions, maybe some scenarios with type inference)?
Activity
gvanrossum commentedon Aug 30, 2018
Maybe the attrs plugin author (@euresti) can shed some insight?
ilevkivskyi commentedon Aug 30, 2018
This may be related to #5263 (if not a duplicate).
ilevkivskyi commentedon Aug 30, 2018
Just to clarify, both issues may be caused by bad interaction of patched
self/clswithcheckmember.bind_self().euresti commentedon Aug 31, 2018
Actually. I can repro with dataclasses too.
So it's related to the added
__cmp-ish__methodsilevkivskyi commentedon Aug 31, 2018
OK, the problem is here and here where we unconditionally grab
id=1for the self-type type variable thus causing a type variable clash (the same probably happens forattrs). I think the solution would be to check somehow the next available id instead.Also btw by agreement ids for generic functions/methods should have negative numbers.
euresti commentedon Aug 31, 2018
Would the solution just be to make the id
-1? It seems to me like -1 should never be used at this point because negative numbers are used for methods/functions and I'm at this point creating it right? Or are the id's global in scope?ilevkivskyi commentedon Sep 1, 2018
After some thinking it seems to me just using
-1should be fine since the methods are auto-generated anyway. Btw, I have found thatsenamal_namedtuplealso uses1instead of-1. Could you please try replacing it by-1in both plugins and in named tuple code and play with it trying to break (try some nested classes within generic functions, maybe some scenarios with type inference)?Avoid type-var collisions by using -1 instead of 1.
Avoid type-var collisions by using -1 instead of 1.
Avoid type-var collisions in attrs and dataclasses (#5552)