Skip to content

Override __int__ and __index__ for bool in builtins.pyi - #16067

Open
jonathandung wants to merge 1 commit into
python:mainfrom
jonathandung:patch-6
Open

Override __int__ and __index__ for bool in builtins.pyi#16067
jonathandung wants to merge 1 commit into
python:mainfrom
jonathandung:patch-6

Conversation

@jonathandung

@jonathandung jonathandung commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Such that the fact that they only return 1 for True and 0 for False may be represented without violating the Liskov Substitution Principle. This change is motivated by astral-sh/ty#4048, from which I learnt no type checker supports the inference of the value of booleans in integer operation context, but that may be fixable by a stub-side modification.

@github-actions

This comment has been minimized.

@jonathandung

Copy link
Copy Markdown
Contributor Author

I do not understand the mypy failure involving the term "erased type".

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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

@srittau

srittau commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

This is a mypy bug, see python/mypy#14764. You may try to ignore this error using # type: ignore[misc] with a link to the bug.

@AlexWaygood

Copy link
Copy Markdown
Member

To be clear, I don't think this, on its own, would help ty much with regards to the linked issue. I don't believe we currently look at these dunders when implementing our special casing for arithmetic regarding literal integers or bools. I'm also not sure we'd want to start looking at these dunders if we were to implement the feature request in the linked issue: I think there are probably simpler ways of achieving that goal.

This PR might be worth doing anyway, though; I don't have a position on that

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.

3 participants