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

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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: 6 additions & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ def parse_config_file(
print(f"{file_read}: No [mypy] section in config file", file=stderr)
else:
section = parser["mypy"]

# if "files" in section:
# raw_files = section["files"]
# normalized_files = [file.strip() for file in raw_files.split(",") if file.strip()]
# section["files"] = ",".join(normalized_files)

prefix = f"{file_read}: [mypy]: "
updates, report_dirs = parse_section(
prefix, options, set_strict_flags, section, config_types, stderr
Expand Down
35 changes: 35 additions & 0 deletions mypy/test/testconfigparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import configparser

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


def test_parse_files_normalization():
options = Options()
config = """
[mypy]
files = file1.py, file2.py, file3.py, ,
"""

parser = configparser.ConfigParser()
parser.read_string(config)

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

# Assert that the trailing commas and empty strings are removed
assert options.files == ["file1.py", "file2.py", "file3.py"]


def test_parse_files_with_empty_strings():
options = Options()
config = """
[mypy]
files = ,file1.py,,file2.py
"""
parser = configparser.RawConfigParser()
parser.read_string(config)

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

# Assert that empty strings are ignored
assert options.files == ["file1.py", "file2.py"]
Loading