Skip to content

Commit a60baee

Browse files
committed
Resolve pylint warnings
1 parent 60fe6c6 commit a60baee

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed

Diff for: python/cog/predictor.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def predict(self, **kwargs: Any) -> Any:
7070
"""
7171
Run a single prediction on the model
7272
"""
73-
pass
7473

7574
def log(self, *messages: str) -> None:
7675
"""
@@ -174,7 +173,7 @@ def load_config() -> CogConfig:
174173
# Assumes the working directory is /src
175174
config_path = os.path.abspath("cog.yaml")
176175
try:
177-
with open(config_path) as fh:
176+
with open(config_path, encoding="utf-8") as fh:
178177
config = yaml.safe_load(fh)
179178
except FileNotFoundError as e:
180179
raise ConfigDoesNotExist(
@@ -548,11 +547,12 @@ class TrainingOutput(TrainingOutputType): # type: ignore
548547
pass
549548

550549
return TrainingOutput
550+
else:
551551

552-
class TrainingOutput(BaseModel):
553-
__root__: TrainingOutputType # type: ignore
552+
class TrainingOutput(BaseModel):
553+
__root__: TrainingOutputType # type: ignore
554554

555-
return TrainingOutput
555+
return TrainingOutput
556556

557557

558558
def human_readable_type_name(t: Type[Union[Any, None]]) -> str:

Diff for: python/cog/server/clients.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def _get_version() -> str:
3838
pass
3939
else:
4040
return version("cog")
41-
import pkg_resources
41+
import pkg_resources # pylint: disable=import-outside-toplevel
4242

4343
return pkg_resources.get_distribution("cog").version
44-
except Exception:
44+
except Exception: # pylint: disable=broad-exception-caught
4545
return "unknown"
4646

4747

48-
_user_agent = f"cog-worker/{_get_version()} {httpx._client.USER_AGENT}"
48+
_user_agent = f"cog-worker/{_get_version()}"
4949
_response_interval = float(os.environ.get("COG_THROTTLE_RESPONSE_INTERVAL", 0.5))
5050

5151
# HACK: signal that we should skip the start webhook when the response interval

Diff for: python/cog/server/http.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ async def healthcheck_startup_failed() -> Any:
102102

103103

104104
def create_app(
105-
config: CogConfig,
106-
shutdown_event: Optional[threading.Event],
107-
threads: int = 1,
105+
config: CogConfig, # pylint: disable=redefined-outer-name
106+
shutdown_event: Optional[threading.Event], # pylint: disable=redefined-outer-name
107+
threads: int = 1, # pylint: disable=redefined-outer-name
108108
upload_url: Optional[str] = None,
109109
mode: str = "predict",
110110
is_build: bool = False,
111+
await_explicit_shutdown: bool = False, # pylint: disable=redefined-outer-name
111112
) -> MyFastAPI:
112-
app = MyFastAPI(
113+
app = MyFastAPI( # pylint: disable=redefined-outer-name
113114
title="Cog", # TODO: mention model name?
114115
# version=None # TODO
115116
)
@@ -242,7 +243,8 @@ def startup() -> None:
242243
app.state.setup_result
243244
and app.state.setup_result.status == schema.Status.FAILED
244245
):
245-
if not args.await_explicit_shutdown: # signal shutdown if interactive run
246+
# signal shutdown if interactive run
247+
if not await_explicit_shutdown:
246248
if shutdown_event is not None:
247249
shutdown_event.set()
248250
else:
@@ -469,9 +471,9 @@ def stop(self) -> None:
469471
os.kill(os.getpid(), signal.SIGKILL)
470472

471473

472-
def is_port_in_use(port: int) -> bool:
473-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
474-
return s.connect_ex(("localhost", port)) == 0
474+
def is_port_in_use(port: int) -> bool: # pylint: disable=redefined-outer-name
475+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
476+
return sock.connect_ex(("localhost", port)) == 0
475477

476478

477479
def signal_ignore(signum: Any, frame: Any) -> None: # pylint: disable=unused-argument
@@ -549,17 +551,25 @@ def _cpu_count() -> int:
549551
threads = _cpu_count()
550552

551553
shutdown_event = threading.Event()
554+
555+
await_explicit_shutdown = args.await_explicit_shutdown
556+
if await_explicit_shutdown:
557+
signal.signal(signal.SIGTERM, signal_ignore)
558+
else:
559+
signal.signal(signal.SIGTERM, signal_set_event(shutdown_event))
560+
552561
app = create_app(
553562
config=config,
554563
shutdown_event=shutdown_event,
555564
threads=threads,
556565
upload_url=args.upload_url,
557566
mode=args.mode,
567+
await_explicit_shutdown=await_explicit_shutdown,
558568
)
559569

560570
host: str = args.host
561571

562-
port = int(os.getenv("PORT", 5000))
572+
port = int(os.getenv("PORT", "5000"))
563573
if is_port_in_use(port):
564574
log.error(f"Port {port} is already in use")
565575
sys.exit(1)
@@ -573,11 +583,6 @@ def _cpu_count() -> int:
573583
workers=1,
574584
)
575585

576-
if args.await_explicit_shutdown:
577-
signal.signal(signal.SIGTERM, signal_ignore)
578-
else:
579-
signal.signal(signal.SIGTERM, signal_set_event(shutdown_event))
580-
581586
s = Server(config=server_config)
582587
s.start()
583588

@@ -589,6 +594,6 @@ def _cpu_count() -> int:
589594
s.stop()
590595

591596
# return error exit code when setup failed and cog is running in interactive mode (not k8s)
592-
if app.state.setup_result and not args.await_explicit_shutdown:
597+
if app.state.setup_result and not await_explicit_shutdown:
593598
if app.state.setup_result.status == schema.Status.FAILED:
594599
exit(-1)

Diff for: python/cog/server/probes.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ def __init__(self, root: PathLike = None) -> None:
2424
self._root.mkdir(exist_ok=True, parents=True)
2525
except OSError:
2626
log.error(
27-
f"Failed to create cog runtime state directory ({self._root}). "
27+
"Failed to create cog runtime state directory (%s). "
2828
"Does it already exist and is a file? Does the user running cog "
29-
"have permissions?"
29+
"have permissions?",
30+
self._root,
3031
)
3132
else:
3233
self._enabled = True

Diff for: python/cog/server/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ async def inner() -> SetupResult:
192192
logs.append(event.message)
193193
elif isinstance(event, Done):
194194
if event.error:
195+
status = schema.Status.FAILED
195196
raise FatalWorkerException(
196197
"Predictor errored during setup: " + event.error_detail
197198
)
198-
status = schema.Status.FAILED
199199
else:
200200
status = schema.Status.SUCCEEDED
201201
self._state = WorkerState.IDLE

Diff for: python/cog/server/worker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ async def read(
9999
def emit_metric(metric_name: str, metric_value: "float | int") -> None:
100100
worker = worker_reference.get(None, None)
101101
if worker is None:
102-
raise Exception("Attempted to emit metric but worker is not running")
103-
worker._emit_metric(metric_name, metric_value)
102+
raise RuntimeError("Attempted to emit metric but worker is not running")
103+
worker.emit_metric(metric_name, metric_value)
104104

105105

106106
class _ChildWorker(_spawn.Process): # type: ignore

Diff for: python/cog/suppress_output.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
@contextmanager
88
def suppress_output() -> Iterator[None]:
9-
null_out = open(os.devnull, "w")
10-
null_err = open(os.devnull, "w")
9+
null_out = open(os.devnull, "w", encoding="utf-8")
10+
null_err = open(os.devnull, "w", encoding="utf-8")
1111
out_fd = sys.stdout.fileno()
1212
err_fd = sys.stderr.fileno()
1313
out_dup_fd = os.dup(out_fd)

0 commit comments

Comments
 (0)