question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mypy should be able to access real dependencies

See original GitHub issue

I am not sure how to better achieve it. Here is the particular problem: my project uses attrs library, and in particular its new attr.s(auto_exc=True) flag. This flag appeared in the last version of attrs and didn’t yet make its way into attrs typeshed which is shipped with mypy (at mypy/typeshed/third_party/2and3/attr). Now, when I use pre-comit to run mypy, it works in its own virtualenv and does not have my dependencies installed. When it tries to find definitions for attrs, it finds that it cannot import such module but it finds that shipped typeshed and uses it. This results in an error being thrown at me regarding unknown auto_exc argument.

At least I would like to be able to at least prohibit mypy using these third-party typesheds. And at most it would be great to allow mypy used by pre-commit to access real package’s dependencies installed. Here are approaches I see:

  1. We could do something like pip install -e . before actually running mypy. But it will install stuff into pre-commit global mypy virtualenv which might be not good when working on multiple projects.
  2. We could make some exception to make pre-commit use my per-project virtualenv. Personally I use path/to/project/env directory, but it should be configurable somehow. And that configuration should not be committed to the repo because my colleagues might prefer different virtualenv locations.
  3. Looks like --python-executable path/to/virtual/env/bin/python argument to mypy helps, but how do we make actual path not being committed to the repo?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
douarddacommented, Nov 20, 2019

Hi, I am facing the same kind of problem. How can I use this mypy pre-commit hook on real-life use cases? I mean I definitively do not want to duplicate my requirements (with extras) in the additional_dependencies config entry, and the ‘language: system’ trick is not really acceptable to me since. as @asottile noticed, it makes the pre-commit hook result depends on the local (aka ‘system’) development venv (in which can live many more packages than what’s listed in my project’s requirements.)

In the end, and this is quite specific to mypy I guess, it makes no sense to run mypy on the local copy of the code in a dedicated venv if the dependencies of said code are not also installed in that venv, and it seems to me not really acceptable to workaround this by maintaining the additional_dependencies entry ‘by hand’.

I guess this is more a limitation of pre-commit itself, but it really reveals itself with mypy. Not sure what to do from there.

edit: I do understand there are also difficulties coming from the caching system of pre-commit.

2reactions
rmorsheacommented, Oct 30, 2019

Good to know. Looking into this more it seems like running mypy via a language: system command might resolve this issue, if not most use cases for a requirements.txt file. Will need to try that out though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running mypy and managing imports
Mypy will use an algorithm very similar to the one Python uses to find where modules and imports are located on the file...
Read more >
Running Mypy in Pre-commit - Jared Khan
Make sure Mypy has access to the installed dependencies of the code it is type-checking; Be careful with the use of flags that...
Read more >
pre-commit mypy with additional_dependencies from custom ...
This works ok if all these dependencies come from the public pypi index. What should I do if I have a package comes...
Read more >
mypy: how to use it in my project? - Breadcrumbs Collector
Hence, if you split your dependencies into dev and “production” ones, ... As a rule of thumb, you should be able to type-annotate...
Read more >
Mypy Documentation - Read the Docs
This function is now statically typed: mypy will use the provided type ... Mypy complains if it can't find a stub (or a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found