typeddjango/pytest-mypy-plugins

Add plugin configuration through pyproject.toml?

Delgan opened this issue · 5 comments

Delgan commented

Hi!

I need a dedicated mypy.ini configuration for tests run by pytest-mypy-plugins. This configuration includes parameters like force_uppercase_builtins = true, which are very useful for unit tests but irrelevant for the entire project.

Currently, I can create a separate mypy-tests.ini file and run pytest with the --mypy-ini-file option. To avoid typing this every time, I can add addopts = "--mypy-ini-file=mypy-tests.ini" to my pytest.ini configuration.

However, there is a drawback to this approach: the path is resolved relative to the current working directory, not to pytest.ini. This can lead to surprising behavior, where test output changes depending on where the pytest command was executed. For example, running tests from outside the project's root folder may cause Mypy tests to fail (because it won't use the required mypy-tests.ini file) although all others ran seamlessly.

I'd like my tests to be robust in such cases. Pytest itself works flawlessly, so I believe plugins should too. I assume resolving the --mypy-ini-file path relative to the "rootdir" is not acceptable for a command-line option. Therefore, it seems appropriate to allow configuration via a file and, in this case, resolve the paths relative to that file. We could even imagine a "override mypy config" entry to avoid the additional mypy-tests.ini file in my scenario.

What are your thoughts on this?

I've noticed that pytest-mypy-plugins already depends on tomlkit, so I assume implementing this shouldn't be overly complicated.
However, I understand if you prefer not to add complexity to the plugin configuration in this way. Yet, If you're open to the idea, I'd be happy to contribute with a PR.

Does --mypy-pyproject-toml-file not work for you?

Delgan commented

The problem would remain the same. Sorry if my description of the issue is not clear.
It's not about using mypy.ini or pyproject.toml, that's not really relevant here.

To put it more simply, I would like to configure pytest-mypy-plugins itself through pyproject.toml instead of using command-line options.

This would prevent tests from passing or failing depending on the current working directory, due to the way relative paths are resolved for --mypy-pyproject-toml-file and --mypy-ini-file.

Oh, I see :)
PR is welcome!

Delgan commented

Great, thanks! I'll work on it.

Tinche commented

Nice! I'm in the process of adding this plugin to the cattrs test suite and I think I want this.

To explain my use case: I want to run Mypy only on a single version of Python, say the latest. It's good enough and keeps complexity down. But I need to use --mypy-only-local-stub.

So my plan was to install the plugin only on 3.12 and put --mypy-only-local-stub into pyproject.toml/[tool.pytest.ini_options].addopts. But if I do that, on other Pythons pytest doesn't know what that argument is and won't run.

I'm sure there are other ways of solving this but this seemed like the simplest/most correct to me.