【问题标题】:mypy does not recognize attrs: Cannot determine type of 's'mypy 无法识别 attrs:无法确定“s”的类型
【发布时间】:2020-05-16 11:38:05
【问题描述】:
> pip3.8 list
Package           Version
----------------- -------
attrs             19.3.0
mypy              0.761
mypy-extensions   0.4.3
pip               19.3.1
setuptools        42.0.2
typed-ast         1.4.1
typing-extensions 3.7.4.1
wheel             0.33.6
> mypy --version
mypy 0.761
# types.py
import typing
import attr

@attr.s(auto_attribs=True, slots=True, frozen=True)
class Test:
    b: bool = False
> mypy types.py
types.py:4: error: Cannot determine type of 's'
Found 1 error in 1 file (checked 1 source file)

我是否需要安装其他东西以获得attrs 支持?

【问题讨论】:

    标签: python python-3.x mypy python-attrs


    【解决方案1】:

    嘿,这很奇怪,可能是 mypy 中的一个错误!将 types.py 重命名为其他名称即可:

    $ mypy types.py
    types.py:4: error: Cannot determine type of 's'
    Found 1 error in 1 file (checked 1 source file)
    $ mv types.py t.py
    $ mypy t.py
    Success: no issues found in 1 source file
    

    电脑! ¯\_(ツ)_/¯

    【讨论】:

    • 我创建了一个 GitHub 问题 here。谢谢,我可能会考虑尝试一下。
    • 它可能在后台使用 stdlib module types 被具有相同名称的用户模块覆盖
    • 这是可能的。 attrs 在检测 ClassVars 时也会进行一些粗略的字符串比较,因为实际上导入它非常慢。
    猜你喜欢
    • 2021-02-03
    • 2021-10-22
    • 1970-01-01
    • 2022-01-13
    • 2019-12-26
    • 2022-06-14
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多