From 4c00eb397a45de6958085b0de7df2a906967e04c Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Wed, 4 Jun 2025 15:09:37 -0400 Subject: [PATCH 1/5] Remove usages of deprecated pkg_resources package --- sdks/python/gen_protos.py | 4 ++-- sdks/python/pyproject.toml | 3 ++- sdks/python/setup.py | 16 +++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sdks/python/gen_protos.py b/sdks/python/gen_protos.py index a2cd1bd4cef3..311e3a1841e1 100644 --- a/sdks/python/gen_protos.py +++ b/sdks/python/gen_protos.py @@ -31,7 +31,7 @@ from collections import defaultdict from importlib import import_module -import pkg_resources +import importlib_resources LOG = logging.getLogger() LOG.setLevel(logging.INFO) @@ -474,7 +474,7 @@ def generate_proto_files(force=False): protoc_gen_mypy = _find_protoc_gen_mypy() from grpc_tools import protoc - builtin_protos = pkg_resources.resource_filename('grpc_tools', '_proto') + builtin_protos = importlib_resources.files('grpc_tools') / '_proto' args = ( [sys.executable] + # expecting to be called from command line ['--proto_path=%s' % builtin_protos] + diff --git a/sdks/python/pyproject.toml b/sdks/python/pyproject.toml index 97a9fe6141ea..337b7a80b287 100644 --- a/sdks/python/pyproject.toml +++ b/sdks/python/pyproject.toml @@ -35,7 +35,8 @@ requires = [ 'pyyaml>=3.12,<7.0.0', # also update Jinja2 bounds in test-suites/xlang/build.gradle (look for xlangWrapperValidation task) "jinja2>=2.7.1,<4.0.0", - 'yapf==0.43.0' + 'yapf==0.43.0', + 'importlib-resources==6.5.2' ] diff --git a/sdks/python/setup.py b/sdks/python/setup.py index d6c4088c1be4..11be4c4e6a1f 100644 --- a/sdks/python/setup.py +++ b/sdks/python/setup.py @@ -31,9 +31,7 @@ # pylint: disable=ungrouped-imports import setuptools -from pkg_resources import normalize_path -from pkg_resources import parse_version -from pkg_resources import to_filename +from packaging.version import parse from setuptools import Command # pylint: disable=wrong-import-order @@ -43,6 +41,14 @@ from distutils.errors import DistutilsError # isort:skip +def to_filename(name: str) -> str: + return name.replace('-', '_') + + +def normalize_path(filename): + return os.path.normcase(os.path.realpath(os.path.normpath(filename))) + + class mypy(Command): user_options = [] @@ -101,7 +107,7 @@ def get_version(): RECOMMENDED_MIN_PIP_VERSION = '19.3.0' try: _PIP_VERSION = distribution('pip').version - if parse_version(_PIP_VERSION) < parse_version(RECOMMENDED_MIN_PIP_VERSION): + if parse(_PIP_VERSION) < parse(RECOMMENDED_MIN_PIP_VERSION): warnings.warn( "You are using version {0} of pip. " \ "However, the recommended min version is {1}.".format( @@ -116,7 +122,7 @@ def get_version(): REQUIRED_CYTHON_VERSION = '3.0.0' try: _CYTHON_VERSION = distribution('cython').version - if parse_version(_CYTHON_VERSION) < parse_version(REQUIRED_CYTHON_VERSION): + if parse(_CYTHON_VERSION) < parse(REQUIRED_CYTHON_VERSION): warnings.warn( "You are using version {0} of cython. " \ "However, version {1} is recommended.".format( From 4af055ce1accfe74f2c0882e1ee8f7b047bbbcfc Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Wed, 4 Jun 2025 15:12:50 -0400 Subject: [PATCH 2/5] use stdlib importlib.resources --- sdks/python/gen_protos.py | 4 ++-- sdks/python/pyproject.toml | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sdks/python/gen_protos.py b/sdks/python/gen_protos.py index 311e3a1841e1..ed9904f02e99 100644 --- a/sdks/python/gen_protos.py +++ b/sdks/python/gen_protos.py @@ -31,7 +31,7 @@ from collections import defaultdict from importlib import import_module -import importlib_resources +import importlib.resources LOG = logging.getLogger() LOG.setLevel(logging.INFO) @@ -474,7 +474,7 @@ def generate_proto_files(force=False): protoc_gen_mypy = _find_protoc_gen_mypy() from grpc_tools import protoc - builtin_protos = importlib_resources.files('grpc_tools') / '_proto' + builtin_protos = importlib.resources.files('grpc_tools') / '_proto' args = ( [sys.executable] + # expecting to be called from command line ['--proto_path=%s' % builtin_protos] + diff --git a/sdks/python/pyproject.toml b/sdks/python/pyproject.toml index 337b7a80b287..36976d92abb4 100644 --- a/sdks/python/pyproject.toml +++ b/sdks/python/pyproject.toml @@ -36,7 +36,6 @@ requires = [ # also update Jinja2 bounds in test-suites/xlang/build.gradle (look for xlangWrapperValidation task) "jinja2>=2.7.1,<4.0.0", 'yapf==0.43.0', - 'importlib-resources==6.5.2' ] From ae443889e7c6092f651cbe0f88361f8e64be5373 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Wed, 4 Jun 2025 15:18:01 -0400 Subject: [PATCH 3/5] remove extra comma --- sdks/python/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdks/python/pyproject.toml b/sdks/python/pyproject.toml index 36976d92abb4..97a9fe6141ea 100644 --- a/sdks/python/pyproject.toml +++ b/sdks/python/pyproject.toml @@ -35,7 +35,7 @@ requires = [ 'pyyaml>=3.12,<7.0.0', # also update Jinja2 bounds in test-suites/xlang/build.gradle (look for xlangWrapperValidation task) "jinja2>=2.7.1,<4.0.0", - 'yapf==0.43.0', + 'yapf==0.43.0' ] From 6c3116818b8c040dcb1a075c8ca5900a1c84b134 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Wed, 4 Jun 2025 15:52:55 -0400 Subject: [PATCH 4/5] linting --- sdks/python/setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdks/python/setup.py b/sdks/python/setup.py index 11be4c4e6a1f..a98eaab33361 100644 --- a/sdks/python/setup.py +++ b/sdks/python/setup.py @@ -46,7 +46,7 @@ def to_filename(name: str) -> str: def normalize_path(filename): - return os.path.normcase(os.path.realpath(os.path.normpath(filename))) + return os.path.normcase(os.path.realpath(os.path.normpath(filename))) class mypy(Command): @@ -361,7 +361,7 @@ def get_portability_package_data(): 'fasteners>=0.3,<1.0', # TODO(https://github.com/grpc/grpc/issues/37710): Unpin grpc 'grpcio>=1.33.1,<2,!=1.48.0,!=1.59.*,!=1.60.*,!=1.61.*,!=1.62.0,!=1.62.1,<1.66.0; python_version <= "3.12"', # pylint: disable=line-too-long - 'grpcio>=1.67.0; python_version >= "3.13"', + 'grpcio>=1.67.0; python_version >= "3.13"', 'hdfs>=2.1.0,<3.0.0', 'httplib2>=0.8,<0.23.0', 'jsonschema>=4.0.0,<5.0.0', From 7d1680ad25c4bfb004664af9c021386273181c4f Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Wed, 4 Jun 2025 16:15:48 -0400 Subject: [PATCH 5/5] import order --- sdks/python/gen_protos.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdks/python/gen_protos.py b/sdks/python/gen_protos.py index ed9904f02e99..d07f046fd229 100644 --- a/sdks/python/gen_protos.py +++ b/sdks/python/gen_protos.py @@ -21,6 +21,7 @@ import argparse import contextlib import glob +import importlib.resources import inspect import logging import os @@ -31,8 +32,6 @@ from collections import defaultdict from importlib import import_module -import importlib.resources - LOG = logging.getLogger() LOG.setLevel(logging.INFO)