Fix startup crash caused by top-level geoip2 import
geoip2 is an optional dependency used only when a MaxMind mmdb path is configured. Importing it unconditionally at module level caused the server to crash on startup with ModuleNotFoundError when the package was absent from the environment. Move the imports under TYPE_CHECKING (for static analysis) and add lazy local imports inside init_geoip() and _geoip_lookup() where geoip2 is actually needed at runtime. The server now starts normally without a MaxMind database, and geoip2 is loaded on demand if the feature is used.
This commit is contained in:
@@ -42,13 +42,13 @@ import time
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import geoip2.database
|
||||
import geoip2.errors
|
||||
import structlog
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import aiohttp
|
||||
import aiosqlite
|
||||
import geoip2.database
|
||||
import geoip2.errors
|
||||
|
||||
log: structlog.stdlib.BoundLogger = structlog.get_logger()
|
||||
|
||||
@@ -154,6 +154,8 @@ def init_geoip(mmdb_path: str | None) -> None:
|
||||
return
|
||||
from pathlib import Path # noqa: PLC0415
|
||||
|
||||
import geoip2.database # noqa: PLC0415
|
||||
|
||||
if not Path(mmdb_path).is_file():
|
||||
log.warning("geoip_mmdb_not_found", path=mmdb_path)
|
||||
return
|
||||
@@ -176,6 +178,8 @@ def _geoip_lookup(ip: str) -> GeoInfo | None:
|
||||
"""
|
||||
if _geoip_reader is None:
|
||||
return None
|
||||
import geoip2.errors # noqa: PLC0415
|
||||
|
||||
try:
|
||||
response = _geoip_reader.country(ip)
|
||||
code: str | None = response.country.iso_code or None
|
||||
|
||||
Reference in New Issue
Block a user