django/django · error · TypeError
Invalid type encountered in the arguments.
Error message
Invalid type encountered in the arguments.
What it means
A TypeError from ListMixin._check_allowed (mutable_list.py:243) when the class declares an `_allowed` type/tuple and an assigned item is not an instance of it. This enforces homogeneous item types on geometries like Polygon (rings must be LinearRing) or MultiLineString (members must be LineString). The check runs on every __setitem__, _set_slice, and append path.
Source
Thrown at django/contrib/gis/geos/mutable_list.py:243
raise ValueError("Cannot have more than %d items" % self._maxlength)
self._set_list(newLen, newItems)
def _set_single_rebuild(self, index, value):
self._set_slice(slice(index, index + 1, 1), [value])
def _checkindex(self, index):
length = len(self)
if 0 <= index < length:
return index
if -length <= index < 0:
return index + length
raise IndexError("invalid index: %s" % index)
def _check_allowed(self, items):
if hasattr(self, "_allowed"):
if False in [isinstance(val, self._allowed) for val in items]:
raise TypeError("Invalid type encountered in the arguments.")
def _set_slice(self, index, values):
"Assign values to a slice of the object"
try:
valueList = list(values)
except TypeError:
raise TypeError("can only assign an iterable to a slice")
self._check_allowed(valueList)
origLen = len(self)
start, stop, step = index.indices(origLen)
# CAREFUL: index.step and step are not the same!
# step will never be None
if index.step is None:
self._assign_simple_slice(start, stop, valueList)
else:View on GitHub (pinned to ae25a40be0)
Solutions
- Construct the correct type before assignment (e.g. build a LinearRing, then append).
- Inspect geom._allowed to learn the required type.
- Use the concrete constructor (Polygon([...]) ) rather than mutating the ring list directly.
Example fix
// before poly[0] = [(0, 0), (1, 1), (2, 0), (0, 0)] # tuple not allowed // after from django.contrib.gis.geos import LinearRing poly[0] = LinearRing([(0, 0), (1, 1), (2, 0), (0, 0)])
Defensive patterns
Strategy: type-guard
Validate before calling
def assign_ring(poly, idx, ring):
if not isinstance(ring, poly._allowed):
from django.contrib.gis.geos import LinearRing
ring = LinearRing(ring)
poly[idx] = ring Type guard
def is_allowed_item(geom, item) -> bool:
allowed = getattr(geom, '_allowed', None)
return allowed is None or isinstance(item, allowed) Prevention
- Construct the correct concrete type before assignment.
- Inspect geom._allowed to learn the required type.
- Use the concrete constructor instead of mutating the list directly.
When it happens
Trigger: Assigning a Point to a Polygon's ring list; appending a LineString to a MultiPoint; assigning a plain tuple where a LinearRing instance is required; mixing concrete geometry subclasses within a collection.
Common situations: Treating ring slots as coordinate lists rather than LinearRing objects; building multi-geometries from heterogeneously typed members; converting between geometry collections without explicit construction.
Related errors
- %s is not a legal index
- Must have at least %d items
- Cannot have more than %d items
- can only assign an iterable to a slice
- Invalid initialization input for LineStrings.
AI-assisted analysis of django/django@ae25a40be0 (2026-08-06).
Data as JSON: /api/errors/547c46f9ef8ffd43.
Report an issue: GitHub.