summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTamas Szabo <szabtam@gmail.com>2020-10-04 22:10:17 +0300
committerTamas Szabo <szabtam@gmail.com>2020-10-04 22:10:17 +0300
commit533c26f0d45324bb5e4c1a157f46ddb2e313f37e (patch)
tree7325db38c27f79ce5d1bf8edfc1157edbf8c0589
parent7aa7d1f603301c7fae7bf6a7839b00b5c6b75d6f (diff)
downloadisort-issue/1525/bug-early-warnings-cant-be-surpressed-with-quiet.tar.gz
Fix warnings can't surpressed with quiet.issue/1525/bug-early-warnings-cant-be-surpressed-with-quiet
Fixes #1525
-rw-r--r--isort/settings.py16
-rw-r--r--tests/unit/test_isort.py6
2 files changed, 15 insertions, 7 deletions
diff --git a/isort/settings.py b/isort/settings.py
index 937e6e06..a89f6d69 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -266,6 +266,11 @@ class Config(_Config):
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 @@ class Config(_Config):
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 @@ class Config(_Config):
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 @@ class Config(_Config):
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 @@ class Config(_Config):
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 19e81b09..31550f95 100644
--- a/tests/unit/test_isort.py
+++ b/tests/unit/test_isort.py
@@ -4825,6 +4825,12 @@ def test_deprecated_settings():
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 = (