Skip to content

Commit 5045c0d

Browse files
SimplicityGuyclaude
andcommitted
refactor: replace type: ignore pragmas with proper type narrowing
- common/config.py: use cast(str, var) after the missing-vars ValueError guard to narrow str | None to str for mypy without assert (which bandit B101 would flag); removes 24 type: ignore comments across all five from_env() classmethod implementations - common/health_server.py: replace bare type: ignore with cast("HealthServer", self.server) to express the invariant that HealthHandler is only ever used with HealthServer - schema-init/schema_init.py: widen dict[str, str | int] to dict[str, Any] so psycopg **kwargs unpacking satisfies mypy; removes type: ignore[arg-type] Remaining pragmas are all legitimate suppressions: - noqa: S104 β€” intentional 0.0.0.0 binding in containerised services - noqa: S105 β€” false positives ("bearer" token type, Discogs URL consts) - noqa: ARG001 β€” request: Request required by slowapi rate limiter - type: ignore[arg-type] on add_exception_handler β€” slowapi handler has exc: RateLimitExceeded vs FastAPI ExceptionHandler exc: Exception Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 17a7d9d commit 5045c0d

3 files changed

Lines changed: 31 additions & 30 deletions

File tree

β€Žcommon/config.pyβ€Ž

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from os import getenv
66
from pathlib import Path
77
import sys
8-
from typing import TYPE_CHECKING, Any
8+
from typing import TYPE_CHECKING, Any, cast
99
import warnings
1010

1111

@@ -90,10 +90,10 @@ def from_env(cls) -> "GraphinatorConfig":
9090
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
9191

9292
return cls(
93-
amqp_connection=amqp_connection, # type: ignore
94-
neo4j_address=neo4j_address, # type: ignore
95-
neo4j_username=neo4j_username, # type: ignore
96-
neo4j_password=neo4j_password, # type: ignore
93+
amqp_connection=cast("str", amqp_connection),
94+
neo4j_address=cast("str", neo4j_address),
95+
neo4j_username=cast("str", neo4j_username),
96+
neo4j_password=cast("str", neo4j_password),
9797
)
9898

9999

@@ -132,11 +132,11 @@ def from_env(cls) -> "TableinatorConfig":
132132
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
133133

134134
return cls(
135-
amqp_connection=amqp_connection, # type: ignore
136-
postgres_address=postgres_address, # type: ignore
137-
postgres_username=postgres_username, # type: ignore
138-
postgres_password=postgres_password, # type: ignore
139-
postgres_database=postgres_database, # type: ignore
135+
amqp_connection=cast("str", amqp_connection),
136+
postgres_address=cast("str", postgres_address),
137+
postgres_username=cast("str", postgres_username),
138+
postgres_password=cast("str", postgres_password),
139+
postgres_database=cast("str", postgres_database),
140140
)
141141

142142

@@ -431,11 +431,11 @@ def from_env(cls) -> "ApiConfig":
431431
oauth_encryption_key = getenv("OAUTH_ENCRYPTION_KEY") or None
432432

433433
return cls(
434-
postgres_address=postgres_address, # type: ignore[arg-type]
435-
postgres_username=postgres_username, # type: ignore[arg-type]
436-
postgres_password=postgres_password, # type: ignore[arg-type]
437-
postgres_database=postgres_database, # type: ignore[arg-type]
438-
jwt_secret_key=jwt_secret_key, # type: ignore[arg-type]
434+
postgres_address=cast("str", postgres_address),
435+
postgres_username=cast("str", postgres_username),
436+
postgres_password=cast("str", postgres_password),
437+
postgres_database=cast("str", postgres_database),
438+
jwt_secret_key=cast("str", jwt_secret_key),
439439
redis_url=redis_url,
440440
jwt_algorithm=jwt_algorithm,
441441
jwt_expire_minutes=jwt_expire_minutes,
@@ -503,13 +503,13 @@ def from_env(cls) -> "CuratorConfig":
503503
cors_origins = [o.strip() for o in cors_origins_env.split(",") if o.strip()] if cors_origins_env else None
504504

505505
return cls(
506-
postgres_address=postgres_address, # type: ignore[arg-type]
507-
postgres_username=postgres_username, # type: ignore[arg-type]
508-
postgres_password=postgres_password, # type: ignore[arg-type]
509-
postgres_database=postgres_database, # type: ignore[arg-type]
510-
neo4j_address=neo4j_address, # type: ignore[arg-type]
511-
neo4j_username=neo4j_username, # type: ignore[arg-type]
512-
neo4j_password=neo4j_password, # type: ignore[arg-type]
506+
postgres_address=cast("str", postgres_address),
507+
postgres_username=cast("str", postgres_username),
508+
postgres_password=cast("str", postgres_password),
509+
postgres_database=cast("str", postgres_database),
510+
neo4j_address=cast("str", neo4j_address),
511+
neo4j_username=cast("str", neo4j_username),
512+
neo4j_password=cast("str", neo4j_password),
513513
discogs_user_agent=discogs_user_agent,
514514
cors_origins=cors_origins,
515515
)
@@ -550,9 +550,9 @@ def from_env(cls) -> "ExploreConfig":
550550
jwt_secret_key = getenv("JWT_SECRET_KEY") or None
551551

552552
return cls(
553-
neo4j_address=neo4j_address, # type: ignore
554-
neo4j_username=neo4j_username, # type: ignore
555-
neo4j_password=neo4j_password, # type: ignore
553+
neo4j_address=cast("str", neo4j_address),
554+
neo4j_username=cast("str", neo4j_username),
555+
neo4j_password=cast("str", neo4j_password),
556556
jwt_secret_key=jwt_secret_key,
557557
)
558558

β€Žcommon/health_server.pyβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import logging
88
from threading import Thread
9-
from typing import Any
9+
from typing import Any, cast
1010

1111

1212
logger = logging.getLogger(__name__)
@@ -19,7 +19,7 @@ def do_GET(self) -> None:
1919
"""Handle GET requests."""
2020
if self.path == "/health":
2121
# Get health data from the server instance
22-
health_data = self.server.get_health_data() # type: ignore
22+
health_data = cast("HealthServer", self.server).get_health_data()
2323

2424
self.send_response(200)
2525
self.send_header("Content-Type", "application/json")

β€Žschema-init/schema_init.pyβ€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import sys
1616
from pathlib import Path
17+
from typing import Any
1718

1819
import psycopg
1920
import structlog
@@ -42,7 +43,7 @@
4243
POSTGRES_DATABASE = os.environ.get("POSTGRES_DATABASE", "discogsography")
4344

4445

45-
def _postgres_connection_params() -> dict[str, str | int]:
46+
def _postgres_connection_params() -> dict[str, Any]:
4647
"""Parse POSTGRES_ADDRESS into psycopg connection params."""
4748
if ":" in POSTGRES_ADDRESS:
4849
host, port_str = POSTGRES_ADDRESS.split(":", 1)
@@ -62,11 +63,11 @@ def _postgres_connection_params() -> dict[str, str | int]:
6263
# ── PostgreSQL ────────────────────────────────────────────────────────────────
6364

6465

65-
def _ensure_postgres_database(params: dict[str, str | int]) -> None:
66+
def _ensure_postgres_database(params: dict[str, Any]) -> None:
6667
"""Create the target database if it does not already exist (synchronous)."""
6768
admin_params = {**params, "dbname": "postgres"}
6869
logger.info("πŸ”§ Ensuring PostgreSQL database exists...", database=POSTGRES_DATABASE)
69-
with psycopg.connect(**admin_params) as conn: # type: ignore[arg-type]
70+
with psycopg.connect(**admin_params) as conn:
7071
conn.autocommit = True
7172
with conn.cursor() as cursor:
7273
cursor.execute(

0 commit comments

Comments
Β (0)