Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions razorpay/client.py
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

Expand Down Expand Up @@ -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
Copy link
Contributor

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.

Copy link
Contributor

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.

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@ankitdas13 ankitdas13 Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razorpay-sanjib Returning None would break the string formatting in User-Agent header. but we can add warning like this

except:
   warnings.warn(
    "Could not detect razorpay package version. Using fallback version. "
    "This may indicate an installation issue.",
    UserWarning,
     stacklevel=4
   )
return version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check this commit


def _get_app_details_ua(self):
Expand Down
Loading