Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place.address gets added as Text type #5

Open
thebuchanan3 opened this issue Jul 27, 2022 · 5 comments
Open

Place.address gets added as Text type #5

thebuchanan3 opened this issue Jul 27, 2022 · 5 comments

Comments

@thebuchanan3
Copy link

thebuchanan3 commented Jul 27, 2022

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
Copy link
Member

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

@thebuchanan3
Copy link
Author

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

3.10.4

@thebuchanan3
Copy link
Author

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
Copy link
Author

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants