See python/mypy#1212. The proposal does not require changes to typing.py but it could benefit from words in PEP 484 requiring that this works.
The idea is simple: in an instance method, you can add a type annotation for self which is a type variable, and make the return type use that same type variable, e.g.
T=TypeVar('T', bound='Copyable')
classCopyable:
defcopy(self: T) ->T:
# return a copy of selfclassC(Copyable): ...
c=C()
c2=c.copy() # type here should be CIt should also work for class methods, using Type[], e.g.
T=TypeVar('T', bound='C')
classC:
deffactory(cls: Type[T]) ->T:
# make a new instance of clsclassD(C): ...
d=D.factory() # type here should be DNote that the Python 2 annotation syntax already supports this, e.g.
T=TypeVar('T', bound='Copyable')
classCopyable:
defcopy(self):
# type: (T) -> T# etc.(IOW if you have a method with self and three arguments, you can give it either 3 or 4 arguments in the # type: comment, and if 4, the first will be used for self. Ditto for class methods.)
See python/mypy#1212. The proposal does not require changes to typing.py but it could benefit from words in PEP 484 requiring that this works.
The idea is simple: in an instance method, you can add a type annotation for
selfwhich is a type variable, and make the return type use that same type variable, e.g.It should also work for class methods, using
Type[], e.g.Note that the Python 2 annotation syntax already supports this, e.g.
(IOW if you have a method with
selfand three arguments, you can give it either 3 or 4 arguments in the# type:comment, and if 4, the first will be used forself. Ditto for class methods.)