From 533c26f0d45324bb5e4c1a157f46ddb2e313f37e Mon Sep 17 00:00:00 2001 From: Tamas Szabo Date: Sun, 4 Oct 2020 22:10:17 +0300 Subject: [PATCH] Fix warnings can't surpressed with quiet. Fixes #1525 --- isort/settings.py | 16 +++++++++------- tests/unit/test_isort.py | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/isort/settings.py b/isort/settings.py index 937e6e067..a89f6d69f 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -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] @@ -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 " @@ -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." @@ -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:" @@ -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)}." diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 19e81b09c..31550f953 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 = (