为什么错误的类型提示在代码中不在调试控制台中时可以很好地工作?

发布于 2025-01-19 02:15:11 字数 1477 浏览 2 评论 0原文

我试图做的是:

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: List[List[int]] = [] 

t = Board(3, 4)

我像这样输错了。

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: list[list[int]] = [] # typo here

t = Board(3, 4)

我认为会发生错误,因为据我所知,在 python 3.9 中引入了使用类型(列表、元组、字典...)作为类型提示,而我正在使用 3.7.4。但相反没有发生错误。觉得这很奇怪,所以我尝试了这个:

a: list[int] = [0]

并且......

类型错误:“类型”对象不可订阅

这是我在输入错误时所期望的(列表->列表)。我在原始代码中放置了一个断点:

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: list[list[int]] = [] 
        ... # breakpoint here
        
t = Board(3, 4)

并在调试控制台中...

self.layout: list[list[int]] = []

发生了同样的错误。

类型错误:“类型”对象不可订阅

问题:如果在我的 python 版本 (3.7.4) 中不允许使用 'list' 作为类型提示,为什么我的原始代码运行时没有错误?

谢谢。

附:也许这更奇怪......

from typing import List

class Test():
    def __init__(self):
        a: list[int] = [0] # it works as a = [0]

if __name__ == "__main__":
    t = Test()
    b: list[int] = [0] # TypeError occurs. Why?

What I tried to do was:

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: List[List[int]] = [] 

t = Board(3, 4)

and I mistyped like this.

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: list[list[int]] = [] # typo here

t = Board(3, 4)

I thought an error would occur because to my knowledge using types(list, tuple, dict, ...) as type hints is introduced in python 3.9 and I'm using 3.7.4. But on contrary no error occurred. Thought it's weird so I tried this:

a: list[int] = [0]

and...

TypeError: 'type' object is not subscriptable

which I expected at first when I made typo (List->list). I put a breakpoint in the original code:

from typing import List
class Board():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.layout: list[list[int]] = [] 
        ... # breakpoint here
        
t = Board(3, 4)

and in debug console...

self.layout: list[list[int]] = []

same error occurred.

TypeError: 'type' object is not subscriptable

Question: If using 'list' as type hints is not allowed in my python version (3.7.4), why did my original code run without errors?

Thank you.

ps. maybe this is weirder...

from typing import List

class Test():
    def __init__(self):
        a: list[int] = [0] # it works as a = [0]

if __name__ == "__main__":
    t = Test()
    b: list[int] = [0] # TypeError occurs. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-26 02:15:11

来自 类型注释的运行时效果

不会评估局部变量的注释:

<前><代码>def f():
x: NonexistentName # 没有错误。

但是,如果它是在模块或类级别,则类型
评价:

x: NonexistentName # 错误!
X类:
    var: NonexistentName # 错误!

From Runtime Effects of Type Annotations:

Annotations for local variables will not be evaluated:

def f():
    x: NonexistentName  # No error.

However, if it is at a module or class level, then the type will be
evaluated:

x: NonexistentName  # Error!
class X:
    var: NonexistentName  # Error!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文