Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run mypy from "root" path #6

Open
Holt59 opened this issue Dec 10, 2019 · 1 comment
Open

Run mypy from "root" path #6

Holt59 opened this issue Dec 10, 2019 · 1 comment

Comments

@Holt59
Copy link

Holt59 commented Dec 10, 2019

I have the following directory structure (standard for a python package I think):

module/
    __init__.py
    ...
tests/
    test_xxx.py
setup.cfg
setup.py

I have installed the module doing pip install -e ..

Problem

When running mypy from the command line, there are no issues with test_xxx.py. When running flycheck-mypy from emacs, I got tons of warnings similar to:

test_settings.py:7: error: Cannot find implementation or library stub for module named 'module.settings'

The problem occurs because flycheck run mypy from the tests folder and not the root one. Even if I specify a mypy config file, the error persists.

Potential fix

There is :working-directory option for flycheck checkers that could be used. Mine look like this (only works for setup.cfg currently):

(defun flycheck-mypy--find-project-root (_checker)
  "Compute an appropriate working-directory for flycheck-ruby.
This is either a parent directory containing a Gemfile, or nil."
  (and
   buffer-file-name
   (locate-dominating-file buffer-file-name "setup.cfg")))

(flycheck-define-checker python-mypy
  "Mypy syntax checker. Requires mypy>=0.3.1.

Customize `flycheck-python-mypy-args` to add specific args to default
executable.

E.g. when processing Python2 files, add \"--py2\".

See URL `http://mypy-lang.org/'."

  :command ("mypy"
            (config-file "--config-file" flycheck-mypy.ini)
            (eval flycheck-python-mypy-args)
            source-original)
  :working-directory flycheck-mypy--find-project-root
  :error-patterns
  ((error line-start (file-name) ":" line ": error:" (message) line-end)
   (warning line-start (file-name) ":" line ": note:" (message) line-end)
   (info line-start (file-name) ":" line ": note:" (message) line-end))
  :modes python-mode)

(add-to-list 'flycheck-checkers 'python-mypy t)

It should be possible to adapt this to either have a variable to define the directory, or use the directory of flycheck-mypy.ini.

@lbolla
Copy link
Owner

lbolla commented Jan 8, 2020

@Holt59 I am happy to take a MR if you have time to work on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants