-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Description
There are several issues that are pretty closely related to each other. This issue tracks them together. Here are the issues:
- Infer the 'cls' argument of a classmethod as the defining class, not Any #292 (type checking
clsargument of class methods) (EDIT: done) - SelfType or another way to spell "type of self" (or, How to define a copy() function) #1212 (
SelfType, return type of acopy()method) - self.__class__() not supported #1195 (type of
self.__class__) - Can we have a generic Type[C]? typing#107 (
Type[T])
Summary of how the above are related:
- If we support
Type[T]we could use that as the type of theclsargument in class methods. - The result of
cls(...)could have typeSelfType. - The type of
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:
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
clsand 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 supportcls(...)unless the decorator exists.