Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
glyph opened this issue Aug 30, 2018 · 7 comments

Comments

@glyph
Copy link

glyph commented Aug 30, 2018

Consider the following example:

from typing import TypeVar, Generic
import attr

class V:
    pass

class W:
    pass

T = TypeVar("T", V, W)

@attr.s(auto_attribs=True)
class A(Generic[T]):
    v: T

reveal_type(A(W()))

My expectation here would be a revealed type of A[W*], which I do get.

But I also get an error on the @attr.s definition:

The erased type of self 'V*' is not a supertype of its class 'A[T`1]'
The erased type of self 'W*' is not a supertype of its class 'A[T`1]'

if I remove the constraints on T, (i.e. T = TypeVar("T")) then the errors go away.

@gvanrossum
Copy link
Member

Maybe the attrs plugin author (@euresti) can shed some insight?

@ilevkivskyi
Copy link
Member

This may be related to #5263 (if not a duplicate).

@ilevkivskyi
Copy link
Member

Just to clarify, both issues may be caused by bad interaction of patched self/cls with checkmember.bind_self().

@euresti
Copy link
Contributor

euresti commented Aug 31, 2018

Actually. I can repro with dataclasses too.

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()))

So it's related to the added __cmp-ish__ methods

@ilevkivskyi
Copy link
Member

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.

@euresti
Copy link
Contributor

euresti commented 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
Copy link
Member

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)?

euresti added a commit to euresti/mypy that referenced this issue Sep 1, 2018
By convention TypeVars for methods should have negative numbers.  Since we're creating these methods we're guaranteed
to have -1 free.
Fixes python#5542
euresti added a commit to euresti/mypy that referenced this issue Sep 2, 2018
By convention TypeVars for methods should have negative numbers.  Since we're creating these methods we're guaranteed
to have -1 free.
Fixes python#5542
ilevkivskyi pushed a commit that referenced this issue Sep 3, 2018
The issue was caused because we were using 1 for the ids of the generated types.
Since by convention TypeVars for methods should have negative numbers and we're creating these methods we're guaranteed to have -1 free. So use that instead.

Fixes #5542
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants