fix: import urllib.parse explicitly instead of relying on side effects - #2804
Open
skippdot wants to merge 1 commit into
Open
fix: import urllib.parse explicitly instead of relying on side effects#2804skippdot wants to merge 1 commit into
skippdot wants to merge 1 commit into
Conversation
- Replace `import urllib` with `import urllib.parse` in _helpers.py, discovery.py, http.py and model.py, which all call `urllib.parse.*` - `import urllib` does not import submodules; the attribute only exists once some other module has imported urllib.parse, so _helpers crashed with AttributeError when used before any such import, and the other three modules depended on httplib2 importing it transitively - Call sites are unchanged; this also fixes the 7 AttributeError failures when running tests/test__helpers.py in isolation Fixes googleapis#2803
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.
Fixes #2803
Four modules import the bare
urllibpackage but callurllib.parse.*:_helpers.py(4 call sites),discovery.py(6),http.py(6),model.py(1).import urllibdoes not import submodules —urllib.parseonly becomes an attribute of the package after some module executesimport urllib.parse. Today that happens as a side effect ofhttplib2being imported transitively, which is why the bug is invisible in most runs._helpers.pyhas no third-party imports, so it crashes outright:The same mechanism is why
pytest tests/test__helpers.pyin isolation fails 7 of 10 tests onmainwhile the full-suite run passes.Change
Replace
import urllibwithimport urllib.parsein the four modules. Call sites are untouched —urllib.parse.urlparse(...)keeps its current spelling — so the diff is one line per module, +4/−4. The other three modules are included because they carry the same undeclared dependency onhttplib2's import side effects, just without a currently-reachable crash.Verification (Python 3.10 and 3.14)
http://example.com?a=bafter.pytest tests/test__helpers.pyin isolation: 7 failed / 3 passed → 10 passed.tests/folder: 338 passed, 1 skipped on both Python versions.black --checkclean;flake8 --select=E9,F63,F7,F82(the set CI enforces) clean.