Skip to content

Item from_dict throws KeyError if fields don't exist, disallowing read of invalid Item JSON #831

@philvarner

Description

@philvarner

The Item.from_dict method uses the dictionary "pop" method for retrieving most fields from the source dict, resulting in a KeyError if they don't exist.

The conceptual issue here is that from_dict doesn't allow the creation on an invalid Item from an invalid JSON/dict representation, whereas manually invoking the init method of Item is typed to and does allow the creation on invalid items:

    def __init__(
        self,
        id: str,
        geometry: Optional[Dict[str, Any]],
        bbox: Optional[List[float]],
        datetime: Optional[Datetime],
        properties: Dict[str, Any],
        stac_extensions: Optional[List[str]] = None,
        href: Optional[str] = None,
        collection: Optional[Union[str, Collection]] = None,
        extra_fields: Optional[Dict[str, Any]] = None,

If the from_dict method is intended to disallow the creation of an Item from an invalid dict representation, it should throw a specific exception to this effect, not the accidental and non-contextualize KeyError.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions