[英]How to have a better code to pass the pre-commit check?
我在 python 3.7 中有這樣的代碼,如:
try:
import json
except ImportError:
import simplejson as json
一旦我運行預提交,我就會收到這樣的失敗:
testcase.py:57: error: Name 'json' already defined (by an import)
我的預提交是 Ubuntu 16.04 上的 1.20.0。 有沒有辦法讓它通過?
這是我的預提交命令:
pre-commit run --all-files
問題更新。
1) 以下是 my.pre-commit-config.yaml 文件。
default_language_version:
python: python3.7
exclude: ^(creds/|utils/license_keygen/)|/migrations/|/node_modules/
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v1.24.0
hooks:
- id: pyupgrade
args: [--py3-plus]
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.7.0
hooks:
- id: reorder-python-imports
args: ['--application-directories=apps', --py3-plus]
- repo: https://github.com/asottile/blacken-docs
rev: v1.3.0
hooks:
- id: blacken-docs
additional_dependencies: [black==19.3b0]
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-merge-conflict
- id: check-symlinks
- id: debug-statements
exclude: _pytest/debugging.py
- id: end-of-file-fixer
- id: fix-encoding-pragma
args: [--remove]
- id: name-tests-test
args: [--django]
exclude: ^(apps/fcldinfra/tests/settings/|apps/portal/src/extender/tests/expected_tests_api/)
- id: trailing-whitespace
# conflicts with black?
#- repo: https://github.com/asottile/add-trailing-comma
# rev: v1.4.1
# hooks:
# - id: add-trailing-comma
# args: [--py36-plus]
#- repo: https://gitlab.com/pycqa/flake8
# rev: 3.7.8
# hooks:
# - id: flake8
# args: [--config=.flake8]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.740'
hooks:
- id: mypy
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.4.1
hooks:
# - id: python-no-eval
- id: python-no-log-warn
- id: python-use-type-annotations
#- repo: https://github.com/myint/docformatter
# rev: v1.3
# hooks:
# - id: docformatter
#- repo: https://github.com/PyCQA/pydocstyle
# rev: 4.0.1
# hooks:
# - id: pydocstyle
#- repo: local
# hooks:
# - id: unittest
# name: Check python unit test pass
# entry: pipenv run python -m unittest discover -p *_test.py --start-directory apps -v
# pass_filenames: false
# language: system
# types: [python]
2)當我執行預提交時,以下是日志消息:
pre-commit run --all-files
pyupgrade................................................................Passed
Reorder python imports...................................................Passed
blacken-docs.............................................................Passed
black....................................................................Passed
Check for merge conflicts................................................Passed
Check for broken symlinks................................................Passed
Debug Statements (Python)................................................Passed
Fix End of Files.........................................................Passed
Fix python encoding pragma...............................................Passed
Tests should end in _test.py.............................................Passed
Trim Trailing Whitespace.................................................Passed
mypy.....................................................................Failed
hookid: mypy
.../testcase.py:57: error: Name 'json' already defined (by an import)
Found 1 error in 1 files (checked 500 source files)
use logger.warning(......................................................Passed
type annotations not comments............................................Passed
在花了幾個小時試圖弄清楚作為初學者用戶發生了什么之后,我意識到“mypy”才是罪魁禍首。 所以我認為我有兩個選擇:
我使用 mypy 看到了很多問題,來自這個鏈接: https://github.com/python/mypy/issues ,對我來說,這不是一個非常成熟的檢查工具。 所以我現在選擇選項1。
謝謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.