Skip to content

Commit

Permalink
Restore mutable item access to Properties
Browse files Browse the repository at this point in the history
Resolves #1430.
  • Loading branch information
sgillies committed Aug 21, 2024
1 parent 103c11f commit 6bf2bf7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This is the first release candidate for 1.10.0.

Changes:

- Mutable item access to Feature, Geometry, and Properties instances has been
restored (reported in #1430). This usage should be avoided as instances of
these classes will be immutable in a future version.
- The setup.cfg duplicates project configuration in pyproject.toml and has been
removed.

Expand Down
15 changes: 9 additions & 6 deletions fiona/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ def _props(self):
}

def __getitem__(self, item):
props = {
k: (dict(v) if isinstance(v, Object) else v)
for k, v in self._props().items()
}
props.update(**self._data)
return props[item]
if item in self._delegated_properties:
return getattr(self._delegate, item)
else:
props = {
k: (dict(v) if isinstance(v, Object) else v)
for k, v in self._props().items()
}
props.update(**self._data)
return props[item]

def __iter__(self):
props = self._props()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,12 @@ def test_feature_repr():
properties=Properties(a=1, foo="bar"),
)
assert repr(feat) == "fiona.Feature(geometry=fiona.Geometry(coordinates=[(0, 0), ...], type='LineString'), id='1', properties=fiona.Properties(a=1, foo='bar'))"


def test_issue1430():
"""__getitem__() returns property, not disconnected dict."""
feat = Feature(properties=Properties())
with pytest.warns(FionaDeprecationWarning, match="immutable"):
feat["properties"]["foo"] = "bar"
assert feat["properties"]["foo"] == "bar"
assert feat.properties["foo"] == "bar"

0 comments on commit 6bf2bf7

Please sign in to comment.