From d0fc9c902e7fd1ed0188076ea1d9b2269821e3c9 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Tue, 5 Aug 2025 16:44:04 +0530 Subject: [PATCH 1/2] added importmetadata for get version --- razorpay/client.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/razorpay/client.py b/razorpay/client.py index ea6cf08d..99937174 100644 --- a/razorpay/client.py +++ b/razorpay/client.py @@ -1,9 +1,9 @@ import os import json import requests -import pkg_resources +import importlib.metadata -from pkg_resources import DistributionNotFound +from importlib.metadata import PackageNotFoundError from types import ModuleType @@ -84,8 +84,20 @@ 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 + # 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 pass return version From 0010f12b92a597be41d470f2a1cd143641f65332 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Mon, 25 Aug 2025 13:16:53 +0530 Subject: [PATCH 2/2] added fallback with warning --- razorpay/client.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/razorpay/client.py b/razorpay/client.py index 99937174..9fdadb86 100644 --- a/razorpay/client.py +++ b/razorpay/client.py @@ -1,9 +1,7 @@ import os import json import requests -import importlib.metadata - -from importlib.metadata import PackageNotFoundError +import warnings from types import ModuleType @@ -98,7 +96,16 @@ def _get_version(self): # 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 - pass + + # 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 def _get_app_details_ua(self):