0

I am trying to create a dynamic model using pydantic but it seems it can't get even the basic example:

from pydantic import BaseModel, create_model
MyModel = create_model('MyModel', foo="foo")

The error is:

MyModel = create_model('MyModel', foo="foo")
  File "pydantic/main.py", line 972, in pydantic.main.create_model
  File "pydantic/main.py", line 228, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/fields.py", line 488, in pydantic.fields.ModelField.infer
  File "pydantic/fields.py", line 419, in pydantic.fields.ModelField.__init__
  File "pydantic/fields.py", line 539, in pydantic.fields.ModelField.prepare
  File "pydantic/fields.py", line 801, in pydantic.fields.ModelField.populate_validators
  File "pydantic/validators.py", line 682, in find_validators
  File "pydantic/dataclasses.py", line 82, in pydantic.dataclasses.is_builtin_dataclass
AttributeError: module 'dataclasses' has no attribute 'is_dataclass'

Since I am using poetry, the command I used is:

poetry run python main.py

Anyone has any idea why it gets such an error?

derek
  • 9,358
  • 11
  • 53
  • 94
  • Do you have the required dependencies? From [the documentation](https://pydantic-docs.helpmanual.io/install/): "pydantic has no required dependencies except python 3.6, 3.7, 3.8, 3.9 or 3.10, `typing-extensions`, and the `dataclasses` backport package for python 3.6". – Hernán Alarcón Mar 06 '22 at 03:22
  • I am using python 3.10.1 so I think I am good for this regard. – derek Mar 06 '22 at 03:42

1 Answers1

0

THe reason is because I have another file dataclasses.py in the same folder as main.py and poetry run python main.py used my local datacalsses.py automatically and thus override the system dataclasses somehow. A good lesson here is do not use class names as your local module name.

derek
  • 9,358
  • 11
  • 53
  • 94