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
If we support Type[T] we could use that as the type of the cls argument in class methods.
The result of cls(...) could have type SelfType.
The type of self.__class__ could be Type[T] (similarly for type(self)).
Another thing to discuss is determining/specifying when overrides of __init__ should be compatible with a base class, which is usually not required. For example, we want to restrict __init__ to accept a single argument here:
class A:
def __init__(self, x: int) -> None: ...
@classmethod
def f(cls) -> None:
a = cls(1)
...
class B(A):
def __init__(self) -> None: ... # should be rejected!
There are at least are a few different ways of fixing the above issue:
Detect any calls to cls and if such things exist, enforce __init__ compatibility implicitly. This wouldn't work well in a stub as function bodies are empty.
Require a magic __init__ decorator or class decorator that causes __init__ compatibility to be enforced. Don't support cls(...) unless the decorator exists.
The text was updated successfully, but these errors were encountered:
There are several issues that are pretty closely related to each other. This issue tracks them together. Here are the issues:
cls
argument of class methods) (EDIT: done)SelfType
, return type of acopy()
method)self.__class__
)Type[T]
)Summary of how the above are related:
Type[T]
we could use that as the type of thecls
argument in class methods.cls(...)
could have typeSelfType
.self.__class__
could beType[T]
(similarly fortype(self)
).Another thing to discuss is determining/specifying when overrides of
__init__
should be compatible with a base class, which is usually not required. For example, we want to restrict__init__
to accept a single argument here:There are at least are a few different ways of fixing the above issue:
cls
and if such things exist, enforce__init__
compatibility implicitly. This wouldn't work well in a stub as function bodies are empty.__init__
decorator or class decorator that causes__init__
compatibility to be enforced. Don't supportcls(...)
unless the decorator exists.The text was updated successfully, but these errors were encountered: