Skip to content

Commit 7b8f0ed

Browse files
authored
fix: pkg_resources deprecation warning on runtime (#307)
1 parent 8266419 commit 7b8f0ed

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

razorpay/client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import os
22
import json
33
import requests
4-
import pkg_resources
5-
6-
from pkg_resources import DistributionNotFound
4+
import warnings
75

86
from types import ModuleType
97

@@ -84,9 +82,30 @@ def _update_user_agent_header(self, options):
8482
def _get_version(self):
8583
version = ""
8684
try: # nosemgrep : gitlab.bandit.B110
87-
version = pkg_resources.require("razorpay")[0].version
88-
except DistributionNotFound: # pragma: no cover
89-
pass
85+
# Try importlib.metadata first (modern approach)
86+
try:
87+
import importlib.metadata
88+
from importlib.metadata import PackageNotFoundError
89+
version = importlib.metadata.version("razorpay")
90+
except ImportError:
91+
# Fall back to pkg_resources
92+
import pkg_resources
93+
from pkg_resources import DistributionNotFound
94+
version = pkg_resources.require("razorpay")[0].version
95+
except (PackageNotFoundError, DistributionNotFound, NameError): # pragma: no cover
96+
# PackageNotFoundError: importlib.metadata couldn't find the package
97+
# DistributionNotFound: pkg_resources couldn't find the package
98+
# NameError: in case the exception classes aren't defined due to import issues
99+
100+
# If all else fails, use the hardcoded version from the package
101+
version = "1.4.3"
102+
103+
warnings.warn(
104+
"Could not detect razorpay package version. Using fallback version."
105+
"This may indicate an installation issue.",
106+
UserWarning,
107+
stacklevel=4
108+
)
90109
return version
91110

92111
def _get_app_details_ua(self):

0 commit comments

Comments
 (0)