-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Perhaps declare a __new__
instead of an __init__
for str
and other similar types
#4514
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
Comments
A short test indicates to me that Edit: Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str.__new__
<built-in method __new__ of type object at 0x81ec40>
>>> str.__init__
<slot wrapper '__init__' of 'object' objects>
>>> |
@JelleZijlstra pointed to #1352 and #1464, where this was (unsuccessfully) tried before. |
FWIW, I just checked the following example from #1464: from enum import Enum
from typing import TYPE_CHECKING
if not TYPE_CHECKING:
reveal_type = print
class I(int, Enum):
a = 1
reveal_type(I['a']) # E: Revealed type is '__main__.I'
class S(str, Enum):
a = '1'
reveal_type(S['a']) # E: Revealed type is '__main__.S'
class F(float, Enum):
a = 1.0
reveal_type(F['a']) # E: Revealed type is '__main__.F' It still works when I add the following definitions to @overload
def __new__(cls: Type[_T], o: object = ...) -> _T: ...
@overload
def __new__(cls: Type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ... (No guarantee that these are the correct definitions for |
At
typeshed/stdlib/2and3/builtins.pyi
Line 446 in 2330083
__init__
is declared forstr
As I understand it,
str
has no proper__init__
, it only has a proper__new__
.I came to this, thinking again about this issue in another repository: python/mypy#9405
The text was updated successfully, but these errors were encountered: