Fix UnboundLocalError in _get_version() when package metadata is missing - #342
Open
yashbudhia wants to merge 1 commit into
Open
Fix UnboundLocalError in _get_version() when package metadata is missing#342yashbudhia wants to merge 1 commit into
yashbudhia wants to merge 1 commit into
Conversation
_get_version() imported DistributionNotFound only inside the pkg_resources fallback branch, but named it in the except clause that also guards the importlib.metadata path. When importlib.metadata.version() raised PackageNotFoundError, evaluating that except tuple hit the unbound name and the original exception was replaced by UnboundLocalError, which propagated out of _update_user_agent_header() and so out of every request. That is exactly the situation the fallback was written for, e.g. running the SDK from a source checkout or a vendored copy that was never pip-installed. The exception class is now resolved before the lookup, so the fallback version and its warning are actually used. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011ZJQzxtgWvkWobxsDcuLoW
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.
Client._get_version()has a fallback for when the package metadata cannot be found, but the fallback never runs. Instead every request raisesUnboundLocalError.Reproduce (any environment where
razorpayis importable but not installed, e.g. a source checkout or a vendored copy):Cause
DistributionNotFoundis imported only inside thepkg_resourcesbranch, but it is named in theexcept (PackageNotFoundError, DistributionNotFound, NameError)clause that also guards theimportlib.metadatapath. Whenimportlib.metadata.version()raisesPackageNotFoundError, Python evaluates that tuple, hits the unbound local, and the newUnboundLocalErrorreplaces the original exception. It is raised while evaluating theexceptexpression, so the clause itself cannot catch it, and it propagates out of_update_user_agent_header()and therefore out of every API call.Fix
Resolve the "not found" exception class before doing the lookup, then look up the version in its own
try. Behaviour when the package is installed is unchanged; when the metadata is missing the documented fallback version is returned with the existingUserWarning. The fallback value itself is left as it was.Tests
tests/test_client_version.pycovers the installed path, the fallback with its warning, and that a request's User-Agent header can still be built without metadata. Full suite: 160 passed, 1 skipped.