From 73c138548afd1ac37d9f80c7fecf458bf133a5aa Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 7 Apr 2023 14:07:38 +0100 Subject: [PATCH 1/2] add .get() to the PackageMetadata protocol --- importlib_metadata/_meta.py | 8 ++++++++ tests/test_api.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/importlib_metadata/_meta.py b/importlib_metadata/_meta.py index 8621c680..e27d34aa 100644 --- a/importlib_metadata/_meta.py +++ b/importlib_metadata/_meta.py @@ -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]]: diff --git a/tests/test_api.py b/tests/test_api.py index 2932c2d2..6dbce1fc 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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 From 15fffb8811e21bd92101f2fa47daaf50ee48c8dd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 7 Apr 2023 12:43:55 -0400 Subject: [PATCH 2/2] Update changelog. --- CHANGES.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index eccdd5ba..3583a211 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,10 @@ +v6.2.0 +====== + +* #384: ``PackageMetadata`` now stipulates an additional ``get`` + method allowing for easy querying of metadata keys that may not + be present. + v6.1.0 ======