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

Hotfix/types are not flagged by static type checking #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

frank-hulo
Copy link

@frank-hulo frank-hulo commented Mar 31, 2025

This is my first PR on this repo, for me the contributing guidelines are not entirely clear, but will check the remakrs.

This pull request includes updates to the geojson_pydantic/geometries.py file to improve type consistency and string formatting. The most important changes include setting default values for type fields in geometry models.

Reason for this update is mitigate not every time required to define the type and model init. E.g. Point(type="Point", coordinates=(0,1)). This is flagged by strict type checkers the only valid method.

Improvements to type consistency:

Improvements to string formatting:

@vincentsarago
Copy link
Member

dear @frank-hulo

Thanks for the PR.

Sadly default type key was made on purpose, see:

from geojson_pydantic.geometries import _GeometryBase
from geojson_pydantic.types import Position
from typing import Any, Literal

class Point(_GeometryBase):
    """Point Model"""

    type: Literal["Point"] = "Point"
    coordinates: Position


    def __wkt_coordinates__(self, coordinates: Any, force_z: bool) -> str:
        """return WKT coordinates."""
        pass

    @property
    def has_z(self):
        """Checks if any coordinate has a Z value."""
        pass


invalid_point_geometry = {"coordinates": [0, 0]}

Point.model_validate(invalid_point_geometry) # This should fail

# But it doesn't 
>> Point(bbox=None, type='Point', coordinates=Position2D(longitude=0.0, latitude=0.0))

We choose this to make sure geojson models validation was strict. Now if there is a better way to make the validation step better I'm super open to discuss

For the second point I'm not quite sure to see the consistency 🤷

This is my first PR on this repo, for me the contributing guidelines are not entirely clear, but will check the remakrs.

For important changes (like the one proposed), we usually expect issue/discussion before submitting any code (to avoid people wasting time proposing something that would be discarded). I'll update the contributions page 🙏

@frank-hulo
Copy link
Author

update

Thank you for the swift response. Good to understand the history, and just to understand your requirements:

class Point(_GeometryBase):
    """Point Model"""

    type: Literal["Point"] = "Point"
    coordinates: Position


    def __wkt_coordinates__(self, coordinates: Any, force_z: bool) -> str:
        """return WKT coordinates."""
        pass

    @property
    def has_z(self):
        """Checks if any coordinate has a Z value."""
        pass


invalid_point_geometry = {"coordinates": [0, 0]}
valid_point_geometry = {"coordinates": [0, 0], type="Point"}

Point.model_validate(invalid_point_geometry) # This should fail
Point.model_validate(valid_point_geometry ) # This should pass
Point(coordinates= [0, 0]) # this should pass

@vincentsarago
Copy link
Member

invalid_point_geometry = {"coordinates": [0, 0]}
valid_point_geometry = {"coordinates": [0, 0], type="Point"}

Point.model_validate(invalid_point_geometry) # This should fail
Point.model_validate(valid_point_geometry ) # This should pass
Point(coordinates= [0, 0]) # this should pass

yeah, invalid_point_geometry is missing the type so it's not a valid GeoJSON geometry. With the proposed changed geojson-pydantic Point model will not complain when using Point.model_validate({"coordinates": [0, 0]})

We understand when building object using the model (e.g Point(coordinates= [0, 0])) it's annoying to have to pass Point(coordinates= [0, 0], type="point") but as mentioned, the validation feature is considered as more important for the library

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.

2 participants