pip icon indicating copy to clipboard operation
pip copied to clipboard

Supply Distribution.locate_file, honoring the abstract method.

Open jaraco opened this issue 2 years ago • 6 comments

Fixes #11684.

jaraco avatar Jan 01 '23 16:01 jaraco

I supply this proposed change as an illustration of what might address the issue. I'd prefer if someone from pip would contribute the rest of it (or replace it with their own).

jaraco avatar Jan 01 '23 16:01 jaraco

How would this function make sense for a zip file? The documentation is sparse on what exactly is expected, and we obviously can’t return a pathlib.Path.

uranusjr avatar Jan 03 '23 00:01 uranusjr

We still need some indication as to how we're supposed to implement the required method. From a brief look at the documentation, it seems that it would be quite a lot of work given that we clearly don't call anything that doesn't use the newly-required method...

Simply raising an error on the missing methods is a possibility, I guess, and that's what this PR does, but

  1. This comment suggests it's not intended as the correct solution, and
  2. It feels like we'd be subverting the intention of the change to make the methods required.

Advice on how we should proceed, and ideally a PR that you do consider complete and correct, would be appreciated.

pfmoore avatar Apr 25 '24 20:04 pfmoore

  1. This comment suggests it's not intended as the correct solution, and

I'm unsure if it's the correct solution or not. I'm unaware of the internals of pip so am unsure why these Distribution objects exist or what users expect from them.

I'll explore in the bug.

jaraco avatar Jul 25 '24 20:07 jaraco

I'm unaware of the internals of pip so am unsure why these Distribution objects exist or what users expect from them.

But isn't the point here that the stdlib is enforcing the implementation of the abstract methods? So what pip, or pip's users, expect from the objects is irrelevant - the stdlib is now requiring that we implement the method(s), so we have no choice. What we're asking is what constitutes (in the stdlib's eyes) a valid implementation. Ideally, that should be in the stdlib documentation, but to be blunt, the documentation is fairly unhelpful - all it says is "Given a path to a file in this distribution, return a SimplePath to it." That suggests that we have to implement some form of SimplePath subclass, in spite of the fact that currently we demonstrably execute no code paths that care about, or even call, locate_file.

If it's acceptable to simply raise NotImplementedError from the method, then I'd say that should be documented in the API documentation, along with a description of the implications of doing so (what importlib.metadata functions will fail if that method raises an error, for example). With that information, we could reasonably judge whether it's OK for pip to do that. And as I say, I'm pretty certain we'd say that it is OK, simply because it's worked until now - although this change in CPython is evidence that we'd have been wrong in that assumption, hence my concern about simply making the same assumption again.

To put this briefly: CPython changed the rules, I'd like them[^1] to tell us what the new rules are, so we don't have to just guess again...

[^1]: More precisely, you, as the CPython core dev expert on importlib.metadata 🙂

pfmoore avatar Jul 25 '24 20:07 pfmoore

CPython changed the rules

As the implementer, I can state with authority that it was always the intention that these methods were abstract and were expected by a subclass to implement them. It was probably an artifact of the Python 2.7 compatibility that the class itself wasn't an ABCMeta so lacked the enforcement in the API. Still, the method was always declared as an abstractmethod (IIRC). That said, you're right that the implementation is shifting to now honor the original intention that was missed and closing the gaps that have emerged since.

jaraco avatar Jul 25 '24 22:07 jaraco