Skip to content

Accept arbitrary objects in operator.truth and operator.not_ - #16810

Merged
srittau merged 1 commit into
python:mainfrom
carljm:cjm/operator-truth-object
Nov 15, 2026
Merged

srittau merged 1 commit into
python:mainfrom
carljm:cjm/operator-truth-object

Conversation

@carljm

@carljm carljm commented Nov 15, 2026

Copy link
Copy Markdown
Member

operator.not_ and operator.truth do not require their arguments to have a __bool__ method. Any Python object can have its truthiness evaluated; lack of a __bool__ method falls back to __len__ and defaults to "truthy" if neither method is present.

Annotating this parameter as SupportsBool encodes an incorrect requirement and causes false positives on e.g. operator.not_(object()).

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit 00fd40b into python:main Nov 15, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants