Skip to content

Add an overload for int, str and bool #14222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

GameRoMan
Copy link

Adds overlaods for __new__ method in int, str and bool classes, when no value is provided as an argument

Adds overlaods for `__new__` method in `int`, `str` and `bool` classes, when no value is provided as an argument
@GameRoMan GameRoMan marked this pull request as draft June 5, 2025 14:39
@JelleZijlstra
Copy link
Member

This is incorrect for int and str because on subclasses, this returns an instance of the subclass, not of str or int itself.

This comment has been minimized.

@@ -243,7 +243,7 @@ _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026

class int:
@overload
def __new__(cls, /) -> Literal[0]: ...
def __new__(cls: type[int], /) -> Literal[0]: ...
Copy link
Member

@AlexWaygood AlexWaygood Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't stop the overload from applying to subclasses, because type[bool] is a subtype of type[int], for example (bool is a subclass of int)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't think the current type system has a way to fix the issue I brought up.

Copy link
Contributor

github-actions bot commented Jun 5, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

colour (https://github.com/colour-science/colour)
+ colour/notation/hexadecimal.py:135: note:     def __new__(cls) -> int

discord.py (https://github.com/Rapptz/discord.py)
+ discord/app_commands/models.py:223: note:     def __new__(cls) -> int

ibis (https://github.com/ibis-project/ibis)
+ ibis/common/temporal.py:203: note:     def __new__(cls) -> int

@JelleZijlstra
Copy link
Member

We could do this for bool (which can't have subclasses) but I don't think we can make it work for int or str, and I don't see much point in the change anyway.

For bool we should remove the = ... for the second overload, so the overloads don't overlap. We also don't need the type[bool] annotation.

I'm somewhat hesitant to accept the change for bool too, since changes to core stubs can sometimes have unintended consequences, and I don't see much benefit here. The mypy-primer output is promising, though.

@AlexWaygood
Copy link
Member

Going to close for now. I'd personally be inclined to accept a PR that just made the change to bool.__new__ (we already hardcode that knowledge into ty), but IIRC it might break a lot of mypy tests, as mypy often assumes that bool() is going to return a bool in its test suite. So it would be good to check that it wouldn't be hugely disruptive to mypy if it was a bool-only PR (I agree with @JelleZijlstra that the benefits are fairly slim, so it's not worth it if it would cause a lot of hassle for mypy IMO)

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