Skip to content

fix: rename utils/logging.py and utils/http.py to avoid stdlib shadowing#282

Merged
spalen0 merged 1 commit into
mainfrom
fix/rename-utils-logging
Jun 16, 2026
Merged

fix: rename utils/logging.py and utils/http.py to avoid stdlib shadowing#282
spalen0 merged 1 commit into
mainfrom
fix/rename-utils-logging

Conversation

@spalen0

@spalen0 spalen0 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Problem

The prune-alerts automation task (and any other script that imports utils.runner / utils.store before the dotenv chain finishes) crashes at module load with:

AttributeError: module 'logging' has no attribute 'getLogger'
(consider renaming '/srv/monitoring/utils/logging.py' since it has the same
name as the standard library module named 'logging' and prevents importing
that standard library module)

Root cause

utils/logging.py collides with the stdlib logging module. The local file does from dotenv import load_dotenv at import time. python-dotenv's main.py does import logging while it's still being initialised, so sys.modules['logging'] already points to the partially-built utils.logging — and the stdlib getLogger attribute isn't there yet.

Second bug, same shape

utils/http.py has the same shadowing pattern against stdlib http. Not visible today because the logging crash fires first, but urllib3.exceptions does from http.client import IncompleteRead which loads utils/http.py as http, then evaluates the -> requests.Response: annotation while requests is still being initialised:

AttributeError: partially initialized module 'requests' from
'.../site-packages/requests/__init__.py' has no attribute 'Response'

Fixing only the logging shadow would just surface this one next CI run.

Fix

Rename both files and update the call sites:

  • utils/logging.pyutils/logger.py (70 .py files + 1 docstring + 2 prose mentions)
  • utils/http.pyutils/http_client.py (11 .py files + 1 logger name)

No behaviour change — these are pure module-path renames.

Verification

$ uv run python utils/prune_alerts.py
[2026-06-16 09:45:45,486] INFO utils.prune_alerts: Pruned 0 alert rows older than 30 days
$ uv run --extra dev pytest tests/ -q
521 passed, 4 skipped, 5 warnings, 5 subtests passed in 4.63s

Both invocations match what production runs (the venv interpreter, picked up via uv run). The test count and pass/fail tally is identical to baseline main (the 4 skips are env-gated, unrelated).

Follow-up suggestion (out of scope here)

A CI guard that fails the build if any utils/<name>.py matches a stdlib module name would prevent the next instance of this. Happy to send a follow-up PR if useful.

utils/logging.py collides with the stdlib `logging` module. When a script
under the repo root imports `utils.logging` (which calls load_dotenv at
module load), the partially-initialised local module ends up in
sys.modules['logging'], so the very next `import logging` inside
python-dotenv's main.py resolves back to our half-built module and dies
with:

  AttributeError: module 'logging' has no attribute 'getLogger'
  (consider renaming '/srv/monitoring/utils/logging.py' ...)

This bites the `prune-alerts` automation task (and any other script that
imports utils.runner/utils.store before the dotenv chain completes).

utils/http.py has the same problem against stdlib `http`: urllib3's
exceptions module does `from http.client import IncompleteRead`, which
loads utils/http.py as `http`, and a module-level
`-> requests.Response:` annotation is then evaluated while `requests` is
itself still being initialised. Not surfaced today because the logging
crash fires first, but it would block the same task the moment the
logging shadow is fixed.

Rename both files and update the 70+ import sites and the few
docstring/README references that point at the old names.

Verified by running utils/prune_alerts.py end-to-end (exits 0, prints
the prune count) and `pytest tests/` (521 passed, 4 skipped, unchanged
from baseline).
@spalen0 spalen0 merged commit 63c75b8 into main Jun 16, 2026
2 checks passed
@spalen0 spalen0 deleted the fix/rename-utils-logging branch June 16, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant