Skip to content

Commit 855c762

Browse files
authored
Some typing fixes (#581)
* Some typing fixes * Use explicit Never return type * Add command name to error messages
1 parent 562984b commit 855c762

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

rsconnect/actions_content.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,37 @@ def build_add_content(
8383
build_store.set_content_item_build_status(content["guid"], BuildStatus.NEEDS_BUILD)
8484

8585

86+
def _validate_build_rm_args(guid: Optional[str], all: bool, purge: bool):
87+
if guid and all:
88+
raise RSConnectException("You must specify only one of -g/--guid or --all, not both.")
89+
if not guid and not all:
90+
raise RSConnectException("You must specify one of -g/--guid or --all.")
91+
92+
8693
def build_remove_content(
8794
connect_server: RSConnectServer,
88-
guid: str,
89-
all: bool = False,
90-
purge: bool = False,
95+
guid: Optional[str],
96+
all: bool,
97+
purge: bool,
9198
) -> list[str]:
9299
"""
93100
:return: A list of guids of the content items that were removed
94101
"""
102+
103+
# Make sure that either `guid` is a string or `all == True`, but not both.
104+
_validate_build_rm_args(guid, all, purge)
105+
95106
build_store = ensure_content_build_store(connect_server)
96107
if build_store.get_build_running():
97108
raise RSConnectException(
98109
"There is a build running on this server, " + "please wait for it to finish before removing content."
99110
)
100-
guids: list[str] = [guid]
111+
guids: list[str]
101112
if all:
102113
guids = [c["guid"] for c in build_store.get_content_items()]
114+
else:
115+
# If we got here, we know `guid` is not None.
116+
guids = [cast(str, guid)]
103117
for guid in guids:
104118
build_store.remove_content_item(guid, purge)
105119
return guids
@@ -125,7 +139,7 @@ def build_start(
125139
running: bool = False,
126140
retry: bool = False,
127141
all: bool = False,
128-
poll_wait: int = 2,
142+
poll_wait: float = 2,
129143
debug: bool = False,
130144
):
131145
build_store = ensure_content_build_store(connect_server)
@@ -288,7 +302,7 @@ def _monitor_build(connect_server: RSConnectServer, content_items: list[ContentI
288302
return True
289303

290304

291-
def _build_content_item(connect_server: RSConnectServer, content: ContentItemWithBuildState, poll_wait: int):
305+
def _build_content_item(connect_server: RSConnectServer, content: ContentItemWithBuildState, poll_wait: float):
292306
build_store = ensure_content_build_store(connect_server)
293307
with RSConnectClient(connect_server) as client:
294308
# Pending futures will still try to execute when ThreadPoolExecutor.shutdown() is called

rsconnect/main.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
else:
1818
from typing_extensions import ParamSpec
1919

20+
if sys.version_info >= (3, 11):
21+
from typing import Never
22+
else:
23+
from typing_extensions import Never
24+
2025
from rsconnect.certificates import read_certificate_file
2126

2227
from . import VERSION, api, validation
@@ -101,7 +106,7 @@
101106
def cli_exception_handler(func: Callable[P, T]) -> Callable[P, T]:
102107
@wraps(func)
103108
def wrapper(*args: P.args, **kwargs: P.kwargs):
104-
def failed(err: str):
109+
def failed(err: str) -> Never:
105110
click.secho(str(err), fg="bright_red", err=False)
106111
sys.exit(1)
107112

@@ -643,6 +648,8 @@ def details(
643648
set_verbosity(verbose)
644649

645650
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert).validate_server()
651+
if not isinstance(ce.remote_server, RSConnectServer):
652+
raise RSConnectException("`rsconnect details` requires a Posit Connect server.")
646653

647654
click.echo(" Posit Connect URL: %s" % ce.remote_server.url)
648655

@@ -2102,13 +2109,6 @@ def _write_framework_manifest(
21022109
write_environment_file(environment, directory)
21032110

21042111

2105-
def _validate_build_rm_args(guid: str, all: bool, purge: bool):
2106-
if guid and all:
2107-
raise RSConnectException("You must specify only one of -g/--guid or --all, not both.")
2108-
if not guid and not all:
2109-
raise RSConnectException("You must specify one of -g/--guid or --all.")
2110-
2111-
21122112
@cli.group(no_args_is_help=True, help="Interact with Posit Connect's content API.")
21132113
def content():
21142114
pass
@@ -2217,6 +2217,8 @@ def content_describe(
22172217
output_params(ctx, locals().items())
22182218
with cli_feedback("", stderr=True):
22192219
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2220+
if not isinstance(ce.remote_server, RSConnectServer):
2221+
raise RSConnectException("`rsconnect content describe` requires a Posit Connect server.")
22202222
result = get_content(ce.remote_server, guid)
22212223
json.dump(result, sys.stdout, indent=2)
22222224

@@ -2264,6 +2266,8 @@ def content_bundle_download(
22642266
output_params(ctx, locals().items())
22652267
with cli_feedback("", stderr=True):
22662268
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2269+
if not isinstance(ce.remote_server, RSConnectServer):
2270+
raise RSConnectException("`rsconnect content download-bundle` requires a Posit Connect server.")
22672271
if exists(output) and not overwrite:
22682272
raise RSConnectException("The output file already exists: %s" % output)
22692273

@@ -2306,6 +2310,8 @@ def add_content_build(
23062310
output_params(ctx, locals().items())
23072311
with cli_feedback("", stderr=True):
23082312
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2313+
if not isinstance(ce.remote_server, RSConnectServer):
2314+
raise RSConnectException("`rsconnect content build add` requires a Posit Connect server.")
23092315
build_add_content(ce.remote_server, guid)
23102316
if len(guid) == 1:
23112317
logger.info('Added "%s".' % guid[0])
@@ -2356,7 +2362,8 @@ def remove_content_build(
23562362
output_params(ctx, locals().items())
23572363
with cli_feedback("", stderr=True):
23582364
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2359-
_validate_build_rm_args(guid, all, purge)
2365+
if not isinstance(ce.remote_server, RSConnectServer):
2366+
raise RSConnectException("`rsconnect content build rm` requires a Posit Connect server.")
23602367
guids = build_remove_content(ce.remote_server, guid, all, purge)
23612368
if len(guids) == 1:
23622369
logger.info('Removed "%s".' % guids[0])
@@ -2400,6 +2407,8 @@ def list_content_build(
24002407
output_params(ctx, locals().items())
24012408
with cli_feedback("", stderr=True):
24022409
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2410+
if not isinstance(ce.remote_server, RSConnectServer):
2411+
raise RSConnectException("`rsconnect content build ls` requires a Posit Connect server.")
24032412
result = build_list_content(ce.remote_server, guid, status)
24042413
json.dump(result, sys.stdout, indent=2)
24052414

@@ -2432,6 +2441,8 @@ def get_build_history(
24322441
with cli_feedback("", stderr=True):
24332442
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert)
24342443
ce.validate_server()
2444+
if not isinstance(ce.remote_server, RSConnectServer):
2445+
raise RSConnectException("`rsconnect content build history` requires a Posit Connect server.")
24352446
result = build_history(ce.remote_server, guid)
24362447
json.dump(result, sys.stdout, indent=2)
24372448

@@ -2481,6 +2492,8 @@ def get_build_logs(
24812492
output_params(ctx, locals().items())
24822493
with cli_feedback("", stderr=True):
24832494
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2495+
if not isinstance(ce.remote_server, RSConnectServer):
2496+
raise RSConnectException("`rsconnect content build logs` requires a Posit Connect server.")
24842497
for line in emit_build_log(ce.remote_server, guid, format, task_id):
24852498
sys.stdout.write(line)
24862499

@@ -2548,6 +2561,8 @@ def start_content_build(
25482561
logger.set_log_output_format(format)
25492562
with cli_feedback("", stderr=True):
25502563
ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server()
2564+
if not isinstance(ce.remote_server, RSConnectServer):
2565+
raise RSConnectException("rsconnect content build run` requires a Posit Connect server.")
25512566
build_start(ce.remote_server, parallelism, aborted, error, running, retry, all, poll_wait, debug)
25522567

25532568

0 commit comments

Comments
 (0)