phillipdupuis / pydantic-to-typescript

CLI Tool for converting pydantic models into typescript definitions
MIT License
285 stars 48 forks source link

AttributeError: type object 'Config' has no attribute 'extra' #3

Closed rbeard0330 closed 3 years ago

rbeard0330 commented 3 years ago

Minimal example:

# main.py

from pydantic import BaseModel

class Foo(BaseModel):
    bar: str

    class Config:
        frozen = True
>> pydantic2ts --module main.py
[traceback omitted]
File \lib\site-packages\pydantic2ts\cli\script.py", line 145, in <listcomp>
    model_extras = [m.Config.extra for m in models]
AttributeError: type object 'Config' has no attribute 'extra'

It appears that when you set Config options on a Pydantic model, the default options are not being added to the model's Config class. (I'm not exactly sure why--Pydantic has a bunch of metaclass razzmatazz that seems like it should populate subclass Configs, but it doesn't seem to be happening?) As a result, if you set some options, but don't set frozen, then the script throws an error when it tries to look up the frozen value. I think the fix is as easy as replacing all m.Config.extra with getattr(m.Config, 'extra', Extra.ignore), and doing that worked for my case, but I didn't do enough homework to feel confident that this wouldn't break something else.

phillipdupuis commented 3 years ago

Sorry for the slow response! I think you’re absolutely right, and great catch

I’ll get on that PR tomorrow, thank you for submitting it!