Open thebuchanan3 opened 2 years ago
Thanks for reporting this bug @thebuchanan3 . On which python version are you?
Thanks for reporting this bug @thebuchanan3 . On which python version are you?
3.10.4
this can probably be fixed by using the smart_union config to avoid type coercion to the first Union type:
without smart_union
:
from pydantic import BaseModel
class A(BaseModel):
item: str
class B(BaseModel):
item: str
class C(BaseModel):
item: A | B
print(C(item=B(item="a")))
# item=A(item='a')
with smart_union
:
from pydantic import BaseModel
class A(BaseModel):
item: str
class B(BaseModel):
item: str
class C(BaseModel, smart_union=True):
item: A | B
print(C(item=B(item="a")))
# item=B(item='a')
as a workaround the setting can be set globally by patching SchemaOrgBase
before other imports:
>>> from pydantic_schemaorg.SchemaOrgBase import SchemaOrgBase
>>> SchemaOrgBase.Config.smart_union = True
>>> from pydantic_schemaorg.Place import Place
>>> from pydantic_schemaorg.PostalAddress import PostalAddress
>>> address = PostalAddress(postalCode="12345")
>>> Place(address=address).address
PostalAddress(reverse_=None, id_=None, context_=None, graph_=None, type_='PostalAddress', mainEntityOfPage=None, additionalType=None, url=None, alternateName=None, sameAs=None, name=None, identifier=None, potentialAction=None, subjectOf=None, description=None, disambiguatingDescription=None, image=None, areaServed=None, contactType=None, availableLanguage=None, serviceArea=None, email=None, hoursAvailable=None, telephone=None, contactOption=None, faxNumber=None, productSupported=None, postalCode='12345', streetAddress=None, addressLocality=None, postOfficeBoxNumber=None, addressCountry=None, addressRegion=None)
@thebuchanan3 Great thanks :D i will patch the SchemaOrgBase to have the Config.smart_union
flag to be True
Using
pydantic_schemaorg==1.0.6
:When creating a place with an address it gets added as Text type:
When adding it after instantiation, it gets added as PostalAddress as expected:
This should probably work the same both ways.