{{ message }}
BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon#2461
Merged
caspervdw merged 1 commit intoJun 30, 2026
Merged
Conversation
A non-Polygon BaseGeometry (for example a GeometryCollection or Point) appearing after the first element of the input list fell through to the (shell, holes) unpacking path and raised a cryptic "TypeError: 'GeometryCollection' object is not subscriptable". Detect BaseGeometry instances that are not Polygons in the construction loop and raise a ValueError naming the offending geometry type. This mirrors the existing member-type check that rejects MultiPolygon inputs. Closes shapely#2178
caspervdw
approved these changes
Jun 30, 2026
caspervdw
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the contribution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon
What
MultiPolygon.__new__only type-checked the first element of its inputlist. A non-Polygon
BaseGeometry(for example aGeometryCollectionorPoint) appearing after the first element fell through to the(shell, holes)unpacking path and hitshell = ob[0], raising acryptic error:
This change detects
BaseGeometryinstances that are notPolygons inthe construction loop and raises a
ValueErrornaming the offendinggeometry type instead:
Why
The previous behaviour surfaced an implementation detail (
__getitem__)rather than the actual problem (an invalid member type). The new check
mirrors the existing member-type guard that already rejects
MultiPolygoninputs, keeping the constructor's error reportingconsistent.
Valid inputs are unaffected: plain
Polygonobjects,(shell, holes)sequence/tuple inputs, and construction from an existing
MultiPolygonall continue to work.
MultiPolygonmembers are still caught earlier bythe existing dedicated check.
Tests
Added
test_fail_list_with_non_polygon_geometry(mirroringtest_fail_list_of_multipolygons), parametrized overGeometryCollection,Point,LineString, andLinearRing. Each type is checked in threepositions: as the only element, before a valid
Polygon, and after a validPolygon. Every case asserts aValueErrornaming the offending type.Verified the new cases fail on the unpatched source (the original
TypeError) and pass with the fix. The fullshapely/tests/geometrysuitepasses.
Closes #2178