BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon by madhavcodez · Pull Request #2461 · shapely/shapely · GitHub
Skip to content

BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon#2461

Merged
caspervdw merged 1 commit into
shapely:mainfrom
madhavcodez:fix/multipolygon-non-polygon-element
Jun 30, 2026
Merged

BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon#2461
caspervdw merged 1 commit into
shapely:mainfrom
madhavcodez:fix/multipolygon-non-polygon-element

Conversation

@madhavcodez

Copy link
Copy Markdown
Contributor

BUG: raise clear ValueError for non-Polygon geometries in MultiPolygon

What

MultiPolygon.__new__ only type-checked the first element of its input
list. A non-Polygon BaseGeometry (for example a GeometryCollection or
Point) appearing after the first element fell through to the
(shell, holes) unpacking path and hit shell = ob[0], raising a
cryptic error:

import shapely
poly = shapely.box(0, 0, 1, 1)
shapely.MultiPolygon([poly, shapely.GeometryCollection([poly])])
# TypeError: 'GeometryCollection' object is not subscriptable

This change detects BaseGeometry instances that are not Polygons in
the construction loop and raises a ValueError naming the offending
geometry type instead:

ValueError: Input must be valid Polygon objects or sequences of
(shell, holes) tuples, got a GeometryCollection

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
MultiPolygon inputs, keeping the constructor's error reporting
consistent.

Valid inputs are unaffected: plain Polygon objects, (shell, holes)
sequence/tuple inputs, and construction from an existing MultiPolygon
all continue to work. MultiPolygon members are still caught earlier by
the existing dedicated check.

Tests

Added test_fail_list_with_non_polygon_geometry (mirroring
test_fail_list_of_multipolygons), parametrized over GeometryCollection,
Point, LineString, and LinearRing. Each type is checked in three
positions: as the only element, before a valid Polygon, and after a valid
Polygon. Every case asserts a ValueError naming the offending type.
Verified the new cases fail on the unpatched source (the original
TypeError) and pass with the fix. The full shapely/tests/geometry suite
passes.

Closes #2178

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 caspervdw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the contribution.

@caspervdw caspervdw merged commit 330e9d3 into shapely:main Jun 30, 2026
23 of 24 checks passed
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.

Multipolygon constructor __new__ failing without notice when GeometryCollection in polygons list

2 participants