Skip to content

Patch the find method from pkg_resources.WorkingSet.#684

Merged
mauritsvanrees merged 3 commits intomasterfrom
maurits-patch-pkg-resources-working-set-find
Mar 5, 2025
Merged

Patch the find method from pkg_resources.WorkingSet.#684
mauritsvanrees merged 3 commits intomasterfrom
maurits-patch-pkg-resources-working-set-find

Conversation

@mauritsvanrees
Copy link
Copy Markdown
Member

Let this use the code from setuptools 75.8.2, if the currently used version is older. This is better at finding installed distributions.

Fixes #683

@icemac
Copy link
Copy Markdown
Contributor

icemac commented Mar 5, 2025

Hm, 49.0.0 and 59.8.0 seem to be too old for this approach – time to drop support for them?

@mauritsvanrees
Copy link
Copy Markdown
Member Author

Hm, 49.0.0 and 59.8.0 seem to be too old for this approach – time to drop support for them?

I have pushed a commit that fixes the AttributeError. When setuptools is older than 61, do not apply the patch. Of course then on older versions you may run into problems exactly because the patch is not there.

For me it would be fine to drop support for setuptools versions older than 61. But if my last commit works, it may be okay to keep them. I have updated the changelog so people may see this.

Let this use the code from `setuptools` 75.8.2, if the currently used version is older.
This is better at finding installed distributions.

Fixes #683
The new version of the method would give an error there:

```
Traceback (most recent call last):
  File "/home/runner/work/buildout/buildout/dev.py", line 27, in <module>
    import zc.buildout.easy_install
  File "/home/runner/work/buildout/buildout/src/zc/buildout/easy_install.py", line 1007, in <module>
    buildout_and_setuptools_dists = list(install(['zc.buildout'], None,
  File "/home/runner/work/buildout/buildout/src/zc/buildout/easy_install.py", line 1005, in install
    return installer.install(specs, working_set)
  File "/home/runner/work/buildout/buildout/src/zc/buildout/easy_install.py", line 758, in install
    dist = env.best_match(req, ws)
  File "/home/runner/work/buildout/buildout/venvs/python3.10/lib/python3.10/site-packages/pkg_resources/__init__.py", line 1045, in best_match
    dist = working_set.find(req)
  File "/home/runner/work/buildout/buildout/src/zc/buildout/patches.py", line 355, in find
    self.normalized_to_canonical_keys.get(req.key),
AttributeError: 'WorkingSet' object has no attribute 'normalized_to_canonical_keys'
```

See for example this test run with `setuptools` 59.8.0:

https://github.com/buildout/buildout/actions/runs/13680434937/job/38251141064?pr=684#step:5:207
They fail on Python 3.9.  Which is strange, because the latest setuptools version supports Python 3.9, so why does it not fail there?

See [this test run](https://github.com/buildout/buildout/actions/runs/13680729029/job/38252752156?pr=684#step:5:210):

```
Now calling 'python dev.py' to create 'bin/buildout' script in main directory.
Traceback (most recent call last):
  File "/home/runner/work/buildout/buildout/dev.py", line 27, in <module>
    import zc.buildout.easy_install
  File "/home/runner/work/buildout/buildout/src/zc/buildout/__init__.py", line 38, in <module>
    import zc.buildout.patches  # NOQA
  File "/home/runner/work/buildout/buildout/src/zc/buildout/patches.py", line 386, in <module>
    patch_pkg_resources_working_set_find()
  File "/home/runner/work/buildout/buildout/src/zc/buildout/patches.py", line 354, in patch_pkg_resources_working_set_find
    def find(self, req: Requirement) -> Distribution | None:
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
```
@mauritsvanrees mauritsvanrees force-pushed the maurits-patch-pkg-resources-working-set-find branch from c9e97d0 to 1bf8b3d Compare March 5, 2025 16:51
@mauritsvanrees
Copy link
Copy Markdown
Member Author

The tests failed on Python 3.9 due to a problem with type hints. So I have removed the type hints. No sense in spending time on this.

I have rebased on master.

@mauritsvanrees
Copy link
Copy Markdown
Member Author

It's green! Please review.

Copy link
Copy Markdown
Contributor

@dataflake dataflake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much knowledge about setuptools internals, but if this works and the tests are green...

@mauritsvanrees mauritsvanrees merged commit 3b353ac into master Mar 5, 2025
@mauritsvanrees mauritsvanrees deleted the maurits-patch-pkg-resources-working-set-find branch March 5, 2025 18:07
@mauritsvanrees
Copy link
Copy Markdown
Member Author

I have released this in 4.1.3.

@dataflake
Copy link
Copy Markdown
Contributor

Hallelujah it works...

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.

Problems loading entry points

3 participants