You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support special object.__new__ and object.__init__ in super. #794
Currently, __new__ is not special at all. While full support is complicated, it is particularly irritating that object.__new__ cannot be added to stubs at all, since subclasses need to override it with different signatures and that is an error.
object.__new__ and object.__init__ are special in another way. For the object class itself, their signatures are (Subclass[T]) -> T and (T) -> None. However, when called from subclass constructors via super(), the signatures are (Subclass[T], *args, **kwargs) -> T and (T, *args, **kwargs) -> None.
In the above, T is a TypeVar that is bounded by the current class, and Subclass[T] is basically Intersection[type, Callable[..., T]] (i.e. supports duck typing once the Protocol stuff works).
(In my notes I am using use the term Class[T] to refer to Intersection[type, Callable[[signature of __init__], T]])
As an icky workaround, I tried using cast(Any, super()), but that says that super is not defined, before settling on # type: ignore
[C:\Projects]python -m mypy --version
mypy 0.501
[C:\Projects]python -m mypy --strict syetest.py
syetest.py:3: warning: Returning Any from function with declared return type "syetest.IntSubclass"
syetest.py:3: error: Too many arguments for "__new__" of "object"
classVersionID(bytes):
"""A byte string of len 32 that only allows a-z, 0-9, and underscores."""vid: bytes# Must use __new__ since bytes is immutable.def__new__(cls, version_id: Union[bytes, str]) ->'VersionID':
# Allow passing as a string and then convert to ascii bytes.ifisinstance(version_id, str):
vid=version_id.encode('ascii')
else:
vid=version_idassertisinstance(vid, bytes), \
"version_id should be bytes-like, but found {}".format(
type(vid))
assertlen(vid) >0, \
"version_id should be at least one character."assertlen(vid) <=32, \
"version_id should be less than 32 characters."assertre.match(b'^[a-z0-9_]*$', vid), \
"version_id should only contain lowercase letters, numbers, " \
"and underscores."## noinspection PyArgumentListreturnsuper().__new__(cls, vid)
def__str__(self) ->str:
returnself.decode('ascii')
Produces: error: Too many arguments for "__new__" of "object"
I believe that is a separate issue, @frankcarey. It looks like mypy is interpreting super().__new__(cls, vid) as object.__new__(cls, vid), when really it's bytes.__new__(cls, vid). You should log a separate issue for this (or add to an existing issue if it has already been reported).
Incidentally, I would remove the line vid: bytes, which suggests that VersionID().vid is a valid attribute containing the ID, when actually VersionID()itself is the ID.
@JukkaL Is something still needed here? We already allow incompatible overrides of __init__ and __new__, and there is a separate issue for inferring attributes from __new__ body.
Activity
Allow arbitrary overriding of __new__
ddfisher commentedon Mar 2, 2016
This is probably partially out of date -- we should investigate what the current situation is.
Syeberman commentedon Mar 3, 2017
I believe this issue still exists. The following code:
Produces these errors:
frankcarey commentedon Feb 23, 2018
Yes, also getting an error..
Produces:
error: Too many arguments for "__new__" of "object"mypy version: 0.560
Syeberman commentedon Feb 23, 2018
I believe that is a separate issue, @frankcarey. It looks like mypy is interpreting
super().__new__(cls, vid)asobject.__new__(cls, vid), when really it'sbytes.__new__(cls, vid). You should log a separate issue for this (or add to an existing issue if it has already been reported).Incidentally, I would remove the line
vid: bytes, which suggests thatVersionID().vidis a valid attribute containing the ID, when actuallyVersionID()itself is the ID.ilevkivskyi commentedon Dec 12, 2018
@JukkaL Is something still needed here? We already allow incompatible overrides of
__init__and__new__, and there is a separate issue for inferring attributes from__new__body.JukkaL commentedon Dec 13, 2018
The original issue seems to be resolved. Any remaining problems can be reported as separate issues.