Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mypy: pre-commit results differ from

I am trying to introduce pre-commit into quite a large existing project and replace the existing direct call to mypy with pre-commit, however, the results differ. It would be unreasonable to try and describe everything I've tried in detail or expect a definitive answer as to what to do. I just hope for some direction or advice as to how to diagnose the situation.

Here's the problem. When I run the 'legacy' mypy invocation, everything passes, but when I call the pre-commit run mypy --all-files, I get a lot of errors along the lines of:

pack_a/pack_b/pack_c/json_formatter.py:171:13: error: Returning
Any from function declared to return "str"  [no-any-return]
                return orjson.dumps(result, self._default).decode('UTF-8')

The function in question is indeed declared to return str, but so is orjson.dumps, so it seems that this should pass (it returns bytes to be precise, but not Any).

The other type of error I get is Unused "type: ignore" comment

I tried to figure out the difference between the configurations, but couldn't.

Here's how mypy is called now:

$(VENV)/bin/mypy --install-types --non-interactive $(CODE)

Where $(CODE) is the list of all the directories that contain 'code' (as opposed to tests and supporting scripts)

- repo: https://github.com/pre-commit/mirrors-mypy
  rev: v0.950
  hooks:
    - id: mypy
      args: [
         "--config-file=setup.cfg",
          --no-strict-optional,
          --ignore-missing-imports,
        ]
      additional_dependencies:
        - pydantic
        - returns
         ... (I checked all the types-* files the old mypy installs and added them here)
      exclude: (a regex to exclude the non-code dirs)

I am not sure how to check that the both configurations check the same set of files, but the number of files checked they report is the same.

There's also the problem that when I edit a file and then run pre-commit run mypy, I get even more errors than with pre-commit run mypy --all-files

Is there anything I could try to diagnose this?

like image 748
Ibolit Avatar asked Nov 03 '25 13:11

Ibolit


1 Answers

the mirrors-mypy README addresses this --

Because pre-commit runs mypy from an isolated virtualenv (without your dependencies) you may also find it useful to add the typed dependencies to additional_dependencies so mypy can better perform dynamic analysis

at least from your description you're missing orjson there -- perhaps more


disclaimer: I wrote pre-commit

like image 81
Anthony Sottile Avatar answered Nov 06 '25 04:11

Anthony Sottile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!