Adds support for pyproject.toml files (#639)

* Adds support for pyproject.toml files

Since mypy 0.900 the pyproject.toml files are supported.

This PR adds a support for it. It searchs for a `tool.django-stubs` section. This is an example configuration:

```
[tool.django-stubs]
django_settings_module = "config.settings.local"
```

Fixes #638

* Added TOML tests

* Use textwrap.dedent instead of trying to manually replace spaces
This commit is contained in:
Cesar Canassa
2021-06-15 00:50:31 +02:00
committed by GitHub
parent 8c387e85fe
commit 397e3f3dac
6 changed files with 135 additions and 19 deletions

View File

@@ -1,8 +1,10 @@
import configparser
import sys
import textwrap
from functools import partial
from typing import Callable, Dict, List, NoReturn, Optional, Tuple, cast
import toml
from django.db.models.fields.related import RelatedField
from mypy.modulefinder import mypy_path
from mypy.nodes import MypyFile, TypeInfo
@@ -63,15 +65,14 @@ def extract_django_settings_module(config_file_path: Optional[str]) -> str:
"""
from mypy.main import CapturableArgumentParser
usage = """(config)
usage = """
(config)
...
[mypy.plugins.django_stubs]
django_settings_module: str (required)
...
""".replace(
"\n" + 8 * " ", "\n"
)
handler = CapturableArgumentParser(prog="(django-stubs) mypy", usage=usage)
"""
handler = CapturableArgumentParser(prog="(django-stubs) mypy", usage=textwrap.dedent(usage))
messages = {
1: "mypy config file is not specified or found",
2: "no section [mypy.plugins.django-stubs]",
@@ -79,18 +80,53 @@ def extract_django_settings_module(config_file_path: Optional[str]) -> str:
}
handler.error("'django_settings_module' is not set: " + messages[error_type])
parser = configparser.ConfigParser()
try:
with open(cast(str, config_file_path)) as handle:
parser.read_file(handle, source=config_file_path)
except (IsADirectoryError, OSError):
exit(1)
def exit_toml(error_type: int) -> NoReturn:
from mypy.main import CapturableArgumentParser
section = "mypy.plugins.django-stubs"
if not parser.has_section(section):
exit(2)
settings = parser.get(section, "django_settings_module", fallback=None) or exit(3)
return cast(str, settings).strip("'\"")
usage = """
(config)
...
[tool.django-stubs]
django_settings_module = str (required)
...
"""
handler = CapturableArgumentParser(prog="(django-stubs) mypy", usage=textwrap.dedent(usage))
messages = {
1: "mypy config file is not specified or found",
2: "no section [tool.django-stubs]",
3: "the setting is not provided",
4: "the setting must be a string",
}
handler.error("'django_settings_module' not found or invalid: " + messages[error_type])
if config_file_path and helpers.is_toml(config_file_path):
toml_data = toml.load(config_file_path)
try:
config = toml_data["tool"]["django-stubs"]
except KeyError:
exit_toml(2)
if "django_settings_module" not in config:
exit_toml(3)
if not isinstance(config["django_settings_module"], str):
exit_toml(4)
return config["django_settings_module"]
else:
parser = configparser.ConfigParser()
try:
with open(cast(str, config_file_path)) as handle:
parser.read_file(handle, source=config_file_path)
except (IsADirectoryError, OSError):
exit(1)
section = "mypy.plugins.django-stubs"
if not parser.has_section(section):
exit(2)
settings = parser.get(section, "django_settings_module", fallback=None) or exit(3)
return cast(str, settings).strip("'\"")
class NewSemanalDjangoPlugin(Plugin):