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
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 C
It 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 D
Note 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.)
The text was updated successfully, but these errors were encountered:
I think this is a good idea. As I understand, not much should be changed in PEP 484. The main part would be to implement this in mypy. I could not help with mypy now, but I would like to make a PR with changes in the PEP text.
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.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
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 forself
. Ditto for class methods.)The text was updated successfully, but these errors were encountered: