Skip to content

Replace deprecated pkg_resources with packaging in setup.py - #1

Closed
jasonwbarnett wants to merge 1 commit into
masterfrom
fix/replace-pkg-resources-with-packaging
Closed

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

Conversation

@jasonwbarnett

Copy link
Copy Markdown

What & why

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

Change

Port 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 in pyproject.toml 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 in the sister project MagicStack/uvloop@b377b7c.

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.

Related

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
@jasonwbarnett

Copy link
Copy Markdown
Author

Superseded by the upstream PR MagicStack#1338 — this fix belongs against upstream rather than the company fork. Closing here.

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.

1 participant