This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make the implementation consistency of operator.countOf/indexOf
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: corona10 Nosy List: corona10, docs@python, miss-islington, rhettinger, rtombs, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2021-07-03 17:25 by rtombs, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27007 merged rtombs, 2021-07-03 17:44
PR 27012 merged corona10, 2021-07-04 12:17
PR 27029 merged miss-islington, 2021-07-05 09:04
PR 27030 merged miss-islington, 2021-07-05 09:05
PR 27053 merged miss-islington, 2021-07-07 13:28
PR 27054 closed rtombs, 2021-07-07 13:50
PR 27055 merged corona10, 2021-07-07 13:51
Messages (10)
msg396917 - (view) Author: Rupert Tombs (rtombs) * Date: 2021-07-03 17:25
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.
msg396974 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-07-05 09:04
New changeset 09302405d22e86884d6058226790c0cdf5b72f14 by Dong-hee Na in branch 'main':
bpo-44558: Make the implementation consistency of operator.indexOf (GH-27012)
https://github.com/python/cpython/commit/09302405d22e86884d6058226790c0cdf5b72f14
msg396981 - (view) Author: miss-islington (miss-islington) Date: 2021-07-05 09:51
New changeset 1f8486fd50ad4666078eb40ce9f24e8aac17a395 by Miss Islington (bot) in branch '3.10':
bpo-44558: Make the implementation consistency of operator.indexOf (GH-27012)
https://github.com/python/cpython/commit/1f8486fd50ad4666078eb40ce9f24e8aac17a395
msg396982 - (view) Author: miss-islington (miss-islington) Date: 2021-07-05 09:52
New changeset 9f47d872dbebdd95fd6b0a3351aeafaf82dfd203 by Miss Islington (bot) in branch '3.9':
bpo-44558: Make the implementation consistency of operator.indexOf (GH-27012)
https://github.com/python/cpython/commit/9f47d872dbebdd95fd6b0a3351aeafaf82dfd203
msg397034 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-07-06 09:36
This issue can be closed, no?
msg397035 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-07-06 09:47
@vstinner

https://github.com/python/cpython/pull/27007 is not yet merged due to CLA issue
msg397081 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-07-07 13:28
New changeset 6bd3ecfc272b122b55a6adec50dd7a7c868f262f by Rupert Tombs in branch 'main':
bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007)
https://github.com/python/cpython/commit/6bd3ecfc272b122b55a6adec50dd7a7c868f262f
msg397086 - (view) Author: miss-islington (miss-islington) Date: 2021-07-07 13:50
New changeset 9f431dd0a59c9ac2de46772a4dbd7db2199ef26a by Miss Islington (bot) in branch '3.10':
bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007)
https://github.com/python/cpython/commit/9f431dd0a59c9ac2de46772a4dbd7db2199ef26a
msg397095 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-07-07 14:55
New changeset 9761abf30617946a374886184b0e36ec050dd8d7 by Dong-hee Na in branch '3.9':
[3.9] bpo-44558: Match countOf `is`/`==` treatment to c (GH-27007). (GH-27055)
https://github.com/python/cpython/commit/9761abf30617946a374886184b0e36ec050dd8d7
msg397096 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-07-07 14:56
Thank you Rupert for reporting and contributing!!
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88724
2021-07-07 14:56:14corona10setstatus: open -> closed
resolution: fixed
messages: + msg397096

stage: patch review -> resolved
2021-07-07 14:55:38corona10setmessages: + msg397095
2021-07-07 13:51:27corona10setpull_requests: + pull_request25611
2021-07-07 13:50:55rtombssetpull_requests: + pull_request25610
2021-07-07 13:50:50miss-islingtonsetmessages: + msg397086
2021-07-07 13:29:46corona10setassignee: docs@python -> corona10
components: - Documentation
title: operator.countOf `is` / `==` inconsistency -> Make the implementation consistency of operator.countOf/indexOf
2021-07-07 13:28:27corona10setmessages: + msg397081
2021-07-07 13:28:27miss-islingtonsetpull_requests: + pull_request25609
2021-07-06 09:47:07corona10setmessages: + msg397035
2021-07-06 09:36:08vstinnersetmessages: + msg397034
2021-07-05 20:51:01rhettingersetnosy: + rhettinger
2021-07-05 09:52:08miss-islingtonsetmessages: + msg396982
2021-07-05 09:51:56miss-islingtonsetmessages: + msg396981
2021-07-05 09:05:01miss-islingtonsetpull_requests: + pull_request25589
2021-07-05 09:04:55miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25588
2021-07-05 09:04:40corona10setmessages: + msg396974
2021-07-04 16:30:45corona10setversions: - Python 3.6, Python 3.7, Python 3.8
2021-07-04 12:17:47corona10setpull_requests: + pull_request25572
2021-07-04 12:09:13corona10setnosy: + vstinner, serhiy.storchaka, corona10
2021-07-03 17:44:33rtombssetkeywords: + patch
stage: patch review
pull_requests: + pull_request25567
2021-07-03 17:25:15rtombscreate