Below is my code:
1 #!/usr/bin/env python
2
3 from typing import TypeVar, Generic
4
5 # T = TypeVar('T')
6 T = TypeVar('T', str, int)
7
8 class MyClass(Generic[T]):
9
10 def __init__(self, value: T)-> None:
11 self.value = value
When I run mypy against this code, I got below error:
test.py:11: error: Need type annotation for 'value'
But If I change the line 6 to line 5, mypy won't report any error.
Is it a bug or I misunderstand anything?
related packages version:
$ python --version Python 3.5.2 $ pip freeze mypy==0.580 typed-ast==1.1.0
This is a special case. Here in fact the inferred type for self.value is ambiguous, for example both self.value: T = value and self.value: Union[int, str] = value would work. Therefore mypy asks you what do you actually mean here.
thanks for your explain, the python 3.5 doesn't support variable annotation, so maybe I should just ignore this error.
In Python 3.5 you can write self.value = value # type: T mypy treats such comment as an equivalent of a variable annotation.