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

Allow trailing commas for files setting in mypy.ini and setup.ini #18621

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
2 changes: 1 addition & 1 deletion mypy/config_parser.py
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ def split_and_match_files(paths: str) -> list[str]:
Returns a list of file paths
"""

return split_and_match_files_list(paths.split(","))
return split_and_match_files_list(split_commas(paths))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do the same for pyproject.toml? (Line 202 uses try_split and is equally prone to trailing comma)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this test:

[case testCmdlineCfgFilesTrailingComma]
# cmd: mypy
[file pyproject.toml]
\[tool.mypy]
files = [
  "a.py",
  "b.py",
]
[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]

and it passed on main. So, I don't think that this change is required.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, not exactly. This is perverse style, but...


[case testCmdlineCfgFilesTrailingComma]
# cmd: mypy
[file pyproject.toml]
\[tool.mypy]
files = """
  a.py,
  b.py,
"""
[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]

(remove the trailing comma to make it pass)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When lists are toml lists, commas are handled much earlier by the parser itself. But mypy seems to support ini-style comma-delimited lists as single strings as well. I don't know how useful that is, but let's keep commas treatment consistent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, got it!

Copy link
Member Author

@sobolevn sobolevn Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, here's the thing. pyproject.toml uses try_split function which is regex based.
Looks like there are multiple settings that are affected.

Let's do it this way: since I cannot really change just one handler, I will merge this PR and create the next one with many tests based on try_split.



def check_follow_imports(choice: str) -> str:
16 changes: 16 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
@@ -1300,6 +1300,22 @@ foo.py:1: error: "int" not callable
[out]
foo/m.py:1: error: "int" not callable

[case testCmdlineCfgFilesTrailingComma]
# cmd: mypy
[file mypy.ini]
\[mypy]
files =
a.py,
b.py,
[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 testCmdlineCfgEnableErrorCodeTrailingComma]
# cmd: mypy .
[file mypy.ini]