fix: rename utils/logging.py and utils/http.py to avoid stdlib shadowing#282
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
prune-alertsautomation task (and any other script that importsutils.runner/utils.storebefore the dotenv chain finishes) crashes at module load with:Root cause
utils/logging.pycollides with the stdlibloggingmodule. The local file doesfrom dotenv import load_dotenvat import time.python-dotenv'smain.pydoesimport loggingwhile it's still being initialised, sosys.modules['logging']already points to the partially-builtutils.logging— and the stdlibgetLoggerattribute isn't there yet.Second bug, same shape
utils/http.pyhas the same shadowing pattern against stdlibhttp. Not visible today because theloggingcrash fires first, buturllib3.exceptionsdoesfrom http.client import IncompleteReadwhich loadsutils/http.pyashttp, then evaluates the-> requests.Response:annotation whilerequestsis still being initialised:Fixing only the
loggingshadow would just surface this one next CI run.Fix
Rename both files and update the call sites:
utils/logging.py→utils/logger.py(70 .py files + 1 docstring + 2 prose mentions)utils/http.py→utils/http_client.py(11 .py files + 1 logger name)No behaviour change — these are pure module-path renames.
Verification
Both invocations match what production runs (the venv interpreter, picked up via
uv run). The test count and pass/fail tally is identical to baselinemain(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>.pymatches a stdlib module name would prevent the next instance of this. Happy to send a follow-up PR if useful.