1010from contextlib import contextmanager
1111from re import Pattern
1212
13+ from ._compat import Mapping , Sized
1314from ._config import get_run_validators , set_run_validators
1415from ._make import _AndValidator , and_ , attrib , attrs
1516from .converters import default_if_none
@@ -397,6 +398,15 @@ def __call__(self, inst, attr, value):
397398 if self .mapping_validator is not None :
398399 self .mapping_validator (inst , attr , value )
399400
401+ if not isinstance (value , Mapping ):
402+ msg = f"'{ attr .name } ' must be a mapping (got { value !r} that is a { value .__class__ !r} )."
403+ raise TypeError (
404+ msg ,
405+ attr ,
406+ Mapping ,
407+ value ,
408+ )
409+
400410 for key in value :
401411 if self .key_validator is not None :
402412 self .key_validator (inst , attr , key )
@@ -543,6 +553,9 @@ def __call__(self, inst, attr, value):
543553 """
544554 We use a callable class to be able to change the ``__repr__``.
545555 """
556+ if not isinstance (value , Sized ):
557+ msg = f"'{ attr .name } ' must be a sized object (got { value !r} that is a { value .__class__ !r} )."
558+ raise TypeError (msg )
546559 if len (value ) > self .max_length :
547560 msg = f"Length of '{ attr .name } ' must be <= { self .max_length } : { len (value )} "
548561 raise ValueError (msg )
@@ -572,6 +585,9 @@ def __call__(self, inst, attr, value):
572585 """
573586 We use a callable class to be able to change the ``__repr__``.
574587 """
588+ if not isinstance (value , Sized ):
589+ msg = f"'{ attr .name } ' must be a sized object (got { value !r} that is a { value .__class__ !r} )."
590+ raise TypeError (msg )
575591 if len (value ) < self .min_length :
576592 msg = f"Length of '{ attr .name } ' must be >= { self .min_length } : { len (value )} "
577593 raise ValueError (msg )
@@ -747,4 +763,4 @@ def or_(*validators):
747763 for v in validators :
748764 vals .extend (v .validators if isinstance (v , _OrValidator ) else [v ])
749765
750- return _OrValidator (tuple (vals ))
766+ return _OrValidator (tuple (vals ))
0 commit comments