refactor(seed): remove obsolete requests import handling in load_from_api - #972
Conversation
…_api
requests is a core dependency, so the ImportError arm of the except (ImportError, OSError) block in SeedDataManager.load_from_api() cannot realistically fire. The OSError arm was actively harmful: requests.exceptions.RequestException subclasses OSError, so connection errors, timeouts, and raise_for_status() failures were all reported as 'requests library not available. Install with: pip install requests'.
Hoist the lazy import to module scope, matching the ingest stack, and drop the block so genuine failures fall through to the existing ProcessingError('Failed to load from API: ...') with the cause chained. Update the docstring Raises section accordingly and add regression tests covering connection, timeout, HTTP status, and JSON parse failures.
Closes semantica-agi#949
|
ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing |
PR Summary by QodoFix load_from_api error reporting by removing obsolete requests import guard
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can type 'qodo, fix this' on a finding and the fix lands right on your PR |
Description
SeedDataManager.load_from_api()ended withexcept (ImportError, OSError)raisingProcessingError("requests library not available. Install with: pip install requests"). Both arms are wrong:ImportErrorcan't realistically fire.requestsis a core dependency (dependenciesinpyproject.toml), not an optional extra, so the lazy import it was guarding served no purpose.OSErrorwas actively harmful.requests.exceptions.RequestExceptionsubclassesOSError, soConnectionError,Timeout, and theHTTPErrorfromraise_for_status()were all caught by that arm and reported as "requests library not available" — telling users to reinstall a library that was already installed, while hiding the real failure and dropping the exception chain.This removes the block and hoists the import to module scope, matching how the rest of the ingest stack (
api_ingestor,web_ingestor,ssrf, …) importsrequests. Genuine failures now fall through to the pre-existingProcessingError(f"Failed to load from API: {e}") from e, which was already there and is unchanged.Type of Change
Filed as a refactor, but the
OSErroroverlap made it a real behavior bug, so both are ticked.Related Issues
Closes #949
Changes Made
except (ImportError, OSError)block fromSeedDataManager.load_from_api(); request, parsing, and network failures now surface through the existingFailed to load from API: {e}path withfrom echaining intact.import requestsout of thetrybody to module scope, with a comment recording why there is no import guard, so the lazy-import-plus-fallback pattern doesn't get reintroduced.load_from_apidocstring, which advertisedProcessingError"if ... requests library is not available".ConnectionError,Timeout,HTTPError, a failingraise_for_status(), and a malformed JSON body — each asserting the real cause reaches the caller and the "requests library not available" text does not.Testing
python -m build)Build not run — the change touches no packaging metadata, only a module body and a test file.
The new tests were verified to fail against the pre-change code (4 of the 5 fail; the malformed-JSON case passes before and after, since
ValueErrorwas never caught by the removed block — it's there to pin that behavior).Test Commands
28 passed.black/flake8report diffs on both touched files, but those are pre-existing onmain(unrelated lines); the added lines are clean.Documentation
The only stale doc was the method's own
Raises:section, corrected in this PR.Breaking Changes
Breaking Changes: No
Every failure mode still raises
ProcessingErrorfromload_from_api(); only the message text changes, and only for cases that were previously mislabeled. Anything matching on the literal string"requests library not available"would need updating — a grep found no such callers or tests in the repo.Additional Notes
Two things for reviewers:
requests.get()here withrequest_with_ssrf_guard()and touches the same region, so whichever lands second needs a small rebase. refactor: remove obsolete requests import exception handling in SeedDataManager #949 was filed as a pre-existing cleanup, explicitly not a blocker for the SSRF fix, so this is based onmainrather than on that branch.load_from_database()(same class, ~50 lines up) has the identicalexcept (ImportError, OSError)shape, mislabeling real DB and socket errors as "Database ingestion module not available". Deliberately not touched here to keep the PR scoped to the linked issue — happy to fix it in a follow-up, or in this PR if you'd prefer them together.