For bugs/questions:
import sys; print(sys.version): 3.7.3import pydantic; print(pydantic.VERSION): 0.25from enum import Enum
from pydantic.dataclasses import dataclass
# this is real life example that used enums, enum is not the issue here
# same will happen with a class or otherwise
class SomeEnum(Enum):
A = 0
B = 1
@dataclass
class model:
SomeEnum: SomeEnum = SomeEnum.A
model()
The problem is caused by having the same name for the hint and class attribute.
The problem only occurs when there is a default.
I know it is brave to name class attribute same as the hint but this is what I wanted and what worked for me without pydantic.
trace:
$ python a_bug.py
Traceback (most recent call last):
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/validators.py", line 386, in find_validators
if issubclass(type_, val_type):
TypeError: issubclass() arg 1 must be a class
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "a_bug.py", line 9, in <module>
@dataclass
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/dataclasses.py", line 127, in dataclass
return wrap(_cls)
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/dataclasses.py", line 122, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, config)
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/dataclasses.py", line 85, in _process_class
cls.__name__, __config__=config, __module__=_cls.__module__, __validators__=validators, **fields
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/main.py", line 574, in create_model
return type(model_name, (__base__,), namespace) # type: ignore
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/main.py", line 197, in __new__
config=config,
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/fields.py", line 140, in infer
schema=schema,
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/fields.py", line 109, in __init__
self.prepare()
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/fields.py", line 175, in prepare
self._populate_validators()
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/fields.py", line 253, in _populate_validators
else find_validators(self.type_, self.model_config.arbitrary_types_allowed)
File "/home/bartek/.pyenv/versions/rvo/lib/python3.7/site-packages/pydantic/validators.py", line 390, in find_validators
raise RuntimeError(f'error checking inheritance of {type_!r} (type: {display_as_type(type_)})') from e
RuntimeError: error checking inheritance of <SomeEnum.A: 0> (type: enum)
For those who stumble upon this rare error, quoting the hint removes the error.
@dataclass
class model:
SomeEnum: 'SomeEnum' = SomeEnum.A
thanks for reporting this, I'm not sure it's worth fixing. Better to use alias and name the field something else pep-8 compliant.
Sorry I was being dumb, this is not a problem with pydantic, but a fundamental of python / an error in your code.
Consider the following:
class Foo:
pass
class Bar:
f: Foo = 1
debug(Bar.__annotations__)
Outputs:
test.py:7 <module>
Bar.__annotations__: {
'f': <class '__main__.Foo'>,
} (dict) len=1
Foo the annotation to f is the class Foo defined above.
However consider this case:
class Foo:
pass
class Bar:
Foo: Foo = 1
debug(Bar.__annotations__)
Outputs:
test.py:7 <module>
Bar.__annotations__: {'Foo': 1} (dict) len=1
In other words, here Foo the annotation to Foo the class property takes the value of Foo the local variable, and becomes 1.
This is not a problem with pydantic.