lexiq-legal / pydantic_schemaorg

Schema.org classes in pydantic
MIT License
56 stars 15 forks source link

Place.address gets added as Text type #5

Open thebuchanan3 opened 2 years ago

thebuchanan3 commented 2 years ago

Using pydantic_schemaorg==1.0.6:

When creating a place with an address it gets added as Text type:

>>> from pydantic_schemaorg.Place import Place
>>> from pydantic_schemaorg.PostalAddress import PostalAddress
>>> address = PostalAddress(postalCode="12345")
>>> Place(address=address).address
Text(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, supersededBy=None)

When adding it after instantiation, it gets added as PostalAddress as expected:

>>> place = Place()
>>> place.address = address
>>> place.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)

This should probably work the same both ways.

crbaker89 commented 2 years ago

Thanks for reporting this bug @thebuchanan3 . On which python version are you?

thebuchanan3 commented 2 years ago

Thanks for reporting this bug @thebuchanan3 . On which python version are you?

3.10.4

thebuchanan3 commented 2 years ago

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')
thebuchanan3 commented 2 years ago

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)
crbaker89 commented 2 years ago

@thebuchanan3 Great thanks :D i will patch the SchemaOrgBase to have the Config.smart_union flag to be True