@@ -245,12 +245,10 @@ def _parse_individual_file(
245245 if is_toml (config_file ):
246246 with open (config_file , "rb" ) as f :
247247 toml_data = tomllib .load (f )
248- # Filter down to just mypy relevant toml keys
249- toml_data = toml_data .get ("tool" , {})
250- if "mypy" not in toml_data :
248+ toml_data = get_mypy_toml_data (config_file , toml_data )
249+ if toml_data is None :
251250 return None
252- toml_data = {"mypy" : toml_data ["mypy" ]}
253- parser = destructure_overrides (toml_data )
251+ parser = destructure_overrides (toml_data , config_file )
254252 config_types = toml_config_types
255253 else :
256254 parser = configparser .RawConfigParser ()
@@ -397,20 +395,48 @@ def is_toml(filename: str) -> bool:
397395 return filename .lower ().endswith (".toml" )
398396
399397
400- def destructure_overrides (toml_data : dict [str , Any ]) -> dict [str , Any ]:
401- """Take the new [[tool.mypy.overrides]] section array in the pyproject.toml file,
402- and convert it back to a flatter structure that the existing config_parser can handle.
398+ def is_pyproject (filename : str ) -> bool :
399+ return os .path .basename (filename ) == "pyproject.toml"
403400
404- E.g. the following pyproject.toml file:
405401
406- [[tool.mypy.overrides]]
402+ def get_mypy_toml_data (
403+ config_file : str ,
404+ toml_data : dict [str , Any ],
405+ ) -> dict [str , Any ] | None :
406+ if is_pyproject (config_file ):
407+ toml_data = toml_data .get ("tool" , {})
408+ if "mypy" not in toml_data :
409+ return None
410+ return {"mypy" : toml_data ["mypy" ]}
411+
412+ if "mypy" in toml_data :
413+ return toml_data
414+
415+ return {"mypy" : toml_data }
416+
417+
418+ def _toml_module_error (config_file : str , message : str ) -> str :
419+ if is_pyproject (config_file ):
420+ return message .format (prefix = "tool.mypy" , override = "[[tool.mypy.overrides]]" )
421+ return message .format (prefix = "mypy" , override = "[[mypy.overrides]]" )
422+
423+
424+ def destructure_overrides (toml_data : dict [str , Any ], config_file : str ) -> dict [str , Any ]:
425+ """Convert TOML overrides sections into the flatter ini-style structure.
426+
427+ ``pyproject.toml`` uses ``[[tool.mypy.overrides]]``.
428+ ``mypy.toml`` and ``.mypy.toml`` use ``[[mypy.overrides]]``.
429+
430+ E.g. the following TOML file:
431+
432+ [[mypy.overrides]]
407433 module = [
408434 "a.b",
409435 "b.*"
410436 ]
411437 disallow_untyped_defs = true
412438
413- [[tool. mypy.overrides]]
439+ [[mypy.overrides]]
414440 module = 'c'
415441 disallow_untyped_defs = false
416442
@@ -434,16 +460,22 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
434460
435461 if not isinstance (toml_data ["mypy" ]["overrides" ], list ):
436462 raise ConfigTOMLValueError (
437- "tool.mypy.overrides sections must be an array. Please make "
438- "sure you are using double brackets like so: [[tool.mypy.overrides]]"
463+ _toml_module_error (
464+ config_file ,
465+ "{prefix}.overrides sections must be an array. Please make "
466+ "sure you are using double brackets like so: {override}" ,
467+ ),
439468 )
440469
441470 result = toml_data .copy ()
442471 for override in result ["mypy" ]["overrides" ]:
443472 if "module" not in override :
444473 raise ConfigTOMLValueError (
445- "toml config file contains a [[tool.mypy.overrides]] "
446- "section, but no module to override was specified."
474+ _toml_module_error (
475+ config_file ,
476+ "toml config file contains a {override} section, but no module to "
477+ "override was specified." ,
478+ ),
447479 )
448480
449481 if isinstance (override ["module" ], str ):
@@ -452,9 +484,11 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
452484 modules = override ["module" ]
453485 else :
454486 raise ConfigTOMLValueError (
455- "toml config file contains a [[tool.mypy.overrides]] "
456- "section with a module value that is not a string or a list of "
457- "strings"
487+ _toml_module_error (
488+ config_file ,
489+ "toml config file contains a {override} section with a module value "
490+ "that is not a string or a list of strings" ,
491+ ),
458492 )
459493
460494 for module in modules :
@@ -470,9 +504,12 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
470504 and result [old_config_name ][new_key ] != new_value
471505 ):
472506 raise ConfigTOMLValueError (
473- "toml config file contains "
474- "[[tool.mypy.overrides]] sections with conflicting "
475- f"values. Module '{ module } ' has two different values for '{ new_key } '"
507+ _toml_module_error (
508+ config_file ,
509+ "toml config file contains {override} sections with "
510+ f"conflicting values. Module '{ module } ' has "
511+ f"two different values for '{ new_key } '" ,
512+ ),
476513 )
477514 result [old_config_name ][new_key ] = new_value
478515
0 commit comments