[Python] Pre-Commit MyPy unable to disable non-error messages

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm putting together some pre-commit hooks for a project that I'm working on, and one of the hooks we want to use is MyPy. The pre-commit results are throwing up a lot of non-error notes related to the "annotation-unchecked" flag, which I want to disable to de-clutter the command line output.

    I have seen from a previous question Suppress Mypy notes that the solution would be to include a disable_error_code = ["annotation-unchecked"] in the pyproject.toml file, however, the notes still appear when running the pre-commit.

    These are the current references to MyPy in the pyproject.toml file:

    [tool.mypy]
    mypy_path = "src"
    disable_error_code = ["annotation-unchecked"] # Disables non-error messages thrown up by mypy


    And these are the settings from the pre-commit-config.yaml file:

    # Type checking
    - repo: https://github.com/pre-commit/mirrors-mypy
    #rev: v0.910
    rev: v1.9.0
    hooks:
    - id: mypy
    files: '(src|tests)/.*\.py$' # Single quote critical due to escape character '\' used in RegEx search string (see YAML - 7.3 Flow Scalar Styles)
    additional_dependencies: [types-requests]


    These are the MyPy messages printed out in the command line. Note how the "notes" are still being printed despite the settings above.

    src/pkg/file1.py:14: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type]
    src/pkg/file1.py:21: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type]
    src/pkg/file1.py:23: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type]
    src/pkg/file2.py:60: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file3.py:143: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file4.py:125: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file4.py:126: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file5.py:20: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file5.py:21: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file6.py:75: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file7.py:164: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    src/pkg/file8.py:709: error: Value of type "Coroutine[Any, Any, None]" must be used [unused-coroutine]
    src/pkg/file9.py:709: note: Are you missing an await?
    src/pkg/file10.py:267: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
    Found 4 errors in 2 files (checked 72 source files)


    Advice on what might have gone wrong here would be much appreciated, thanks!


    It turns out this issue was caused by the presence of a .mypy.ini file in the project folder that I overlooked. Deleting it solved the problem.

    When multiple configuration files are present in the project folder, MyPy looks for settings in the following order of priority (ref):

    1. ./mypy.ini
    2. ./.mypy.ini
    3. ./pyproject.toml
    4. ./setup.cfg
    5. $XDG_CONFIG_HOME/mypy/config
    6. ~/.config/mypy/config
    7. ~/.mypy.ini

    Where multiple such files exist, @anit3res' answer of adding an argument to the .pre-commit-config.yaml file specifying the config file to look at works beautifully:

    - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.9.0
    hooks:
    - id: mypy
    files: '(src|tests)/.*\.py$' # Single quote critical due to escape character '\' used in RegEx search string (see YAML - 7.3 Flow Scalar Styles)
    additional_dependencies: [types-requests]
    args: [--config-file=./pyproject.toml]

    Continue reading...

Compartilhe esta Página