-
Notifications
You must be signed in to change notification settings - Fork 91
fix: pkg_resources deprecation warning on runtime #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d0fc9c9
cd3caf9
0010f12
02f2d64
bcd5487
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,7 @@ | ||
| import os | ||
| import json | ||
| import requests | ||
| import pkg_resources | ||
|
|
||
| from pkg_resources import DistributionNotFound | ||
| import warnings | ||
|
|
||
| from types import ModuleType | ||
|
|
||
|
|
@@ -84,9 +82,30 @@ def _update_user_agent_header(self, options): | |
| def _get_version(self): | ||
| version = "" | ||
| try: # nosemgrep : gitlab.bandit.B110 | ||
| version = pkg_resources.require("razorpay")[0].version | ||
| except DistributionNotFound: # pragma: no cover | ||
| pass | ||
| # Try importlib.metadata first (modern approach) | ||
| try: | ||
| import importlib.metadata | ||
| from importlib.metadata import PackageNotFoundError | ||
| version = importlib.metadata.version("razorpay") | ||
| except ImportError: | ||
| # Fall back to pkg_resources | ||
| import pkg_resources | ||
| from pkg_resources import DistributionNotFound | ||
| version = pkg_resources.require("razorpay")[0].version | ||
| except (PackageNotFoundError, DistributionNotFound, NameError): # pragma: no cover | ||
| # PackageNotFoundError: importlib.metadata couldn't find the package | ||
| # DistributionNotFound: pkg_resources couldn't find the package | ||
| # NameError: in case the exception classes aren't defined due to import issues | ||
|
|
||
| # If all else fails, use the hardcoded version from the package | ||
| version = "1.4.3" | ||
|
|
||
| warnings.warn( | ||
| "Could not detect razorpay package version. Using fallback version." | ||
| "This may indicate an installation issue.", | ||
| UserWarning, | ||
| stacklevel=4 | ||
| ) | ||
| return version | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning an empty string on exception is safe, but would it be more useful to return None or log a warning for SDK users who may rely on version info?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @razorpay-sanjib Returning
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please check this commit |
||
|
|
||
| def _get_app_details_ua(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching multiple exceptions ensures robustness, but consider logging a warning if version retrieval fails, to aid client debugging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching NameError is thoughtful for edge-case compatibility, but it may also mask unrelated issues. Consider logging or at least adding a comment for future maintainers.