1

I'm new to python and experimenting with type hints, however they only seem to work in some instances. They seem to work as expected on the property return type, however when I try to assign an integer to a string value (i.e. self._my_string = 4), I get no issues reported.

class TypeHintTest(object):
    _my_string: str

    def __init__(self):
        self._my_string = 4  # no error

    @property
    def as_int(self) -> int:  
        return self._my_string  # Error : expected int got str

The resulting object then contains an int value (as expected).

I'm using pyCharm 2018.3.2 Community edition, the interpreter is 3.6

enter image description here

The following question seems to be similar, but the solution of changing the constructor to def __init__(self) -> None does not change anything. Python: All type hints errors in subclass constructure seems ignored

4

2 回答 2

3

顾名思义,类型提示就是提示。如果为变量分配不同的类型,Python 不会引发错误。
然而,Pycharm应该说它需要另一种变量类型。

于 2019-01-03T10:50:15.490 回答
2

正如@FHTMitchell 指出它在 PyCharm 中的一个错误

请参阅错误跟踪器条目

于 2019-01-28T16:26:24.180 回答