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

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

@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 😂

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.

3 participants