diff_cover icon indicating copy to clipboard operation
diff_cover copied to clipboard

Mypy ignores fail diff-cover

Open Lyranile opened this issue 7 months ago • 5 comments

When doing mypy checks like the following

mypy .

It shows me problems based on the flags I give it

When using it with diff-cover it actually fails on any line that I explicitly wanted to ignore with mypy

That means if I do ignore-follow-imports, diff-cover will show problems on changes lines with any imported module

I assume that is because it's not covered, but I'm looking for coverage to take these flags as well

How would I go about doing this?

Lyranile avatar Jul 08 '25 06:07 Lyranile

Try putting your config in a config file. Diff cover does not have a mechanism to forward flags to the underlying tool

https://mypy.readthedocs.io/en/stable/config_file.html

Bachmann1234 avatar Jul 08 '25 12:07 Bachmann1234

I do have my configuration stored in pyproject, but diff-cover ignores that configuration What I'm trying to say is when I do ignore_missing_imports, allow_untyped_globals or do any kind of ignore of mypy, coverage will ignore that, leading to failures on missing imports (despite being ignored) I believe it is because it is claiming that part of the code is not covered by mypy, which it's obviously not because I'm ignoring it since some module like logging is untyped

Am I understanding it correctly? doesn't it defeat the whole point of the tool? Seeing as you only take changes from specific files because of git-diff but then checking other files because they were imported I was expecting it to only see what lines are covered out of what mypy could even cover in the first place

Is there a way perhaps to keep just the feature of doing mypy only on the git-diff changes without involving coverage?

I'm wondering if I'm the only one facing such issues

Lyranile avatar Jul 15 '25 12:07 Lyranile

@Lyranile so I could had sworn we had a mypy tool but when I actually tried to look into this mypy is not a tool supported by diff-quality. So can I ask what you are doing when you say "using mypy with diff cover"

What command are you running exactly?

Bachmann1234 avatar Jul 17 '25 02:07 Bachmann1234

STATIC_TYPING_PYTHON_VERSION=3.13
FAIL_THRESHOLD=100
MYPY_ARGS="--explicit-package-bases --strict --follow-imports=silent --ignore-missing-imports --check-untyped-defs --disallow-untyped-calls --disallow-untyped-defs --warn-unused-ignores --warn-return-any --python-version ${STATIC_TYPING_PYTHON_VERSION}"

mypy $MYPY_ARGS --cobertura-xml-report mypy_report --no-error-summary . || echo "ignore errors"

diff-cover mypy_report/cobertura.xml --compare-branch origin/$MAIN_BRANCH  --fail-under=$FAIL_THRESHOLD

All mypy args are being ignored and fail on the pipeline, they do work with mypy but not with coverage

Lyranile avatar Jul 17 '25 06:07 Lyranile

@Lyranile took a stab at reproducing this

I vibed up a dummy repo https://github.com/Bachmann1234/issue512/tree/branch2

on this branch I made two scripts based on your script

https://github.com/Bachmann1234/issue512/blob/branch2/allow_missing_imports.sh https://github.com/Bachmann1234/issue512/blob/branch2/disallow_missing_imports.sh

When I run both of these I see the behavior you are talking about

❯ ./disallow_missing_imports.sh
mixed_module.py:4: error: Cannot find implementation or library stub for module named "nonsense"  [import-not-found]
mixed_module.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
mixed_module.py:9: error: Name "random" is not defined  [name-defined]
mixed_module.py:12: error: Function is missing a type annotation  [no-untyped-def]
mixed_module.py:34: error: Need type annotation for "items" (hint: "items: list[<type>] = ...")  [var-annotated]
mixed_module.py:36: error: Function is missing a type annotation  [no-untyped-def]
venv/bin/activate_this.py:33: error: "str" has no attribute "decode"; maybe "encode"?  [attr-defined]
venv/bin/activate_this.py:36: error: Module has no attribute "real_prefix"  [attr-defined]
untyped_module.py:7: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:15: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:22: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"  [arg-type]
untyped_module.py:29: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:33: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:37: error: Call to untyped function "_transform_item" in typed context  [no-untyped-call]
untyped_module.py:41: error: Function is missing a type annotation  [no-untyped-def]
Generated Cobertura report: /Users/bachmann/code/issue512/mypy_report/cobertura.xml
ignore errors
Failure. Coverage is below 100%.
-------------
Diff Coverage
Diff: origin/main...HEAD, staged and unstaged changes
-------------
mixed_module.py (0.0%): Missing lines 4
-------------
Total:   1 line
Missing: 1 line
Coverage: 0%
-------------
❯ ./allow_missing_imports.sh
mixed_module.py:9: error: Name "random" is not defined  [name-defined]
mixed_module.py:12: error: Function is missing a type annotation  [no-untyped-def]
mixed_module.py:34: error: Need type annotation for "items" (hint: "items: list[<type>] = ...")  [var-annotated]
mixed_module.py:36: error: Function is missing a type annotation  [no-untyped-def]
venv/bin/activate_this.py:33: error: "str" has no attribute "decode"; maybe "encode"?  [attr-defined]
venv/bin/activate_this.py:36: error: Module has no attribute "real_prefix"  [attr-defined]
untyped_module.py:7: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:15: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:22: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"  [arg-type]
untyped_module.py:29: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:33: error: Function is missing a type annotation  [no-untyped-def]
untyped_module.py:37: error: Call to untyped function "_transform_item" in typed context  [no-untyped-call]
untyped_module.py:41: error: Function is missing a type annotation  [no-untyped-def]
Generated Cobertura report: /Users/bachmann/code/issue512/mypy_report/cobertura.xml
ignore errors
Failure. Coverage is below 100%.
-------------
Diff Coverage
Diff: origin/main...HEAD, staged and unstaged changes
-------------
mixed_module.py (0.0%): Missing lines 4
-------------
Total:   1 line
Missing: 1 line
Coverage: 0%
-------------

So we see the mypy text report is different (mixed_module.py:4: error: Cannot find implementation or library stub for module named "nonsense" [import-not-found] is only there when we dont allow missing imports.

However, when I compared the xml reports were identical (except for timestamp of course).

Diff-cover relies on the xml report for its analysis. So I think this issue with with mypy. The arguments dont seem to affect its report.

Bachmann1234 avatar Jul 20 '25 17:07 Bachmann1234