尽管使用 "from __future__ import annotations",Flake8 仍给出 F832 Undefined Name

Flake8 gives F832 Undefined Name despite using "from __future__ import annotations"

我有这个 pyi 文件。

from __future__ import annotations
import datetime


class Payment:
    amount: float = ...
    date: datetime.date = ...

    def set_amount(self: Payment, amount: float = Amounts.Null):
        ...

    def get_amount(self: Payment) -> float:
        ...

    def get_date(self: Payment) -> datetime.date:
        ...


class Amounts:
    Null: float = ...

使用 from __future__ import annotations 有助于消除 self 参数的 F821 undefined name 'Payment' 警告。

但是flake8还是显示F821 undefined name 'Amounts'。能否以某种方式修复此警告(无需完全禁用 F821)?或者这可能是一个错误?


请注意,我在 Python 3.10.0 上使用 flake8 4.0.1 (mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0)

错误没有错:

$ python3 t.pyi
Traceback (most recent call last):
  File "t.pyi", line 5, in <module>
    class Payment:
  File "t.pyi", line 9, in Payment
    def set_amount(self: Payment, amount: float = Amounts.Null):
NameError: name 'Amounts' is not defined

__future__ annotations 延迟 annotations 但你有一个默认值(这不是注释!)引用 class 稍后在文件中

一个选项是简单的重新排序:

from __future__ import annotations
import datetime


class Amounts:
    Null: float = ...


class Payment:
    amount: float = ...
    date: datetime.date = ...

    def set_amount(self: Payment, amount: float = Amounts.Null):
        ...

# ...

更好的是完全省略默认值,因为它在 pyi(或存根函数)中什么都不做:

from __future__ import annotations
import datetime


class Payment:
    amount: float = ...
    date: datetime.date = ...

    def set_amount(self: Payment, amount: float = ...):

# ...

另请注意,您的函数有一点不正确——现在类型检查器会将 set_amount 解释为 -> Any,因为您省略了 return 值,这是正确的做法是 -> None

您可以采取的另一条途径是通过 # noqaper-file-ignores 或其他方式忽略错误——尽管这可能不是您想要做的,因为代码 as-is坏了


免责声明:我目前维护 flake8