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

Automatically use mypy.ini or pyproject.toml when not in the root directory of the workspace #190

Open
theomessin opened this issue Jul 23, 2019 · 14 comments
Labels
feature-request Request for new features or functionality needs PR

Comments

@theomessin
Copy link

theomessin commented Jul 23, 2019

When using the Mypy linter, our standard is to have a mypy.ini in the project's root with a bunch of config values. Currently we have our developers use this setting for vscode:

  "python.linting.mypyArgs": [
    "--config-file=${workspaceFolder}/mypy.ini"
  ]

However, if a project doesn't have a mypy.ini file linting fails silently (which can cause a lot of issues as a dev might think that mypy worked fine).

I think that auto-detecting and using the mypy.ini file should be a part of the plugin itself.

@theomessin theomessin added triage-needed feature-request Request for new features or functionality labels Jul 23, 2019
@brettcannon
Copy link
Member

https://mypy.readthedocs.io/en/latest/config_file.html#the-mypy-configuration-file suggests the configuration file is detected automatically by mypy and so the extension shouldn't have to do anything special. Or am I misunderstanding what you're asking for?

@theomessin
Copy link
Author

However, mypy will auto-detect mypy.ini "in the current directory". The extension calls mypy on each file, whereas you'd normally just call it for all files from the project root, right?

@brettcannon
Copy link
Member

@theomessin ah, gotcha. And since mypy doesn't walk up the directory hierarchy looking for a config it won't find it if you're linting a file in a subdirectory.

Please do consider upvoting microsoft/vscode-python#1577 as that's probably going to be the easiest way to see this resolved.

@DonJayamanne DonJayamanne added bug Issue identified by VS Code Team member as probable bug and removed bug Issue identified by VS Code Team member as probable bug labels Jul 25, 2019
@KholdStare
Copy link

Any updates?

@ssbarnea
Copy link

ssbarnea commented Jan 13, 2023

@brettcannon With the PEP-621 migration, mypy is also able to read pyproject.toml and my impression is that it it does load it.

Maybe we should rename this ticket into "mypy-plugin support should load config from same default locations as mypy itself, without requiring any configuration"

@KholdStare
Copy link

@ssbarnea We have a usecase where we don't store the mypy settings in pyproject.toml, but in .mypy.ini in the project directory. We have a monorepo with several python projects and are using vscode's multiroot workspace. All these python projects share the same mypy settings, and the only way we could reduce duplication (no copy pasting in all the pyproject.toml files) is to symlink a "master" .mypy.ini into all the subprojects.

Currently we have to do this to make mypy work:

  "python.linting.mypyArgs": [
    "--config-file=${workspaceFolder}/.mypy.ini"
  ]

@brettcannon brettcannon changed the title Automatically use mypy.ini Automatically use mypy.ini or pyproject.toml Jan 16, 2023
@brettcannon brettcannon changed the title Automatically use mypy.ini or pyproject.toml Automatically use mypy.ini or pyproject.toml when not in the root directory of the workspace Jan 16, 2023
@brettcannon
Copy link
Member

Maybe we should rename this ticket

I renamed it to try to make it clearer while keeping some keywords in it to facilitate searching.

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Oct 31, 2023
@karthiknadig
Copy link
Member

There is a cwd setting that can be used to set the this. By default, it is the workspace directory, if you want it to run in the same directory as the file you can set it to ${fileDirname}, and cwd will change to the parent directory of the file.

@evbo
Copy link

evbo commented Feb 22, 2024

@karthiknadig can we please reopen? ${fileDirname} is not working for very many cases, such as when you have multiple sub folders.

What does seem to work without any hiccups is the following:

    "mypy-type-checker.args": [
        "--config-file=${workspaceFolder}/pyproject.toml"
    ]

However, it would be nice not to have to specify this since it is quite boilerplate configuration

@karthiknadig
Copy link
Member

@evbo Can you elaborate on your scenario? like what is the directory structure and where are your config files.

From your what is see above it looks like you have a single pyproject.toml in your workspace folder. that means you don't need to specify mypy-type-checker.args. The cwd by default is your workspace folder, and mypy should pickup pyproject.toml.

The ${fileDirname} is useful if you have different configurations for different parts of your project. From what I understand that is not your case.

@evbo
Copy link

evbo commented Feb 22, 2024

It is a mono repo, like:

project_root
    python_module_a
        pyproject.toml
        venv
        folder
            source.py
        source.py
    python_module_b

@karthiknadig
Copy link
Member

Do you have a settings file you can share? and also do you have or tried using *.code-workspace?

@evbo
Copy link

evbo commented Feb 23, 2024

settings.json

Thanks, I have not used the multi-workspace configuration but am not really interested in using it for two reasons:

  1. The major benefits don't apply in my case since I have a root folder all of these sub projects belong under
  2. The nature of this issue is to "automatically" run tooling, and so I'm trying to tamp down on additional config as much as possible.

@karthiknadig
Copy link
Member

@evbo This really is a bug on mypy to add the ability to find config in parent directories (similar to pylint, ruff, flake8, etc). I can see that this might be useful until such support is added to mypy.

I recommend creating an issue, and we are open to a PR on this.
Things to consider:

  1. check the args and if it does not already have a config, and the cwd does not have a config initiate the search.
  2. Ensure that this search happens every time for mypy case, but produces and warning for dmypy. dmypy does not re-read configuration.

The changes should happen in here:

def _linting_helper(document: workspace.Document) -> None:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality needs PR
Projects
None yet
Development

No branches or pull requests

8 participants