fix(sitemap): always return a list from extract_urls_from_sitemap#743
Open
valter-silva-au wants to merge 1 commit into
Open
fix(sitemap): always return a list from extract_urls_from_sitemap#743valter-silva-au wants to merge 1 commit into
valter-silva-au wants to merge 1 commit into
Conversation
- Dedent `return urls` out of the try/else so the function returns the (possibly partial) list on every path instead of falling through to None when the try body raises (unreachable child sitemap, malformed XML, gzip failure). - This fixes the recursion blast-radius where a single failing child sitemap caused urls.extend(None) -> TypeError on the parent, and the documents.py len(None) crash that discarded partial crawl results. - Add tests (pytest-mock): requests.get raising returns [], and a sitemapindex whose child fetch fails still returns a list.
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.
Issue
extract_urls_from_sitemap()inlib/shared/layers/python-sdk/python/genai_core/websites/sitemap.pyplacesreturn urlsinside theelse:clause of atry/except/else. Theelseblock only runs when thetrybody raises nothing, so on any error theexceptswallows the exception, the function falls off the end, and it implicitly returnsNoneinstead of the partial/empty list.Real sitemaps hit this regularly — unreachable child sitemaps, malformed/rejected XML, gzip failures. Two concrete blast radii:
sitemap.py): asitemapindexrecurses into each child viaurls.extend(extract_urls_from_sitemap(elem.text)). If a child errors and returnsNone, the parent doesurls.extend(None)→TypeError: 'NoneType' object is not iterable, which its ownexceptthen swallows — so the whole tree collapses toNone.documents.py):limit = min(limit, len(urls_to_crawl))→len(None)→TypeError, masked by a genericexceptthat discards the partial crawl results.Fix
Dedent
return urlsout of theelse:to function-body level so the function returns the (possibly partial) accumulated list on every path — success, non-200, and exception — and neverNone. Theexceptbranch already leavesurlsas the list gathered so far, so a single trailingreturn urlscovers all cases.Tests
Added to the existing
tests/shared/layers/python-sdk/genai_core/websites/sitemap_test.py, using the repo's establishedmockerfixture (pytest-mock, same style as the siblingcrawler_test.py):test_extract_urls_returns_list_when_request_fails—requests.getraises → asserts the return is[](a list), notNone.test_extract_urls_returns_list_when_child_sitemap_fails— asitemapindexwhose child fetch raises → asserts the parent still returns alistrather than crashing onurls.extend(None).Both tests fail on the current code (RED:
None/'NoneType' object is not iterable) and pass after the dedent (GREEN). The fullpytest tests/suite stays green (269 passed);flake8,black --check, andbanditare clean on the changed files. Pure logic + arequests.getmock — no AWS deploy required.Risk
Low. Single-function control-flow fix touching only the error/return path; the success path is byte-for-byte identical; backward-compatible; no API/CDK-synth surface.