pytest-mypy-plugins icon indicating copy to clipboard operation
pytest-mypy-plugins copied to clipboard

Add Mypy configuration through root "pyproject.toml" file

Open Delgan opened this issue 2 years ago • 2 comments

Hi!

This is a proposal to fix #133.

This offers a way to use Mypy configuration specific to tests without needing the --mypy-pyproject-toml-file command line argument. Instead, users can define a [tool.pytest-mypy-plugins.mypy-config] table in their repository's pyproject.toml file.

This approach addresses two issues:

  • Avoids the need for an extra file in the repository to store Mypy test configuration.
  • Ensures that pytest can be invoked from any location (this is not possible when --mypy-pyproject-toml-file is specified via the addopts configuration, as paths are resolved relative to the current working directory).

The order of precedence for Mypy configuration is as follows, with each subsequent option taking precedence over the previous ones if it exists:

  1. Use configuration from [tool.pytest-mypy-plugins.mypy-config] in the root pyproject.toml as default.
  2. Override default with options from --mypy-pyproject-toml-file or --mypy-ini-file if provided.
  3. Override again with config_mypy section in a test if present.

I hope this process isn't too convoluted. Please let me know if you prefer a different approach. I made slight modifications to the join_toml_configs() function to implement the rules mentioned above. Additionally, I can add more unit tests if necessary.

Delgan avatar Nov 04 '23 22:11 Delgan

Thanks a lot!

sobolevn avatar Nov 05 '23 07:11 sobolevn

@sobolevn I updated the PR according to your comments. :+1:

Two remarks waiting for your opinion:

  • maybe I should rename the mypy-config table to config-mypy for consistency with the existing inline field.
  • I could also add tests for join_ini_configs() but I don't know what are your preferences regarding the assert_file_contents() utils function (should it be duplicated, or what is the common file where I should move it).

Delgan avatar Nov 05 '23 21:11 Delgan