Skip to content

Add deprecation warnings for iteration & getitem in multipart geometries#950

Merged
jorisvandenbossche merged 10 commits intoshapely:masterfrom
tomplex:deprecate-iteration
Sep 10, 2020
Merged

Add deprecation warnings for iteration & getitem in multipart geometries#950
jorisvandenbossche merged 10 commits intoshapely:masterfrom
tomplex:deprecate-iteration

Conversation

@tomplex
Copy link
Copy Markdown
Contributor

@tomplex tomplex commented Jul 28, 2020

As per #932, this adds deprecation warnings for __iter__ and __getitem__ in multi-part geometries.

Looks like stacklevel=1 gives a message like:

/Users/tom/dev/python/contrib/Shapely/shapely/geometry/base.py:880: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.

which feels right to me.

@coveralls
Copy link
Copy Markdown

coveralls commented Jul 28, 2020

Coverage Status

Coverage increased (+0.08%) to 83.083% when pulling 722a43c on tomplex:deprecate-iteration into 82ad633 on Toblerity:master.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Jul 31, 2020

Looking into #952 showed me a couple places where the library itself actually depends on length / iteration over geometries, so there's more to do here than I realized.

Copy link
Copy Markdown
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this!!

a couple places where the library itself actually depends on length / iteration over geometries, so there's more to do here than I realized.

Indeed, wherever this pattern is used in Shapely itself, we can already add a .geom to avoid the warning.

The output of the tests can give a good indication where there are still such cases to fix (assuming good coverage of the tests, of course). See eg https://travis-ci.org/github/Toblerity/Shapely/jobs/712405513 which has in the output:

=============================== warnings summary ===============================
tests/test_getitem.py: 3 tests with warnings
tests/test_multilinestring.py: 2 tests with warnings
tests/test_multipoint.py: 1 test with warning
tests/test_multipolygon.py: 1 test with warning
tests/test_shared_paths.py: 1 test with warning
tests/test_split.py: 12 tests with warnings
  /home/travis/build/Toblerity/Shapely/shapely/geometry/base.py:902: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.
...

(sometimes it can help to let the warning error to find all cases where it is being raised)


Does this already add a warning for GeometryCollection as well? (in which case we should add a test for that)

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Aug 6, 2020

Thanks for the review, @jorisvandenbossche! Looks like there's still a lot to do but I've a good start on it now.

With regards to adding a test for GeometryCollection, that's another place I need to address, but I'll wait until #951 is merged since it'll touch the same code.

As a thought: do we want to deprecate __len__ as well? A len usually implies iterability, and if we're telling users to use the geoms property to access individual parts and to iterate, it feels like they should have to use geoms to get a len, as well.

@tomplex tomplex changed the title Add deprecation warnings for iteration & getitem in multipart geometries WIP: Add deprecation warnings for iteration & getitem in multipart geometries Aug 6, 2020
@jorisvandenbossche
Copy link
Copy Markdown
Member

do we want to deprecate __len__ as well?

Ah, that's a good point, and something we didn't consider yet in the RFC.

I agree that kind of follows out of deprecating iteration/getitem, and similarly users can switch len(obj) to len(obj.geoms).
An alternative could be to add a "length" to all types of geometries, where the single-part geometries like Point, LineString, Polygon would have a length of 1.

(for this PR we can maybe focus on iteration/getitem, since that seems already quite some work anyways?)

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Aug 6, 2020

Sure, we can address deprecation (or other changes to) __len__ for a later PR. I'll add something to the RFC later today to try to get some discussion going.

@jorisvandenbossche
Copy link
Copy Markdown
Member

jorisvandenbossche commented Aug 21, 2020

@tomplex do you have some time to update this?
(I could otherwise open a PR against your branch on your fork, if you like)

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Aug 21, 2020

Yes sorry for the stagnation! I'll aim to get more done here this weekend! With the summer fading I've been focused on being outside instead of writing more code after work =)

@jorisvandenbossche
Copy link
Copy Markdown
Member

That's certainly a good focus! ;-)

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Aug 31, 2020

Hey @jorisvandenbossche, despite my best efforts I've been struggling to find time to work on this, so if you want to add to it, please feel free.

@jorisvandenbossche jorisvandenbossche changed the title WIP: Add deprecation warnings for iteration & getitem in multipart geometries Add deprecation warnings for iteration & getitem in multipart geometries Sep 7, 2020
@jorisvandenbossche
Copy link
Copy Markdown
Member

@tomplex I updated this: merged latest master + fixed some remaining usages of getitem in the tests. The full test build now runs without showing any warning.

This should be good for a final review now.

@tomplex
Copy link
Copy Markdown
Contributor Author

tomplex commented Sep 8, 2020

Thanks for pushing on this, @jorisvandenbossche. Things have been busy lately and I've been struggling to keep up. Are there any other required changes before this gets merged?

@jorisvandenbossche
Copy link
Copy Markdown
Member

No, I thnk this is ready for merging. I will do a follow-up PR to also look into the len issue.

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.

4 participants