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
Self-referenced ClassVar causes TypeError: issubclass() arg 1 must be a class (in 1.9.0, worked in 1.8.2) #3679
from __future__ importannotationsimportpydanticimporttypingclassWithClassVar(pydantic.BaseModel):
Instances : typing.ClassVar[dict[str,WithClassVar]] = {} # fails with issubclass() arg 1 must be a class; arg 1 is typing.ClassVar object # Instances : typing.ClassVar[dict[str,'WithClassVar']] = {} # works# instance : dict[str,WithClassVar] = {} # no classvar - works
...
I believe this case is specific to a ClassVar referencing itself. The argument passed to issubclass is not a class, but an instance of typing.ClassVar.
Now, it doesn't work even when quoted, this "workaround" was ok before:
from __future__ importannotationsimportpydanticimporttypingclassWithClassVar(pydantic.BaseModel):
Instances : typing.ClassVar[dict[str,WithClassVar]] = {} # fails with issubclass() arg 1 must be a class; arg 1 is typing.ClassVar object # Instances : typing.ClassVar[dict[str,'WithClassVar']] = {} # worked in 1.9, stopped working in 1.10.2# instance : dict[str,WithClassVar] = {} # no classvar - works
Activity
ajkaijanaho commentedon Jan 18, 2022
I get the same error with the following variant:
However, if I remove the first line (importing annotations), the example seems to work.
The following class definition passes initial muster
... but if I try to construct an instance, I get a problem:
leads to
guard against ClassVar in fields, fix #3679
guard against ClassVar in fields (#4064)
Fetch upstream
guard against ClassVar in fields (#4064)
fix ClassVars, better fix for #3679
fix ClassVars, better fix for #3679 (#4077)
guard against ClassVar in fields (pydantic#4064)
fix ClassVars, better fix for pydantic#3679 (pydantic#4077)
mcgfeller commentedon Dec 5, 2022
Unfortunately, this stopped working again on 1.10.2 with Python 3.11:
Now, it doesn't work even when quoted, this "workaround" was ok before:
Workaround for pydantic/pydantic#3679 (comment)