Skip to content

Replace deprecated pkg_resources with packaging in setup.py#1338

Open
jasonwbarnett wants to merge 1 commit into
MagicStack:masterfrom
altana-ai:fix/replace-pkg-resources-with-packaging
Open

Replace deprecated pkg_resources with packaging in setup.py#1338
jasonwbarnett wants to merge 1 commit into
MagicStack:masterfrom
altana-ai:fix/replace-pkg-resources-with-packaging

Conversation

@jasonwbarnett

Copy link
Copy Markdown

Summary

setup.py uses the deprecated pkg_resources module (from setuptools) to validate the installed Cython version when building from source. pkg_resources emits DeprecationWarning: pkg_resources is deprecated as an API on import, is slated for removal, and is unavailable in some modern setuptools configurations — which can make source builds warn or fail.

This ports the Cython version check to the packaging library:

# before
import pkg_resources
cython_dep = pkg_resources.Requirement.parse(CYTHON_DEPENDENCY)
if Cython.__version__ not in cython_dep:

# after
from packaging.requirements import Requirement
cython_dep = Requirement(CYTHON_DEPENDENCY)
if not cython_dep.specifier.contains(Cython.__version__, prereleases=True):
  • Adds packaging>=20 to [build-system].requires so it is available at build time.
  • pkg_resources.Requirement.__contains__ used prereleases=True internally, so passing prereleases=True preserves the original behavior exactly (including accepting in-range prereleases such as 3.3.0b1).

This mirrors the same fix already made in the sister project uvloop (MagicStack/uvloop@b377b7c).

Relationship to #1314

#1314 addresses the same deprecation by removing the Cython version check entirely. This PR is an alternative that keeps the check while dropping the deprecated dependency. Happy to defer to whichever direction you prefer.

Verification

  • python -m py_compile setup.py passes; no pkg_resources references remain in the tree.
  • Confirmed packaging.requirements.Requirement("Cython(>=3.2.1,<4.0.0)") parses the parenthesized PEP 508 form, and that .specifier.contains(v, prereleases=True) matches the old pkg_resources behavior across in-range, out-of-range, and prerelease versions.

Closes #1337

setup.py used pkg_resources.Requirement to validate the installed Cython
version at build time. pkg_resources is deprecated by setuptools and slated
for removal: it emits a DeprecationWarning on import and is unavailable in
some modern setuptools configurations, which can make source builds warn or
fail.

Port the check to the packaging library:
  - pkg_resources.Requirement.parse(CYTHON_DEPENDENCY)
      -> packaging.requirements.Requirement(CYTHON_DEPENDENCY)
  - Cython.__version__ not in cython_dep
      -> not cython_dep.specifier.contains(Cython.__version__, prereleases=True)

pkg_resources.Requirement.__contains__ passed prereleases=True internally, so
passing prereleases=True preserves the original behavior exactly. Add
packaging>=20 to build-system.requires so it is available when building from
source.

Mirrors the same fix in the sister project MagicStack/uvloop@b377b7c.

Refs: MagicStack#1337

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ScLtrCvUAVqPrVbcqsGUDJ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setup.py relies on deprecated pkg_resources

1 participant