类型提示在 pycharm 2018.1 中并不总是有效? [英] Type hinting not always working in pycharm 2018.1?
问题描述
我今天开始使用类型提示.在阅读了关于类型提示的文档后,我尝试写一些愚蠢的例子来检查它是如何工作的,但被困在像这样简单的事情上.
I have started to use type hinting today. After reading documentation about type hinting I tried to write some dumb examples to check how it works and was stuck at something as simple as this.
a: int = 7.33
我没有收到任何警告或错误.一切正常,就像我没有使用类型提示一样.我期待一个警告说不能将浮点数分配给 int var.
I don't get any warning or error. Everything is working like I wasn't using type hinting. I was expecting a warning saying that a float can't be assigned to an int var.
我尝试过其他类似的方法:
I have tried other things like:
def sum_two_numbers(a:int , b:int)->int:
return a+b
def main() -> None:
asd = sum_two_numbers(2, 5.4)
正如预期的那样,我收到了 5.4 编号的警告.Pycharm 警告我正在传递一个浮点数,而 int 应该是.
and as expected I get a warning in the 5.4 number. Pycharm warns that I'm passing a float were an int should be.
那么,这里发生了什么?
So, what is happening here?
我已经尝试过 mypy 并且它正确地检测到了我之前提到的问题.有没有办法让pycharm也检测到它?
Edited: I Have tried mypy and it correctly detects the previous problem I stated. Is there anyway to make pycharm detect it too?
干杯.
推荐答案
不幸的是,PyCharm 不检查变量赋值的类型.请投票给 PY-24832.
PyCharm doesn't check types for variable assignments, unfortunately. Please vote for PY-24832.
这篇关于类型提示在 pycharm 2018.1 中并不总是有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!