For reasons beyond the scope of this question, I'd like to create a dynamic Python-Pydantic class. I'm close, but am not aware of how to add the type hint.
Given:
class MyClass(BaseModel):
class Config:
extra = Extra.allow
schema_extra = {
'$id': "http://my-site.com/production.json",
'$schema': "https://json-schema.org/draft/2020-12/schema",
'title': 'pattern="^.+-.*"'
}
if __name__ == '__main__':
cls = type('A', (MyClass,), {'__doc__': 'class created by type', 'my_field': Field("test", type='int')})
p = cls()
schema = p.schema()
pprint(schema)
I get this:
{'$id': 'http://my-site.com/production.json',
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'description': 'class created by type',
'properties': {'my_field': {'default': 'test',
'title': 'My Field',
'type': 'string'}},
'title': 'pattern="^.+-.*"',
'type': 'object'}
I would like "properties -> myfield -> type" to be an int instead of string.