[issue46376] PyMapping_Check returns 1 for list

Raymond Hettinger report at bugs.python.org
Fri Jan 14 23:34:42 EST 2022


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> It changes behavior for objects not being iterable/sequences 
> if not inheriting from `abc.collections.Sequence`.

This would be a breaking change (for example, it broke the long stable sre_parse code).

The utility of PySequence_Check and PyMapping_Check is already so low that it isn't work breaking other things to just to marginally improve these two minor and rarely used functions.

These functions will never be fully reliable.  The documentation explains that in general, we can't tell if a class with __getitem__ is a mapping or a sequence.  Sometimes hints are present (such as the tp_flags), but the can't get a reliable result.  As Guido observed, "calling PyMapping_Check() was never particularly reliable, and extension modules depending on it probably always had subtle bugs."  That was true in 2011 and it is still true today.

I recommend closing this.  These functions are mostly unimportant and unreliable and cannot be made correct.  In contrast, iterability is important and needs to be stable.  Special cases aren't important enough to break the rules.

----------
assignee:  -> rhettinger

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46376>
_______________________________________


More information about the Python-bugs-list mailing list