Skip to content

Commit

Permalink
Fix warnings can't surpressed with quiet.
Browse files Browse the repository at this point in the history
Fixes #1525
  • Loading branch information
sztamas committed Oct 4, 2020
1 parent 7aa7d1f commit 533c26f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions isort/settings.py
Expand Up @@ -266,6 +266,11 @@ def __init__(
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]
Expand All @@ -276,7 +281,7 @@ def __init__(
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 "
Expand Down Expand Up @@ -343,7 +348,7 @@ def __init__(
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."
Expand All @@ -355,10 +360,7 @@ def __init__(
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:"
Expand Down Expand Up @@ -425,7 +427,7 @@ def __init__(
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)}."
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_isort.py
Expand Up @@ -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 = (
Expand Down

0 comments on commit 533c26f

Please sign in to comment.