我正在将black
并flake8
作为 git 中的钩子添加到我的 python 项目中pre-commit
,除了F841
警告之外,一切看起来都很好。我添加了# noqa: F841
以避免警告但仍然得到它。
有谁知道如何解决这个问题,或者我做错了什么?
在有根容器中,此问题的解决方案是运行--user "$(id -u):$(id -g)"
,但这不适用于无根包含系统(无根 docker,或在我的情况下为 podman):
$ mkdir x
$ podman run --user "$(id -u):$(id -g)" -v "$PWD/x:/x:rw" ubuntu:focal bash -c 'echo hi >> /x/test'
bash: /x/test: Permission denied
Run Code Online (Sandbox Code Playgroud)
因此,对于无根容器系统,我应该删除--user
,因为根用户会自动映射到调用用户:
$ podman run -v "$PWD/x:/x:rw" ubuntu:focal bash -c 'echo hi >> /x/test'
$ ls -al x
total 12
drwxr-xr-x 2 asottile asottile 4096 Sep 3 10:02 .
drwxrwxrwt 18 root root 4096 Sep 3 10:01 ..
-rw-r--r-- 1 asottile asottile 3 Sep 3 10:02 test …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 flakehell 作为预提交钩子运行。
我的 .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: flakehell
name: flakehell
entry: flakehell lint project/
language: python
pass_filenames: true
stages: [push, commit]
verbose: true
Run Code Online (Sandbox Code Playgroud)
pyproject.toml:
[tool.flakehell]
exclude = ["README.md"]
format = "colored"
show_source = true
[tool.flakehell.plugins]
flake8-bandit = ["+*", "-S322"]
flake8-bugbear = ["+*"]
flake8-builtins = ["+*"]
flake8-comprehensions = ["+*"]
flake8-darglint = ["+*"]
flake8-docstrings = ["+*"]
flake8-eradicate = ["+*"]
flake8-isort = ["+*"]
flake8-mutable = ["+*"]
flake8-pytest-style = ["+*"]
flake8-spellcheck = ["+*"]
mccabe = ["+*"]
pep8-naming = ["+*"] …
Run Code Online (Sandbox Code Playgroud) 我的 python 代码中有这样的定义:
options = defaultdict(lambda: defaultdict(lambda: defaultdict(str)))
options['modem1']['ByCost'] = ...
options['modem1']['ByCarrier'] = ...
options['modem1']['BySimSignal'] = ...
options['modem1']['SwitchBackByTimer'] = ...
Run Code Online (Sandbox Code Playgroud)
在我的预提交/mypy 检查期间它失败了,因为消息如下:
Need type annotation for 'options'
Run Code Online (Sandbox Code Playgroud)
由于我没有看到这段代码有任何问题,那么有什么解决办法可以让它通过 mypy 检查这一行吗?
顺便说一句,除了在我的代码中设置之外,我正在寻找其他方法:
disable_error_codes with error code var-annotated
Run Code Online (Sandbox Code Playgroud)
多谢 !
杰克
预提交是一个很棒的工具,我喜欢将其合并到詹金斯管道中,以确保有人在提交到存储库期间不会尝试跳过它。
所以我的问题是:
success
或fail
?我正在开发一个pre-commit==2.15.0
已添加到 python 需求文件中的项目。我安装了要求。现在,当我尝试执行以下操作时,git commit
出现以下错误:
An unexpected error has occurred: ExecutableNotFoundError: Executable `/bin/sh` not found
Check the log at C:\Users\username\.cache\pre-commit\pre-commit.log
Run Code Online (Sandbox Code Playgroud)
在我的预提交日志中,我有:
pre-commit version: 2.15.0
sys.version:
3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]
sys.executable: c:\users\username\appdata\local\programs\python\python39\python.exe
os.name: nt
sys.platform: win32
Traceback (most recent call last):
File "c:\users\username\appdata\local\programs\python\python39\lib\site-packages\pre_commit\error_handler.py", line 65, in error_handler
yield
File "c:\users\username\appdata\local\programs\python\python39\lib\site-packages\pre_commit\main.py", line 368, in main
return hook_impl(
File "c:\users\username\appdata\local\programs\python\python39\lib\site-packages\pre_commit\commands\hook_impl.py", line 231, in hook_impl
retv, stdin = _run_legacy(hook_type, hook_dir, args)
File "c:\users\username\appdata\local\programs\python\python39\lib\site-packages\pre_commit\commands\hook_impl.py", line …
Run Code Online (Sandbox Code Playgroud) 有没有办法pytest.mark
在预提交挂钩期间排除标记为 的 pytests 运行?特别是,我想排除标记为集成测试的测试。
测试的内容如下所示
pytestmark = [pytest.mark.integration, pytest.mark.reporting_api]
### some tests
Run Code Online (Sandbox Code Playgroud)
配置.pre-commit-conifg.yaml
pytest
是
- repo: local
hooks:
- id: pytest
name: pytest
entry: pytest test/
language: system
pass_filenames: false
types: [python]
stages: [commit, push]
Run Code Online (Sandbox Code Playgroud) 我正在为 sqlite3 使用 python 绑定,我正在尝试执行这样的查询
表格1
col1 | col2
------------
aaaaa|1
aaabb|2
bbbbb|3
Run Code Online (Sandbox Code Playgroud)
测试文件
def get_rows(db, ugc):
# I want a startswith query. but want to protect against potential sql injection
# with the user-generated-content
return db.execute(
# Does not work :)
"SELECT * FROM table1 WHERE col1 LIKE ? + '%'",
[ugc],
).fetchall()
Run Code Online (Sandbox Code Playgroud)
有没有办法安全地做到这一点?
预期行为:
>>> get_rows('aa')
[('aaaaa', 1), ('aaabb', 2)]
Run Code Online (Sandbox Code Playgroud) $ pip install pyttsx3
Collecting pyttsx3
Using cached pyttsx3-2.80-py3-none-any.whl (39 kB)
Collecting pyobjc>=2.4
Using cached pyobjc-6.1-py3-none-any.whl (2.9 kB)
Collecting comtypes; "win32" in sys_platform
Using cached comtypes-1.1.7.zip (180 kB)
Collecting pyobjc-framework-SyncServices==6.1
Using cached pyobjc-framework-SyncServices-6.1.tar.gz (42 kB)
ERROR: Command errored out with exit status 1:
command: 'c:\users\abrar\appdata\local\programs\python\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Abrar\\AppData\\Local\\Temp\\pip-install-e8rlz1t4\\pyobjc-framework-SyncServices\\setup.py'"'"'; __file__='"'"'C:\\Users\\Abrar\\AppData\\Local\\Temp\\pip-install-e8rlz1t4\\pyobjc-framework-SyncServices\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\Abrar\AppData\Local\Temp\pip-install-e8rlz1t4\pyobjc-framework-SyncServices\pip-egg-info'
l = plistlib.readPlist("/System/Library/CoreServices/SystemVersion.plist")
File "c:\users\abrar\appdata\local\programs\python\python3
Run Code Online (Sandbox Code Playgroud)
我在 python 3.7.3 上尝试这个并使用最新的 pip 版本,但我不明白如何修复它
更新到最新版本后,我在 Mac 操作系统中收到以下提交错误。尝试重新启动 VS Code,也尝试使用终端,但出现同样的错误。直到git add .
这一步工作正常。只有提交命令无法正常工作。可能是什么问题?
git commit -m "Commit message"
.git/hooks/pre-commit: line 15: python: command not found
.git/hooks/pre-commit: line 16: python: command not found
Run Code Online (Sandbox Code Playgroud) python ×7
git ×3
pre-commit ×3
python-3.x ×2
commit ×1
docker ×1
flake8 ×1
git-commit ×1
jenkins ×1
mypy ×1
pip ×1
podman ×1
pytest ×1
python-3.6 ×1
pyttsx ×1
rootless ×1
sqlite ×1