Skip to content

[2.0] Remove mutability of geometries #960

Merged
mwtoews merged 3 commits intoshapely:shapely-2.0from
jorisvandenbossche:shapely-2.0-remove-mutability
Aug 26, 2020
Merged

[2.0] Remove mutability of geometries #960
mwtoews merged 3 commits intoshapely:shapely-2.0from
jorisvandenbossche:shapely-2.0-remove-mutability

Conversation

@jorisvandenbossche
Copy link
Copy Markdown
Member

I branched off from master, so the shapely-2.0 branch first needs to be reset to master state for this to work (the actual commit is only the last one)

@coveralls
Copy link
Copy Markdown

coveralls commented Aug 11, 2020

Coverage Status

Coverage increased (+0.03%) to 82.614% when pulling a04431f on jorisvandenbossche:shapely-2.0-remove-mutability into 210e71b on Toblerity:shapely-2.0.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

@sgillies could you rebase the shapely-2.0 branch on top of master? (see also #962) Then the diff of this PR should be more correct.

@sgillies
Copy link
Copy Markdown
Contributor

@jorisvandenbossche done. You still need to rebase your own branch, right?

@jorisvandenbossche jorisvandenbossche changed the base branch from shapely-2.0 to master August 18, 2020 06:05
@jorisvandenbossche jorisvandenbossche changed the base branch from master to shapely-2.0 August 18, 2020 06:05
@jorisvandenbossche
Copy link
Copy Markdown
Member Author

Thnks! Switching branches back and again also seemed to trigger GitHub

@jorisvandenbossche jorisvandenbossche marked this pull request as ready for review August 18, 2020 06:07
@jorisvandenbossche
Copy link
Copy Markdown
Member Author

@sgillies @mwtoews this is ready for review/merge


def _set_coords(self, ob):
raise NotImplementedError(
"set_coords must be provided by derived classes")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bye bye fragile mutable geometries 🔪

Copy link
Copy Markdown
Contributor

@sgillies sgillies left a comment

Choose a reason for hiding this comment

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

I love the +/- ratio on this PR: 1/132 🥇

Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Theses changes are welcome! I have suggestions to keep (and modify) two tests to check that point and linestring geometries now have immutable .coords properties. It might be helpful to the reader to see a comment to say that these were mutable in previous versions. All other tests should be dropped, however.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

keep (and modify) two tests to check that point and linestring geometries now have immutable .coords properties. It might be helpful to the reader to see a comment to say that these were mutable in previous versions.

Good point, will update for that.

Thanks for the review!

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

Added back tests for point/linestring/linearring to ensure immutability.

We now have those errors:

In [1]: from shapely.geometry import LineString

In [3]: l = LineString([(0,0), (1,1)])


In [6]: l.coords[0] = (5, 5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-41687dadde5d> in <module>
----> 1 l.coords[0] = (5, 5)

TypeError: 'CoordinateSequence' object does not support item assignment

In [7]: l.coords = [(0,0), (1,1)]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-dc6ae96a6b61> in <module>
----> 1 l.coords = [(0,0), (1,1)]

AttributeError: can't set attribute

which are the default errors produced by Python for a property without a setter (the first one about CoordinateSequence is already existing on master, just added to the test for completeness).

Are we OK with that test? I could also add back a setter to the coords property with a more custom error if we want that.

Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

🎉

@sgillies
Copy link
Copy Markdown
Contributor

@jorisvandenbossche I'm fine with that. Since we're going to have a warning period, a custom error won't be necessary. If these objects had custom setters, custom exceptions would help users differentiate between Shapely usage bugs and Shapely bugs/Python usage bugs. In 2.0, trying to modify a coordinate sequence is a Python usage bug, and a type or attribute error fits.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

OK, this should be good to go then!

@sgillies
Copy link
Copy Markdown
Contributor

@mwtoews @jorisvandenbossche merge when you are ready. I'm happy to let the two of you be stewards of the shapely-2.0 branch while I focus on the others.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

I would be happy with managing the shapely-2.0 branch (as noted in #962, I would also prefer to regularly rebase it on top of master initially), but then you will need to give me commit rights ;) (I can ensure to use them wisely!)

@mwtoews mwtoews merged commit f0eed38 into shapely:shapely-2.0 Aug 26, 2020
@jorisvandenbossche jorisvandenbossche deleted the shapely-2.0-remove-mutability branch August 26, 2020 20:17
@sgillies
Copy link
Copy Markdown
Contributor

@jorisvandenbossche I'd lost track of that requirement and will make it so.

@jorisvandenbossche
Copy link
Copy Markdown
Member Author

Thanks!

jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Sep 13, 2020
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Nov 27, 2020
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Apr 5, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request May 27, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request May 27, 2021
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Aug 20, 2021
jorisvandenbossche added a commit that referenced this pull request Aug 21, 2021
* keep tests for immutability
jorisvandenbossche added a commit to jorisvandenbossche/shapely that referenced this pull request Sep 30, 2021
jorisvandenbossche added a commit that referenced this pull request Oct 27, 2021
* keep tests for immutability
jorisvandenbossche added a commit that referenced this pull request Oct 28, 2021
* keep tests for immutability
jorisvandenbossche added a commit that referenced this pull request Nov 8, 2021
* keep tests for immutability
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