-2

I have such code in python 3.7, as :

try:
    import json
except ImportError:
    import simplejson as json

And once I am running pre-commit, I have received such failure:

testcase.py:57: error: Name 'json' already defined (by an import)

my pre-commit is 1.20.0 on Ubuntu 16.04. Is there a way to make it pass ?

Here is my pre-commit command:

pre-commit run --all-files

Question update.

1) following is my .pre-commit-config.yaml file.

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) And when I was executing pre-commit , following is the log message:

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
user3595231
  • 711
  • 12
  • 29
  • 3
    Could you please add what is in your pre-commit config to the question? – alecxe Nov 11 '19 at 19:02
  • 1
    alecxe, I have just added it in my original question. Thanks. – user3595231 Nov 11 '19 at 19:45
  • 2
    you'll need to attach the full output and the `.pre-commit-config.yaml` which is leading to this – anthony sottile Nov 11 '19 at 21:13
  • 2
    This has nothing to do with Git itself, but rather with the Python linter(s) you are using in your pre-commit hook. All we can tell you in Git-land is that you can bypass the pre-commit hook entirely, or remove it entirely. – torek Nov 11 '19 at 21:29
  • 1
    What linter do you use? `pylint`? `flake8`? – phd Nov 11 '19 at 22:02
  • 1
    Anthony, I have just updated message. – user3595231 Nov 11 '19 at 22:16
  • 1
    Hi phd. As I listed above, I am not sure it is of pylint or flack8. I was using the default ".pre-commit-config.yaml" after pre-commit is installed. But somehow, it fails that piece of code. – user3595231 Nov 12 '19 at 01:29
  • 1
    If I understand correctly, pre-commit is to wrap up all the necessary checking tools and end user just to run it in a black box way, right ? – user3595231 Nov 12 '19 at 01:56

1 Answers1

0

After spent hours to try to figure out what is going on as a Beginner user, I realize it was that "mypy" that is the culprit. So I think there are 2 options for me:

  1. To have mypy disabled in the ".pre-commit-config.yaml" file, so that my pre-commit will be skip this checking.
  2. To add some "# type: ignore" at the end of such "failed" line as suggested in the doc.

I have seen lot of issues by using mypy, from this link: https://github.com/python/mypy/issues, as to me, this is not a very mature checking tool. so I am choosing option 1 for now.

Thanks all for the help.

user3595231
  • 711
  • 12
  • 29