Skip to content

ValueError: dictionary update sequence element #0 when using dict on a FeatureCollection #172

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

Closed
rsemlal-murmuration opened this issue May 5, 2025 · 2 comments · Fixed by #173

Comments

@rsemlal-murmuration
Copy link

Description

We have an error ValueError: dictionary update sequence element #0 has length X; 2 is required raised when using dict() on a valid instance of FeatureCollection.

How to reproduce

from geojson_pydantic import FeatureCollection

fc = FeatureCollection.model_validate({'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {}, 'geometry': {'type': 'Point', 'coordinates': [1, 2]}}]})

dict(fc) # <-- here

Expected

The call to dict should return a dict

Observed

The call to dict raises a ValueError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 5; 2 is required

Why is this an issue

We are using FeatureCollection as an input parameter type for a Prefect flow.

It looks something like this

# Flow definition
@flow(name="my_flow")
async def my_flow(area: FeatureCollection):
   return "yes"

my_flow(....) # Flow usage

It turns out that Prefect, since version 3.2.16 we believe, start applying a dict on flows input parameters if these parameters are Pydantic models (see here)

Since the dict fails on FeatureCollections, it makes unusable in this context.

@vincentsarago
Copy link
Member

🤔 I wonder if this is due to

def __iter__(self) -> Iterator[Feat]: # type: ignore [override]
"""iterate over features"""
return iter(self.features)
def __len__(self) -> int:
"""return features length"""
return len(self.features)
def __getitem__(self, index: int) -> Feat:
"""get feature at a given index"""
return self.features[index]

ref python/cpython#116938

this is unfortunate, sadly I'm not quite sure we want to remove those method in geojson-pydantic

I don't know prefect by IMO they should be using model_dump() instead of dict() 🤷

@rsemlal-murmuration
Copy link
Author

Thanks for the quick resolution, everything works fine with geojson-pydantic 2.0.0 👌

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 a pull request may close this issue.

2 participants