Skip to content

Commit

Permalink
Toml support for pydantic-mypy plugin config (pydantic#2908)
Browse files Browse the repository at this point in the history
* add toml reader

* fix path

* skip configparser step

* fix quotes

* full pyproject.toml check

* add doc note

* cleaner formatting, raise ValueError for non-bool

* fix tests

* add bad config test case

* add changelog file.

* bump mypy to 0.902

* tweak change MD, fix formatting in requirements

* import check around toml

* switch to tomli for parsing to match mypy dependency

* import check around toml/tomli

* add note on tomli usage

* more succinct changelog entry

* fix quotes in changelog

* linting fixes, remove unnecessary stub install

* mypy checks on mypy plugin file

* wrongly placed pragma no cover

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
  • Loading branch information
2 people authored and jpribyl committed Dec 4, 2021
1 parent e3d616f commit 77e98d4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 1 addition & 4 deletions changes/2908-jrwalk.md
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Adds configuration logic to the `PydanticPluginConfig` constructor, enabling parsing of the configuration slots from `pyproject.toml` for `mypy>0.900`.
Adds test cases for parsing config from `pyproject.toml`, including a failing case for a non-boolean value (this matches the old `ConfigParser` behavior of raising a `ValueError` on failures in the `getboolean` method).
Increases the pinned version to `mypy==0.902` in the test suite, and includes formatting changes to prior test cases to match.
Adds documentation change reflecting `pyproject.toml` configuration for plugin.
Make `pydantic-mypy` plugin compatible with `pyproject.toml` configuration, consistent with `mypy` changes. See the [doc](https://pydantic-docs.helpmanual.io/mypy_plugin/#configuring-the-plugin) for more information.
14 changes: 12 additions & 2 deletions pydantic/mypy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from configparser import ConfigParser
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type as TypingType, Union

import toml
try:
import toml
except ImportError: # pragma: no cover
# future-proofing for upcoming `mypy` releases which will switch dependencies
try:
import tomli as toml # type: ignore
except ImportError:
import warnings

warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
toml = None # type: ignore
from mypy.errorcodes import ErrorCode
from mypy.nodes import (
ARG_NAMED,
Expand Down Expand Up @@ -112,7 +122,7 @@ def __init__(self, options: Options) -> None:
if options.config_file is None: # pragma: no cover
return

if options.config_file.endswith('.toml'):
if toml and options.config_file.endswith('.toml'):
with open(options.config_file, 'r') as rf:
config = toml.load(rf).get('tool', {}).get('pydantic-mypy', {})
for key in self.__slots__:
Expand Down
2 changes: 1 addition & 1 deletion tests/mypy/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
('pyproject-plugin.toml', 'plugin_success.py', None),
('pyproject-plugin.toml', 'plugin_fail.py', 'plugin-fail.txt'),
('pyproject-plugin-strict.toml', 'plugin_success.py', 'plugin-success-strict.txt'),
('pyproject-plugin-strict.toml', 'plugin_fail.py', 'plugin-fail-strict.txt')
('pyproject-plugin-strict.toml', 'plugin_fail.py', 'plugin-fail-strict.txt'),
]
executable_modules = list({fname[:-3] for _, fname, out_fname in cases if out_fname is None})

Expand Down
1 change: 1 addition & 0 deletions tests/requirements-linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ flake8-quotes==3.3.0
hypothesis==6.17.4
isort==5.9.3
mypy==0.910
types-toml==0.1.5
pycodestyle==2.7.0
pyflakes==2.3.1
twine==3.4.2

0 comments on commit 77e98d4

Please sign in to comment.