Repository navigation
Closed
Labels
stubs: incompleteAnnotations or sub-modules missing from an existing package or moduleAnnotations or sub-modules missing from an existing package or module
Description
This seems like a similar issue to #2091. The bytes type implements __init__ but doesn't appear to provide an overload for __new__. This makes it harder for subclasses of bytes to define their own behaviour for __new__.
typeshed/stdlib/3/builtins.pyi
Lines 344 to 355 in 9c3978e
| class bytes(ByteString): | |
| @overload | |
| def __init__(self, ints: Iterable[int]) -> None: ... | |
| @overload | |
| def __init__(self, string: str, encoding: str, | |
| errors: str = ...) -> None: ... | |
| @overload | |
| def __init__(self, length: int) -> None: ... | |
| @overload | |
| def __init__(self) -> None: ... | |
| @overload | |
| def __init__(self, o: SupportsBytes) -> None: ... |
This also appears to be the case for some of the other builtins (e.g. str)
typeshed/stdlib/3/builtins.pyi
Lines 267 to 272 in 9c3978e
| class str(Sequence[str]): | |
| @overload | |
| def __init__(self, o: object = ...) -> None: ... | |
| @overload | |
| def __init__(self, o: bytes, encoding: str = ..., errors: str = ...) -> None: ... | |
Reacted by Dominic Davis-Foster
Metadata
Metadata
Assignees
Labels
stubs: incompleteAnnotations or sub-modules missing from an existing package or moduleAnnotations or sub-modules missing from an existing package or module
Activity
srittau commentedon Nov 29, 2018
Could you give an example where this is a problem?
bytesandstrshould inherit__new__fromobjectif I am not mistaken.mystor commentedon Nov 29, 2018
They do, but the argument list is wrong. As a strawman example:
This doesn't typecheck, because the call to
super().__new__(cls, b)is inferred to beobject.__new__(cls, b)which is incorrect asobject.__new__takes only theclsargument.bytes, on the other hand, takes an argument to create the object.This is also a bit like the tuple case again in that
__init__doesn't make much sense forbytesorstr, as they are non-mutable and can only be constructed with__new__in the first place.srittau commentedon Nov 29, 2018
Thanks for the explanation. Makes sense. We appreciate pull requests!
sproshev commentedon Nov 30, 2018
str.__new__had been added in #1352 but then revertedmystor commentedon Nov 30, 2018
Figure I should add a ref to the reason it was backed out: #1464
It appears it was backed out because the addition of
__new__tostrcaused problems for classes deriving from bothstrandEnum, as thestr's__new__would now take priority rather than theEnum's. I'm guessing this is caused by some magic wrt howstris actually implemented within python :-/Perhaps there needs to be a special case for these builtins to make constructing them work correctly?
Use __new__ instead of __init__ for some builtin classes
Use __new__ instead of __init__ for some builtin classes (#4555)
hauntsaninja commentedon Sep 30, 2020
Closed by #4555 (and thank you for git-revise!)
performance: remove redundant cast
performance: remove redundant cast