-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support special object.__new__
and object.__init__
in super
.
#794
Comments
This is probably partially out of date -- we should investigate what the current situation is. |
I believe this issue still exists. The following code: class IntSubclass(int):
def __new__(cls, v: int) -> IntSubclass:
return super().__new__(cls, v) Produces these errors:
|
Yes, also getting an error.. class VersionID(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.
if isinstance(version_id, str):
vid = version_id.encode('ascii')
else:
vid = version_id
assert isinstance(vid, bytes), \
"version_id should be bytes-like, but found {}".format(
type(vid))
assert len(vid) > 0, \
"version_id should be at least one character."
assert len(vid) <= 32, \
"version_id should be less than 32 characters."
assert re.match(b'^[a-z0-9_]*$', vid), \
"version_id should only contain lowercase letters, numbers, " \
"and underscores."
## noinspection PyArgumentList
return super().__new__(cls, vid)
def __str__(self) -> str:
return self.decode('ascii') Produces: mypy version: 0.560 |
I believe that is a separate issue, @frankcarey. It looks like mypy is interpreting Incidentally, I would remove the line |
@JukkaL Is something still needed here? We already allow incompatible overrides of |
The original issue seems to be resolved. Any remaining problems can be reported as separate issues. |
Currently,
__new__
is not special at all. While full support is complicated, it is particularly irritating thatobject.__new__
cannot be added to stubs at all, since subclasses need to override it with different signatures and that is an error.object.__new__
andobject.__init__
are special in another way. For theobject
class itself, their signatures are(Subclass[T]) -> T
and(T) -> None
. However, when called from subclass constructors viasuper()
, the signatures are(Subclass[T], *args, **kwargs) -> T
and(T, *args, **kwargs) -> None
.In the above,
T
is aTypeVar
that is bounded by the current class, andSubclass[T]
is basicallyIntersection[type, Callable[..., T]]
(i.e. supports duck typing once theProtocol
stuff works).(In my notes I am using use the term
Class[T]
to refer toIntersection[type, Callable[[signature of __init__], T]]
)As an icky workaround, I tried using
cast(Any, super())
, but that says thatsuper
is not defined, before settling on# type: ignore
The text was updated successfully, but these errors were encountered: