Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings can't surpressed with quiet. #1526

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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