Skip to content

Test and fix trailing commas in many multiline string options in pyproject.toml #18624

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

Merged
merged 6 commits into from
Feb 7, 2025
Merged
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
6 changes: 4 additions & 2 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ def parse_version(v: str | float) -> tuple[int, int]:
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"""
if isinstance(v, str):
return [p.strip() for p in re.split(split_regex, v)]

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]


Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,15 @@ reveal_type(1) # N: Revealed type is "Literal[1]?"
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/custom_errorcode.py


[case testPyprojectPluginsTrailingComma]
# flags: --config-file tmp/pyproject.toml
[file pyproject.toml]
# This test checks that trailing commas in string-based `plugins` are allowed.
\[tool.mypy]
plugins = """
<ROOT>/test-data/unit/plugins/function_sig_hook.py,
<ROOT>/test-data/unit/plugins/method_in_decorator.py,
"""
[out]
93 changes: 93 additions & 0 deletions test-data/unit/cmdline.pyproject.test
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,96 @@ Neither is this!
description = "Factory ⸻ A code generator 🏭"
\[tool.mypy]
[file x.py]

[case testPyprojectFilesTrailingComma]
# cmd: mypy
[file pyproject.toml]
\[tool.mypy]
# We combine multiple tests in a single one here, because these tests are slow.
files = """
a.py,
b.py,
"""
always_true = """
FLAG_A1,
FLAG_B1,
"""
always_false = """
FLAG_A2,
FLAG_B2,
"""
[file a.py]
x: str = 'x' # ok'

# --always-true
FLAG_A1 = False
FLAG_B1 = False
if not FLAG_A1: # unreachable
x: int = 'x'
if not FLAG_B1: # unreachable
y: int = 'y'

# --always-false
FLAG_A2 = True
FLAG_B2 = True
if FLAG_A2: # unreachable
x: int = 'x'
if FLAG_B2: # unreachable
y: int = 'y'
[file b.py]
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file c.py]
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]

[case testPyprojectModulesTrailingComma]
# cmd: mypy
[file pyproject.toml]
\[tool.mypy]
# We combine multiple tests in a single one here, because these tests are slow.
modules = """
a,
b,
"""
disable_error_code = """
operator,
import,
"""
enable_error_code = """
redundant-expr,
ignore-without-code,
"""
[file a.py]
x: str = 'x' # ok

# --enable-error-code
a: int = 'a' # type: ignore

# --disable-error-code
'a' + 1
[file b.py]
y: int = 'y'
[file c.py]
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]
b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
a.py:4: error: "type: ignore" comment without error code (consider "type: ignore[assignment]" instead)

[case testPyprojectPackagesTrailingComma]
# cmd: mypy
[file pyproject.toml]
\[tool.mypy]
packages = """
a,
b,
"""
[file a/__init__.py]
x: str = 'x' # ok
[file b/__init__.py]
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file c/__init__.py]
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]
32 changes: 32 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,38 @@ always_true =
MY_VAR,
[out]

[case testCmdlineCfgModulesTrailingComma]
# cmd: mypy
[file mypy.ini]
\[mypy]
modules =
a,
b,
[file a.py]
x: str = 'x' # ok
[file b.py]
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file c.py]
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]

[case testCmdlineCfgPackagesTrailingComma]
# cmd: mypy
[file mypy.ini]
\[mypy]
packages =
a,
b,
[file a/__init__.py]
x: str = 'x' # ok
[file b/__init__.py]
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file c/__init__.py]
# This should not trigger any errors, because it is not included:
z: int = 'z'
[out]

[case testTypeVarTupleUnpackEnabled]
# cmd: mypy --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack a.py
[file a.py]
Expand Down