importlib_metadata icon indicating copy to clipboard operation
importlib_metadata copied to clipboard

Add a `Distribution.editable` property

Open FFY00 opened this issue 11 months ago • 1 comments

Recently, when trying to check if a distribution is editable, I noticed doing so using the current API is very un-ergonomic. This information is available via the direct_url.json file of PEP 610, which is exposed in Distribution.origin.

When url refers to a local directory, the dir_info key MUST be present as a dictionary with the following key:

editable (type: boolean): true if the distribution was installed in editable mode, false otherwise. If absent, default to false.

So, to check if the distribution is editable, we can check Distribution.origin.dir_info.editable, however, Distribution.origin might be None (no direct_url.json), and Distribution.origin.dir_info might not be set (url in direct_url.json in not a local directory), which.

is_editable = distribution.origin and distribution.origin.dir_info and distribution.origin.dir_info.editable
try:
    is_editable = distribution.origin.dir_info.editable
except AttributeError:
    is_editable = False

It would be helpful to have this information available via a more ergonomic API.

FFY00 avatar Jan 26 '25 01:01 FFY00

If only Python had a feature like C# or Swift to conditionally bypass null properties.

is_editable = bool(distribution.origin.?dir_info.?editable)

Since it doesn't, this library needs to provide special functions to provide these ergonomics.

I'm not sure where to supply this interface. There are at least a couple of potential options:

def editable(distribution_name: str) -> bool:
  ...
class Distribution:
  @property
  def editable(self) -> bool:
    ...

Or maybe both (similar to metadata and version and more).

I'm thinking both (where editable merely returns distribution(distribution_name).editable).

jaraco avatar Apr 27 '25 12:04 jaraco