Skip to content

Commit 289fbdd

Browse files
authored
Merge pull request #1737 from dbcli/RW/respect-no-show-warnings
Respect `--no-show-warnings`
2 parents 2775a15 + 031d518 commit 289fbdd

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
Bug Fixes
1010
---------
1111
* Correct how password help is rendered in the helpdoc.
12+
* Respect `--no-show-warnings`, overriding settings in `~/.myclirc`.
1213

1314

1415
Internal

mycli/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ def __init__(
194194
defaults_file: str | None = None,
195195
login_path: str | None = None,
196196
auto_vertical_output: bool = False,
197-
show_warnings: bool = False,
198197
warn: bool | None = None,
199198
myclirc: str = "~/.myclirc",
200199
) -> None:
@@ -277,7 +276,7 @@ def __init__(
277276

278277
# read from cli argument or user config file
279278
self.auto_vertical_output = auto_vertical_output or c["main"].as_bool("auto_vertical_output")
280-
self.show_warnings = show_warnings or c["main"].as_bool("show_warnings")
279+
self.show_warnings = c["main"].as_bool("show_warnings")
281280

282281
# Write user config if system config wasn't the last config loaded.
283282
if c.filename not in self.system_config_files and not os.path.exists(myclirc):
@@ -608,6 +607,7 @@ def connect(
608607
use_keyring: bool | None = None,
609608
reset_keyring: bool | None = None,
610609
keepalive_ticks: int | None = None,
610+
show_warnings: bool | None = None,
611611
) -> None:
612612
cnf = {
613613
"database": None,
@@ -637,6 +637,8 @@ def connect(
637637
ssl_config: dict[str, Any] = ssl or {}
638638
user_connection_config = self.config_without_package_defaults.get('connection', {})
639639
self.keepalive_ticks = keepalive_ticks
640+
if show_warnings is not None:
641+
self.show_warnings = show_warnings
640642

641643
int_port = port and int(port)
642644
if not int_port:
@@ -2093,9 +2095,10 @@ class CliArgs:
20932095
is_flag=True,
20942096
help='Automatically switch to vertical output mode if the result is wider than the terminal width.',
20952097
)
2096-
show_warnings: bool = clickdc.option(
2098+
show_warnings: bool | None = clickdc.option(
20972099
'--show-warnings/--no-show-warnings',
20982100
is_flag=True,
2101+
default=None,
20992102
clickdc=None,
21002103
help='Automatically show warnings after executing a SQL statement.',
21012104
)
@@ -2517,10 +2520,6 @@ def get_password_from_file(password_file: str | None) -> str | None:
25172520

25182521
combined_init_cmd = "; ".join(cmd.strip() for cmd in init_cmds if cmd)
25192522

2520-
# --show-warnings / --no-show-warnings
2521-
if cli_args.show_warnings:
2522-
mycli.show_warnings = cli_args.show_warnings
2523-
25242523
if cli_args.use_keyring is not None and cli_args.use_keyring.lower() == 'reset':
25252524
use_keyring = True
25262525
reset_keyring = True
@@ -2627,6 +2626,7 @@ def get_password_from_file(password_file: str | None) -> str | None:
26272626
use_keyring=use_keyring,
26282627
reset_keyring=reset_keyring,
26292628
keepalive_ticks=keepalive_ticks,
2629+
show_warnings=cli_args.show_warnings,
26302630
)
26312631

26322632
if combined_init_cmd:

test/test_main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,49 @@ def test_output_with_warning_and_show_warnings_disabled(executor):
460460
assert expected not in result.output
461461

462462

463+
@dbtest
464+
def test_no_show_warnings_overrides_myclirc_setting(executor):
465+
runner = CliRunner()
466+
sql = 'EXPLAIN SELECT 1'
467+
expected = 'select 1'
468+
469+
with NamedTemporaryFile(prefix=TEMPFILE_PREFIX, mode='w', delete=False) as myclirc:
470+
myclirc.write(
471+
dedent("""\
472+
[main]
473+
show_warnings = True
474+
""")
475+
)
476+
myclirc.flush()
477+
args = [
478+
'--user',
479+
USER,
480+
'--host',
481+
HOST,
482+
'--port',
483+
PORT,
484+
'--password',
485+
PASSWORD,
486+
'--myclirc',
487+
myclirc.name,
488+
'--defaults-file',
489+
default_config_file,
490+
TEST_DATABASE,
491+
]
492+
493+
result = runner.invoke(click_entrypoint, args=args, input=sql)
494+
assert expected in result.output
495+
496+
result = runner.invoke(click_entrypoint, args=args + ['--no-show-warnings'], input=sql)
497+
assert expected not in result.output
498+
499+
try:
500+
if os.path.exists(myclirc.name):
501+
os.remove(myclirc.name)
502+
except Exception as e:
503+
print(f"An error occurred while attempting to delete the file: {e}")
504+
505+
463506
@dbtest
464507
def test_output_with_multiple_warnings_in_single_statement(executor):
465508
runner = CliRunner()

0 commit comments

Comments
 (0)