8000 Better handling of missing sections · python/mypy@ecf7b33 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecf7b33

Browse files
committed
Better handling of missing sections
1 parent 5410ecb commit ecf7b33

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

mypy/config_parser.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,32 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
138138
if filename is not None:
139139
filename = os.path.expanduser(filename)
140140
if os.path.splitext(filename)[1] == '.toml':
141-
parse_toml_config_file(options, set_strict_flags, filename, stdout, stderr)
141+
parse_toml_config_file(
142+
options, set_strict_flags, filename, stdout, stderr, explicit=True)
142143
else:
143-
parse_ini_config_file(options, set_strict_flags, filename, stdout, stderr)
144+
parse_ini_config_file(
145+
options, set_strict_flags, filename, stdout, stderr, explicit=True)
144146
else:
145147
for filename in defaults.CONFIG_FILES:
146148
filename = os.path.expanduser(filename)
147149
if not os.path.isfile(filename):
148150
continue
149151
if os.path.splitext(filename)[1] == '.toml':
150-
parsed = parse_toml_config_file(options, set_strict_flags,
151-
filename, stdout, stderr)
152+
parsed = parse_toml_config_file(
153+
options, set_strict_flags, filename, stdout, stderr, explicit=False)
152154
else:
153-
parsed = parse_ini_config_file(options, set_strict_flags,
154-
filename, stdout, stderr)
155+
parsed = parse_ini_config_file(
156+
options, set_strict_flags, filename, stdout, stderr, explicit=False)
155157
if parsed:
156158
break
157159

158160

159161
def parse_toml_config_file(options: Options, set_strict_flags: Callable[[], None],
160162
filename: str,
161163
stdout: Optional[TextIO] = None,
162-
stderr: Optional[TextIO] = None) -> bool:
164+
stderr: Optional[TextIO] = None,
165+
*,
166+
explicit: bool) -> bool:
163167
stderr = stderr or sys.stderr
164168

165169
# Load the toml config file.
@@ -172,7 +176,8 @@ def parse_toml_config_file(options: Options, set_strict_flags: Callable[[], None
172176
options.config_file = filename
173177

174178
if 'tool' not in table or 'mypy' not in table['tool']:
175-
print("%s: No 'tool.mypy' table in config file" % filename, file=stderr)
179+
if explicit:
180+
print("%s: No 'tool.mypy' table in config file" % filename, file=stderr)
176181
return False
177182

178183
# Handle the mypy table.
@@ -228,7 +233,9 @@ def parse_toml_config_file(options: Options, set_strict_flags: Callable[[], None
228233
def parse_ini_config_file(options: Options, set_strict_flags: Callable[[], None],
229234
filename: str,
230235
stdout: Optional[TextIO] = None,
231-
stderr: Optional[TextIO] = None) -> bool:
236+
stderr: Optional[TextIO] = None,
237+
*,
238+
explicit: bool) -> bool:
232239
stderr = stderr or sys.stderr
233240
parser = configparser.RawConfigParser()
234241
retv = False
@@ -245,7 +252,7 @@ def parse_ini_config_file(options: Options, set_strict_flags: Callable[[], None]
245252
os.path.abspath(filename))
246253

247254
if 'mypy' not in parser:
248-
if filename not in defaults.SHARED_CONFIG_FILES:
255+
if not explicit and filename not in defaults.SHARED_CONFIG_FILES:
249256
print("%s: No [mypy] section in config file" % filename, file=stderr)
250257
else:
251258
retv = True

test-data/unit/cmdline.test

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,34 @@ warn_unused_ignores = true
180180
[out]
181181
main.py:1: error: unused 'type: ignore' comment
182182

183-
[case testMissingMypySection]
183+
[case testMissingTomlMypySection1]
184184
# cmd: mypy main.py --config-file=pyproject.toml
185185
[file pyproject.toml]
186186
[file main.py]
187-
# type: ignore
188187
[out]
189188
pyproject.toml: No 'tool.mypy' table in config file
190189
== Return code: 0
191190

191+
[case testMissingTomlMypySection2]
192+
# cmd: mypy main.py
193+
# We shouldn't complain if it wasn't specified as the config file
194+
[file pyproject.toml]
195+
[file main.py]
196+
[out]
197+
198+
[case testMissingTomlMypySection3]
199+
# cmd: mypy main.py
200+
# We shouldn't complain if it wasn't specified as the config file
201+
# And we should pick up the setup.cfg
202+
[file pyproject.toml]
203+
[file setup.cfg]
204+
\[mypy]
205+
warn_unused_ignores = true
206+
[file main.py]
207+
# type: ignore
208+
[out]
209+
main.py:1: error: unused 'type: ignore' comment
210+
192211
[case testPerFileConfigSection]
193212
# cmd: mypy x.py y.py z.py
194213
[file pyproject.toml]

0 commit comments

Comments
 (0)
0