Skip to content

Fix #19491, crash when using enable_error_code value of wrong type in pyproject.toml #19494

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

from collections.abc import Mapping, MutableMapping, Sequence
from typing import Any, Callable, Final, TextIO, Union
from typing_extensions import TypeAlias as _TypeAlias
from typing_extensions import Never, TypeAlias

from mypy import defaults
from mypy.options import PER_MODULE_OPTIONS, Options

_CONFIG_VALUE_TYPES: _TypeAlias = Union[
_CONFIG_VALUE_TYPES: TypeAlias = Union[
str, bool, int, float, dict[str, str], list[str], tuple[int, int]
]
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
_INI_PARSER_CALLABLE: TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]


class VersionTypeError(argparse.ArgumentTypeError):
Expand Down Expand Up @@ -60,14 +60,31 @@ def parse_version(v: str | float) -> tuple[int, int]:
return major, minor


def try_split(v: str | Sequence[str], split_regex: str = "[,]") -> list[str]:
"""Split and trim a str or list of str into a list of str"""
def try_split(v: str | Sequence[str] | object, split_regex: str = ",") -> list[str]:
"""Split and trim a str or sequence (eg: list) of str into a list of str.
If an element of the input is not str, a type error will be raised."""

def complain(x: object, additional_info: str = "") -> Never:
raise argparse.ArgumentTypeError(
f"Expected a list or a stringified version thereof, but got: '{x}', of type {type(x).__name__}.{additional_info}"
)

if isinstance(v, str):
items = [p.strip() for p in re.split(split_regex, v)]
if items and items[-1] == "":
items.pop(-1)
return items
return [p.strip() for p in v]
elif isinstance(v, Sequence):
return [
(
p.strip()
if isinstance(p, str)
else complain(p, additional_info=" (As an element of the list.)")
)
for p in v
]
else:
complain(v)


def validate_codes(codes: list[str]) -> list[str]:
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/cmdline.pyproject.test
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,14 @@ y: int = 'y' # E: Incompatible types in assignment (expression has type "str",
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]

[case testPyprojectTOMLSettingOfWrongType]
# cmd: mypy a.py
[file pyproject.toml]
\[tool.mypy]
enable_error_code = true
[file a.py]
x: int = 1
[out]
pyproject.toml: [mypy]: enable_error_code: Expected a list or a stringified version thereof, but got: 'True', of type bool.
== Return code: 0
Loading