Skip to content

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

@gadomski gadomski self-requested a review June 24, 2025 13:40
Copy link
Contributor

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about new instead of from_attrs?

Otherwise, LGTM.

@geospatial-jeff
Copy link
Contributor

geospatial-jeff commented Jun 24, 2025

LGTM! I'll save you the philosophical rant on why it feels insane to add a bunch of function calls because developers are too lazy to type out a single word. I understand why this is a good change, but it's also the type of change that makes me question what software is turning into 😂

@vincentsarago
Copy link
Member Author

@gadomski sorry for pinging you on an old PR

agreed from_attrs might not be great, what do you think about .create() (if not I'll switch to .new())

Point.from_attrs(coordinates=(0,0))

Point.new(coordinates=(0,0))

Point.create(coordinates=(0,0))

@gadomski
Copy link
Contributor

gadomski commented Oct 8, 2025

@vincentsarago create vibes well for me 👍🏼

Copy link
Contributor

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two tweaks

vincentsarago and others added 2 commits October 8, 2025 14:51
Co-authored-by: Pete Gadomski <[email protected]>
Co-authored-by: Pete Gadomski <[email protected]>
@vincentsarago vincentsarago merged commit 0218167 into main Oct 8, 2025
6 checks passed
@vincentsarago vincentsarago deleted the feature/add-from-attrs-methods branch October 8, 2025 13:18
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.

4 participants