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

add .get() to the PackageMetadata protocol #444

Merged
merged 2 commits into from Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions importlib_metadata/_meta.py
Expand Up @@ -18,6 +18,14 @@ def __getitem__(self, key: str) -> str:
def __iter__(self) -> Iterator[str]:
... # pragma: no cover

@overload
def get(self, name: str, failobj: None = None) -> Optional[str]:
... # pragma: no cover

@overload
def get(self, name: str, failobj: _T) -> Union[str, _T]:
... # pragma: no cover

# overload per python/importlib_metadata#435
@overload
def get_all(self, name: str, failobj: None = None) -> Optional[List[Any]]:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_api.py
Expand Up @@ -148,6 +148,20 @@ def test_missing_key_legacy(self):
with suppress_known_deprecation():
assert md['does-not-exist'] is None

def test_get_key(self):
"""
Getting a key gets the key.
"""
md = metadata('egginfo-pkg')
assert md.get('Name') == 'egginfo-pkg'

def test_get_missing_key(self):
"""
Requesting a missing key will return None.
"""
md = metadata('distinfo-pkg')
assert md.get('does-not-exist') is None

@staticmethod
def _test_files(files):
root = files[0].root
Expand Down