[New-bugs-announce] [issue44558] operator.countOf `is` / `==` inconsistency

Rupert Tombs report at bugs.python.org
Sat Jul 3 13:25:15 EDT 2021


New submission from Rupert Tombs <rupert.tombs at gmail.com>:

operator.countOf behaves differently in its docstring and its c and python implementations when `is` and `==` differ.

help(countOf) ->

    countOf(a, b, /)
        Return the number of times b occurs in a.

This could be read to say that it returns equal to `sum(x is b for x in a)`.

Its python implementation returns `sum(x == b for x in a)`.

Since its c implementation uses `PyObject_RichCompareBool`, it returns `sum(x is b or x == b for x in a)`.

Results of these implementations can differ when `x is b` does not imply `x == b`;
that could be from an __eq__ method, but the the float NaN is a real example since NaN != NaN.


The issue is demonstrated with a possible fix here https://godbolt.org/z/cPT7TToG7


Since the c version has been in the wild for decades, I suggest that it should be taken to define the function;
the python should be updated to match it, and the docstring could say
"Return the number of items in a which are, or which equal, b."

I will open a pull request with these changes shortly.

----------
assignee: docs at python
components: Documentation, Library (Lib)
messages: 396917
nosy: docs at python, rtombs
priority: normal
severity: normal
status: open
title: operator.countOf `is` / `==` inconsistency
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9

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


More information about the New-bugs-announce mailing list