team23 / pydantic-partial

Create partial models from pydantic models
MIT License
51 stars 8 forks source link

Unexpected class name returned by representation #3

Closed khssnv closed 2 years ago

khssnv commented 2 years ago

A repr call on a regular pydantic model returns a string like ModelName(param=value). For a model created by pydantic_partial.create_partial_model it returns ParentModelNamePartial(param=value) instead and misses the model name.

$ ipython
In [1]: import pydantic, pydantic_partial

In [2]: class Foo(pydantic.BaseModel):
   ...:     a: int
   ...:     b: str
   ...: 

In [3]: repr(Foo(a=1, b="b"))
Out[3]: "Foo(a=1, b='b')"

In [4]: Bar = pydantic_partial.create_partial_model(Foo)

In [5]: repr(Bar(a=1))
Out[5]: "FooPartial(a=1, b=None)"

In [6]: class Baz(Foo):
   ...:     pass
   ...: 

In [7]: repr(Baz(a=1, b="b"))
Out[7]: "Baz(a=1, b='b')"
ddanier commented 2 years ago

If you want to give it the name "Bar" you have to use it like this:

class Bar(pydantic_partial.create_partial_model(Foo)):
    pass

Otherwise the function create_partial_model will create a new model named FooPartial which you stored under the variable name Bar. Thats just how Python works actually 😉

I could add a parameter name to the create_partial_model function so you could do something like:

Bar = pydantic_partial.create_partial_model(Foo, name="Bar")

...would that follow what create_model in pydantic allows. What do you think?

ddanier commented 2 years ago

As no further questions did come up - I will close this issue.

khssnv commented 2 years ago

Thank you for explanation. Inheriting from a partial model works for me :+1: