-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
Adds overlaods for `__new__` method in `int`, `str` and `bool` classes, when no value is provided as an argument
This is incorrect for int and str because on subclasses, this returns an instance of the subclass, not of |
This comment has been minimized.
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]: ... |
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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.
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
|
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 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. |
Going to close for now. I'd personally be inclined to accept a PR that just made the change to |
Adds overlaods for
__new__
method inint
,str
andbool
classes, when no value is provided as an argument