Fix warnings can't surpressed with quiet.
Fixes #1525
diff --git a/isort/settings.py b/isort/settings.py
index 937e6e0..a89f6d6 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -266,6 +266,11 @@
super().__init__(**config_vars) # type: ignore
return
+ # We can't use self.quiet to conditionally show warnings before super.__init__() is called
+ # at the end of this method. _Config is also frozen so setting self.quiet isn't possible.
+ # Therefore we extract quiet early here in a variable and use that in warning conditions.
+ quiet = config_overrides.get("quiet", False)
+
sources: List[Dict[str, Any]] = [_DEFAULT_SETTINGS]
config_settings: Dict[str, Any]
@@ -276,7 +281,7 @@
CONFIG_SECTIONS.get(os.path.basename(settings_file), FALLBACK_CONFIG_SECTIONS),
)
project_root = os.path.dirname(settings_file)
- if not config_settings:
+ if not config_settings and not quiet:
warn(
f"A custom settings file was specified: {settings_file} but no configuration "
"was found inside. This can happen when [settings] is used as the config "
@@ -343,7 +348,7 @@
combined_config.pop(key)
if maps_to_section in KNOWN_SECTION_MAPPING:
section_name = f"known_{KNOWN_SECTION_MAPPING[maps_to_section].lower()}"
- if section_name in combined_config and not self.quiet:
+ if section_name in combined_config and not quiet:
warn(
f"Can't set both {key} and {section_name} in the same config file.\n"
f"Default to {section_name} if unsure."
@@ -355,10 +360,7 @@
combined_config[section_name] = frozenset(value)
else:
known_other[import_heading] = frozenset(value)
- if (
- maps_to_section not in combined_config.get("sections", ())
- and not self.quiet
- ):
+ if maps_to_section not in combined_config.get("sections", ()) and not quiet:
warn(
f"`{key}` setting is defined, but {maps_to_section} is not"
" included in `sections` config option:"
@@ -425,7 +427,7 @@
if deprecated_options_used:
for deprecated_option in deprecated_options_used:
combined_config.pop(deprecated_option)
- if not self.quiet:
+ if not quiet:
warn(
"W0503: Deprecated config options were used: "
f"{', '.join(deprecated_options_used)}."
diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py
index 19e81b0..31550f9 100644
--- a/tests/unit/test_isort.py
+++ b/tests/unit/test_isort.py
@@ -4825,6 +4825,12 @@
assert isort.code("hi", not_skip=True)
+def test_deprecated_settings_no_warn_in_quiet_mode(recwarn):
+ """Test to ensure isort does NOT warn in quiet mode even though settings are deprecated"""
+ assert isort.code("hi", not_skip=True, quiet=True)
+ assert not recwarn
+
+
def test_only_sections() -> None:
"""Test to ensure that the within sections relative position of imports are maintained"""
test_input = (