Skip to content

Fix: Handle Trailing Commas and Empty Strings in File Paths #3

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 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 24, 2025
commit e80c2fae7b8d55aac3bf68e552d02230be05e14a
5 changes: 3 additions & 2 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ def parse_config_file(

# Raise an error if there are any remaining empty strings
if "" in files_split:
raise ValueError("Invalid config: Empty filenames are not allowed except for trailing commas.")
raise ValueError(
"Invalid config: Empty filenames are not allowed except for trailing commas."
)
Comment on lines +335 to +337

Choose a reason for hiding this comment

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

medium

The error message could be more informative. Consider including details about why empty filenames are invalid, or suggesting how to correct the configuration (e.g., removing the extra commas).

raise ValueError(
    "Invalid config: Empty filenames are not allowed. Please ensure all file entries are valid."
)


options.files = files_split


prefix = f"{file_read}: [mypy]: "
updates, report_dirs = parse_section(
prefix, options, set_strict_flags, section, config_types, stderr
Expand Down
75 changes: 15 additions & 60 deletions mypy/test/testconfigparser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import tempfile
from unittest import TestCase, main
from mypy.options import Options

from mypy.config_parser import parse_config_file
from mypy.options import Options


class TestConfigParser(TestCase):
def test_parse_config_file_with_single_file(self) -> None:
Expand All @@ -20,13 +22,7 @@ def test_parse_config_file_with_single_file(self) -> None:

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, ["file1.py"])

Expand All @@ -45,13 +41,7 @@ def test_parse_config_file_with_no_spaces(self) -> None:

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, ["file1.py", "file2.py", "file3.py"])

Expand All @@ -64,19 +54,13 @@ def test_parse_config_file_with_extra_spaces(self) -> None:
f.write(
"""
[mypy]
files = file1.py , file2.py , file3.py
files = file1.py , file2.py , file3.py
"""
)

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, ["file1.py", "file2.py", "file3.py"])

Expand All @@ -89,19 +73,13 @@ def test_parse_config_file_with_empty_files_key(self) -> None:
f.write(
"""
[mypy]
files =
files =
"""
)

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, [])

Expand All @@ -121,13 +99,7 @@ def test_parse_config_file_with_only_comma(self) -> None:
options = Options()

with self.assertRaises(ValueError) as cm:
parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertIn("Invalid config", str(cm.exception))

Expand All @@ -140,19 +112,13 @@ def test_parse_config_file_with_only_whitespace(self) -> None:
f.write(
"""
[mypy]
files =
files =
"""
)

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, [])

Expand All @@ -172,13 +138,7 @@ def test_parse_config_file_with_mixed_valid_and_invalid_entries(self) -> None:
options = Options()

with self.assertRaises(ValueError) as cm:
parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertIn("Invalid config", str(cm.exception))

Expand All @@ -199,15 +159,10 @@ def test_parse_config_file_with_newlines_between_files(self) -> None:

options = Options()

parse_config_file(
options,
lambda: None,
config_path,
stdout=None,
stderr=None,
)
parse_config_file(options, lambda: None, config_path, stdout=None, stderr=None)

self.assertEqual(options.files, ["file1.py", "file2.py", "file3.py"])


if __name__ == "__main__":
main()