1

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?

Ibolit
  • 9,218
  • 7
  • 52
  • 96

1 Answers1

4

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thank you, Anthony, I will try that. PS: I know, I watch your YT channel – Ibolit Nov 02 '22 at 14:48
  • 1
    Is there a way to declare `additional_dependencies` from git? The project appears to depend on some first-party libs that are not published in pypi or a local artifactory but is used directly from git – Ibolit Nov 02 '22 at 15:45
  • Or from a local directory that is not a part of the project – Ibolit Nov 02 '22 at 15:51
  • there's nothing special, `additional_dependencies` is just passed to pip – anthony sottile Nov 02 '22 at 16:45