Skip to content

add from_attrs class method to construct object without type attribute #175

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vincentsarago
Copy link
Member

another take at #171 and #174

This PR adds a from_attrs(cls, **kwargs: Any) classmethod to Feature/FeatureCollection and geometries Object to enable Object creation with partial attributes (e.g without type={object type}).

from geojson_pydantic import Point

Point.from_attrs(coordinates=[0,0])
>> Point(bbox=None, type='Point', coordinates=Position2D(longitude=0.0, latitude=0.0))

Point.from_attrs(type="Point", coordinates=[0,0])
>> Point(bbox=None, type='Point', coordinates=Position2D(longitude=0.0, latitude=0.0))

Point.from_attrs(type="P", coordinates=[0,0])
ValidationError: 1 validation error for Point
type
  Input should be 'Point' [type=literal_error, input_value='P', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_erro

I've not added type information on kwargs to make sure the method could work with subclasses

NOTE: I'm open to class method name if from_attrs doesn't make sense

cc @kopp @geospatial-jeff @frank-hulo @gadomski

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

Successfully merging this pull request may close these issues.

1 participant