尽管使用了“from __future__ import comments”,但 Flake8 仍给出 F832 Undefined Name

发布于 2025-01-15 16:09:47 字数 800 浏览 2 评论 0原文

我有这个 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 注解 有助于消除 self 参数的 F821 undefined name 'Payment' 警告。

但是,flake8 仍然显示 F821 未定义名称“Amounts”。能否以某种方式修复此警告(无需完全禁用 F821)?或者这可能是一个错误?


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

I have this pyi file.

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 = ...

Using from __future__ import annotations helps remove the F821 undefined name 'Payment' warnings for the self arguments.

However, flake8 still shows F821 undefined name 'Amounts'. Can this warning be fixed somehow (without disabling F821 entirely)? Or it this a bug perhaps?


Note, I am using flake8 4.0.1 (mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) on Python 3.10.0.

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

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

发布评论

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

评论(1

<逆流佳人身旁 2025-01-22 16:09:47

错误没有错误:

$ 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 defers annotations 但你有一个默认值(这不是一个注释!)引用文件后面的类中的

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

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 因为您省略了返回值,所以正确的做法是 ->; None

您可以采取的其他途径是通过 # noqa、per-file-ignores 或其他方式忽略错误 - 尽管这可能不是这样你想做的事情,因为代码原样已损坏


免责声明:我目前维护 flake8

the error is not wrong:

$ 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 defers annotations but you've got a default value (which is not an annotation!) referring to a class later in the file

one option is a simple reordering:

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):
        ...

# ...

even better is to omit the default value entirely, since it does nothing in a pyi (or stub function):

from __future__ import annotations
import datetime


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

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

# ...

note also that your function is subtly incorrect -- right now a type checker will interpret set_amount as -> Any because you omit the return value, the correct thing to do there is -> None

the other route you can take is to ignore the errors via # noqa, per-file-ignores, or other means -- though that's probably not what you want to do since the code as-is is broken


disclaimer: I currently maintain flake8

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