尽管使用了“from __future__ import comments”,但 Flake8 仍给出 F832 Undefined Name
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误没有错误:
__future__
annotations
defers annotations 但你有一个默认值(这不是一个注释!)引用文件后面的类中的一个选项是简单的重新排序:
更好的是完全省略默认值,因为它在 pyi (或存根函数)中不执行任何操作:
另请注意,您的函数稍微不正确 - 现在是类型检查器会解释
set_amount
为-> Any
因为您省略了返回值,所以正确的做法是->; None
您可以采取的其他途径是通过 # noqa、
per-file-ignores
或其他方式忽略错误 - 尽管这可能不是这样你想做的事情,因为代码原样已损坏免责声明:我目前维护 flake8
the error is not wrong:
__future__
annotations
defers annotations but you've got a default value (which is not an annotation!) referring to a class later in the fileone option is a simple reordering:
even better is to omit the default value entirely, since it does nothing in a pyi (or stub function):
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 brokendisclaimer: I currently maintain flake8