Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark interfaces as Protocols and add missing self argument #4144

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4144.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Explicitely marked as ``Protocol`` and fixed missing ``self`` argument in interfaces ``pkg_resources.IMetadataProvider`` and ``pkg_resources.IResourceProvider`` -- by :user:`Avasam`
29 changes: 15 additions & 14 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import time
import re
import types
from typing import Protocol
import zipfile
import zipimport
import warnings
Expand Down Expand Up @@ -544,54 +545,54 @@ def get_entry_info(dist, group, name):
return get_distribution(dist).get_entry_info(group, name)


class IMetadataProvider:
def has_metadata(name):
class IMetadataProvider(Protocol):
Copy link
Member

Choose a reason for hiding this comment

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

I have no idea if these protocols are used anywhere (such as with Zope), but I suspect not. If this change ends up causing unexpected disruption, we may have to back it out. Thanks for making it a separate PR.

def has_metadata(self, name):
"""Does the package's distribution contain the named metadata?"""

def get_metadata(name):
def get_metadata(self, name):
"""The named metadata resource as a string"""

def get_metadata_lines(name):
def get_metadata_lines(self, name):
"""Yield named metadata resource as list of non-blank non-comment lines

Leading and trailing whitespace is stripped from each line, and lines
with ``#`` as the first non-blank character are omitted."""

def metadata_isdir(name):
def metadata_isdir(self, name):
"""Is the named metadata a directory? (like ``os.path.isdir()``)"""

def metadata_listdir(name):
def metadata_listdir(self, name):
"""List of metadata names in the directory (like ``os.listdir()``)"""

def run_script(script_name, namespace):
def run_script(self, script_name, namespace):
"""Execute the named script in the supplied namespace dictionary"""


class IResourceProvider(IMetadataProvider):
class IResourceProvider(IMetadataProvider, Protocol):
Copy link
Member

Choose a reason for hiding this comment

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

Today I learned about extending protocols. Nice work.

"""An object that provides access to package resources"""

def get_resource_filename(manager, resource_name):
def get_resource_filename(self, manager, resource_name):
"""Return a true filesystem path for `resource_name`

`manager` must be an ``IResourceManager``"""

def get_resource_stream(manager, resource_name):
def get_resource_stream(self, manager, resource_name):
"""Return a readable file-like object for `resource_name`

`manager` must be an ``IResourceManager``"""

def get_resource_string(manager, resource_name):
def get_resource_string(self, manager, resource_name):
"""Return a string containing the contents of `resource_name`

`manager` must be an ``IResourceManager``"""

def has_resource(resource_name):
def has_resource(self, resource_name):
"""Does the package contain the named resource?"""

def resource_isdir(resource_name):
def resource_isdir(self, resource_name):
"""Is the named resource a directory? (like ``os.path.isdir()``)"""

def resource_listdir(resource_name):
def resource_listdir(self, resource_name):
"""List of resource names in the directory (like ``os.listdir()``)"""


Expand Down
Loading