在我的项目的主脚本中,gettext安装_()在其他模块中用于翻译的函数(如in print(_('Something to translate'))).
正如文件所述:
_()函数[是]安装在Python的内置命名空间中,因此可以在应用程序的所有模块中轻松访问它.
所以,一切都运行良好.
唯一的问题:flake8显示错误(实际上由PyFlakes返回):
$ flake8 *.py
lib.py:2:12: F821 undefined name '_'
main_script.py:8:7: F821 undefined name '_'
Run Code Online (Sandbox Code Playgroud)
这是正常的,因为_在main_script.py和lib.py中确实没有定义.
.
??? lib.py
??? locale
? ??? de
? ??? LC_MESSAGES
? ??? myapp.mo
? ??? myapp.po
??? main_script.py
Run Code Online (Sandbox Code Playgroud)
lib.py包含这个:
def fct(sentence):
return _(sentence)
Run Code Online (Sandbox Code Playgroud)
和main_script.py这个:
#!/usr/bin/env python3
import gettext
import lib
gettext.translation('myapp', 'locale', ['de']).install()
print(_('A sentence'))
print(lib.fct('A sentence'))
Run Code Online (Sandbox Code Playgroud)
和myapp.po包含:
msgid ""
msgstr ""
"Project-Id-Version: …Run Code Online (Sandbox Code Playgroud) 我们用它flake8来测试我们的代码,我们pytest用夹具。以下代码:
from staylists.tests.fixtures import fixture1 # noqa: F401
def test_case(fixture1): # noqa: F811
# Test goes here
assert 1 == 1
Run Code Online (Sandbox Code Playgroud)
lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1在 linting 期间生成错误。
在Python中,留下这样的尾随逗号当然不是SyntaxError:
In [1]: x = 1 ,
In [2]: x
Out[2]: (1,)
In [3]: type(x)
Out[3]: tuple
Run Code Online (Sandbox Code Playgroud)
但是,与此同时,如果尾随的逗号被意外添加,可能很难捕捉到这种"问题",特别是对于新手来说.
我想我们是否可以在智能代码质量控制功能的帮助下,早期,静态地捕捉到这种"问题"PyCharm ; mypy,pylint或flake8静态代码分析工具.
或者,另一个想法是限制/突出显示一个项目元组隐式没有括号.可能吗?
我有我的flake8配置文件~/.config/flake8
[flake8]
max-line-length = 100
Run Code Online (Sandbox Code Playgroud)
但是,当我运行flake8配置文件时,没有拿起.我知道,因为我仍然会收到超过79字符的警告.
我在使用redhat,但在mac上也是如此.
我用pyenv.全球是2.7.6(甚至不确定这是否相关)
我在“ added_parts = new_part_set[(new_part_set["duplicate"] == False) & (new_part_set["version"] == "target")]"**行得到E712的flake 8错误
以下是我们用于电子表格比较的代码片段
source_df = pd.read_excel(self.source, sheet).fillna('NA')
target_df = pd.read_excel(self.target, sheet).fillna('NA')
file_path = os.path.dirname(self.source)
column_list = source_df.columns.tolist()
source_df['version'] = "source"
target_df['version'] = "target"
source_df.sort_values(by=unique_col)
source_df = source_df.reindex()
target_df.sort_values(by=unique_col)
target_df = target_df.reindex()
# full_set = pd.concat([source_df, target_df], ignore_index=True)
diff_panel = pd.concat([source_df, target_df],
axis='columns', keys=['df1', 'df2'], join='outer', sort=False)
diff_output = diff_panel.apply(self.__report_diff, axis=0)
diff_output['has_change'] = diff_output.apply(self.__has_change)
full_set = pd.concat([source_df, target_df], ignore_index=True)
changes = full_set.drop_duplicates(subset=column_list, keep='last')
dupe_records = changes.set_index(unique_col).index.unique()
changes['duplicate'] = changes[unique_col].isin(dupe_records)
removed_parts = changes[(changes["duplicate"] …Run Code Online (Sandbox Code Playgroud) 有没有办法忽略指定目录中发生的错误?
例如,目录D103 Missing docstring in public function内的每个文件都有错误/foo,我想忽略该错误。
是否可以在setup.cfg文件中设置这样的设置?
我flake8用于 linting 和black格式化。
flake8对执行规则和格式规则产生警告:
我只想看执行规则。我不关心格式规则,因为它们会通过black格式自动修复:
我可以通过运行一次禁用这些规则flake8 --ignore=E271,E225,W291,E231。但是,没有要禁用的格式规则的详尽列表,必须一次发现一个。拥有类似于 JavaScript 的 eslint 的东西会很棒,其中有一个插件可以禁用代码格式化程序涵盖的所有格式化规则。
有没有类似的方法来禁用所有格式规则的flake8?
解决方案:根据以下答案,我最终忽略了使用此命令的所有格式规则:
flake8 --ignore=E101,E111,E114,E115,E116,E117,E12,E13,E2,E3,E401,E5,E70,W1,W2,W3,W5 file.py
Run Code Online (Sandbox Code Playgroud) 我在 Python 中的函数的文档字符串中添加了乳胶数学表达式。它会触发错误“W605 无效转义序列”,从而破坏 flake8 检查。如何修复它?
"""
The test function is defined as:
\sin(\pi x)/x
"""
Run Code Online (Sandbox Code Playgroud)
我现在通过使用双斜杠解决了这个问题。
我在 Python 中使用包 pydantic 和链接器 Flake8。我想将来自 pydantic 的 constr 与常规 Experssion 一起使用。只应传递某些字符。(az、AZ、0-9 和 _)
常规"^[a-zA-Z0-9_]*$"Experssion 有效,但 flake8 向我显示以下错误:
前向注释中的语法错误 '^[a-zA-Z0-9_]*$' flake8(F722)
class RedisSettings(BaseModel):
keyInput: constr(regex="^[a-zA-Z0-9_]*$") = ""
keyOutput: constr(regex="^[a-zA-Z0-9_]*$") = ""
Run Code Online (Sandbox Code Playgroud)
你能帮我避免错误信息吗?
我有一个条件,如下所示:
ok = (not a > 10 and
not b < 10 and
not c > 99 and
d == 99)
Run Code Online (Sandbox Code Playgroud)
flake8 抱怨这一行并显示错误消息:
W504 二元运算符后换行
当我移动操作员时,它会抛出不同的错误:
ok = (not a > 10
and not b < 10
and not c > 99
and d == 99)
Run Code Online (Sandbox Code Playgroud)
W503 二元运算符前换行
我尝试了多个建议(例如,this),但 flake8 仍然抱怨换行。我的代码中的实际情况非常长,因此我无法将其放在一行中,而且我的团队更喜欢将长行括起来()而不是使用\.
flake8 ×10
python ×9
pep8 ×2
python-3.x ×2
formatting ×1
gettext ×1
pandas ×1
pycharm ×1
pydantic ×1
pyflakes ×1
pylint ×1
python-2.7 ×1
python-black ×1