Skip to content
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

[conf] Allow trailing commas in ini configuration of multiline values #14240

Merged
merged 1 commit into from
Dec 6, 2022
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
23 changes: 16 additions & 7 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def check_follow_imports(choice: str) -> str:
return choice


def split_commas(value: str) -> list[str]:
# Uses a bit smarter technique to allow last trailing comma
# and to remove last `""` item from the split.
items = value.split(",")
if items and items[-1] == "":
items.pop(-1)
return items


# For most options, the type of the default value set in options.py is
# sufficient, and we don't have to do anything here. This table
# exists to specify types for values initialized to None or container
Expand All @@ -151,13 +160,13 @@ def check_follow_imports(choice: str) -> str:
"junit_xml": expand_path,
"follow_imports": check_follow_imports,
"no_site_packages": bool,
"plugins": lambda s: [p.strip() for p in s.split(",")],
"always_true": lambda s: [p.strip() for p in s.split(",")],
"always_false": lambda s: [p.strip() for p in s.split(",")],
"enable_incomplete_feature": lambda s: [p.strip() for p in s.split(",")],
"disable_error_code": lambda s: validate_codes([p.strip() for p in s.split(",")]),
"enable_error_code": lambda s: validate_codes([p.strip() for p in s.split(",")]),
"package_root": lambda s: [p.strip() for p in s.split(",")],
"plugins": lambda s: [p.strip() for p in split_commas(s)],
"always_true": lambda s: [p.strip() for p in split_commas(s)],
"always_false": lambda s: [p.strip() for p in split_commas(s)],
"enable_incomplete_feature": lambda s: [p.strip() for p in split_commas(s)],
"disable_error_code": lambda s: validate_codes([p.strip() for p in split_commas(s)]),
"enable_error_code": lambda s: validate_codes([p.strip() for p in split_commas(s)]),
"package_root": lambda s: [p.strip() for p in split_commas(s)],
"cache_dir": expand_path,
"python_executable": expand_path,
"strict": bool,
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ reveal_type(f()) # N: Revealed type is "builtins.int"
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/customentry.py:register

[case testCustomPluginEntryPointFileTrailingComma]
# flags: --config-file tmp/mypy.ini
def f() -> str: ...
reveal_type(f()) # N: Revealed type is "builtins.int"
[file mypy.ini]
\[mypy]
plugins =
<ROOT>/test-data/unit/plugins/customentry.py:register,

[case testCustomPluginEntryPoint]
# flags: --config-file tmp/mypy.ini
def f() -> str: ...
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,29 @@ foo.py:1: error: "int" not callable
1()
[out]
foo/m.py:1: error: "int" not callable

[case testCmdlineCfgEnableErrorCodeTrailingComma]
# cmd: mypy .
[file mypy.ini]
\[mypy]
enable_error_code =
truthy-bool,
redundant-expr,
[out]

[case testCmdlineCfgDisableErrorCodeTrailingComma]
# cmd: mypy .
[file mypy.ini]
\[mypy]
disable_error_code =
misc,
override,
[out]

[case testCmdlineCfgAlwaysTrueTrailingComma]
# cmd: mypy .
[file mypy.ini]
\[mypy]
always_true =
MY_VAR,
[out]