@@ -482,14 +482,16 @@ def validate_document_class(
482482 option : str , value : Any
483483) -> Union [type [MutableMapping [str , Any ]], type [RawBSONDocument ]]:
484484 """Validate the document_class option."""
485- # issubclass can raise TypeError for generic aliases like SON[str, Any].
486- # In that case we can use the base class for the comparison.
487- is_mapping = False
485+ # Generic aliases like SON[str, Any] or dict[str, Any] aren't classes, so
486+ # resolve to their origin before the subclass check. Whether issubclass()
487+ # raises TypeError or just returns False for such aliases is inconsistent
488+ # across Python implementations (e.g. PyPy vs CPython), so check the
489+ # origin proactively instead of relying on catching the error.
490+ check_class = getattr (value , "__origin__" , value )
488491 try :
489- is_mapping = issubclass (value , abc .MutableMapping )
492+ is_mapping = issubclass (check_class , abc .MutableMapping )
490493 except TypeError :
491- if hasattr (value , "__origin__" ):
492- is_mapping = issubclass (value .__origin__ , abc .MutableMapping )
494+ is_mapping = False
493495 if not is_mapping and not issubclass (value , RawBSONDocument ):
494496 raise TypeError (
495497 f"{ option } must be dict, bson.son.SON, "
0 commit comments