【问题标题】:Mypy typing errors when upgrading to Twisted 21.2.0升级到 Twisted 21.2.0 时出现 Mypy 输入错误
【发布时间】:2021-06-25 06:17:41
【问题描述】:

尝试将 Twisted 软件包从 20.3.0 升级到 21.2.0。
升级后,在运行 Mypy 时,我在访问 reactor(来自 twisted.internet import reactor)时遇到以下错误:

error: Module has no attribute "run"  [attr-defined]  
error: Module has no attribute "running"  [attr-defined]  
error: Module has no attribute "running"  [attr-defined]  
error: Module has no attribute "callLater"  [attr-defined]  
error: Module has no attribute "callInThread"  [attr-defined]  
error: Module has no attribute "callFromThread"  [attr-defined]  
error: Module has no attribute "spawnProcess"  [attr-defined]  
error: Module has no attribute "spawnProcess"  [attr-defined]

使用reveal_type(reactor) 运行Mypy 时:

Revealed type is '_importlib_modulespec.ModuleType'

我试图了解在不忽略错误的情况下处理这种情况的最佳方法是什么,以便在使用反应器时进行类型检查。
有没有更好的方法来导入反应堆?有没有办法为 Mypy 标记反应器的类型?我应该在每次使用之前将其转换为类型吗?

【问题讨论】:

    标签: twisted mypy python-typing


    【解决方案1】:

    花费了大量的搜索,但我终于在 Twisted 源代码本身中找到了解决方案。在行尾添加以下注释:

    # type: ignore[attr-defined]

    例如:

    reactor.callFromThread(reactor.stop)  # type: ignore[attr-defined]
    

    这将“修复”它。

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 2014-07-16
      • 2016-11-22
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      相关资源
      最近更新 更多