Skip to content

Commit 5a65705

Browse files
committed
Refactor the casting into a wrapper for brevity and to document its purpose.
1 parent 0830c39 commit 5a65705

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

‎importlib_metadata/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from importlib import import_module
2828
from importlib.abc import MetaPathFinder
2929
from itertools import starmap
30-
from typing import Any, cast
30+
from typing import Any
3131

3232
from . import _meta
3333
from ._collections import FreezableDefaultDict, Pair
@@ -38,6 +38,7 @@
3838
from ._functools import method_cache, pass_none
3939
from ._itertools import always_iterable, bucket, unique_everseen
4040
from ._meta import PackageMetadata, SimplePath
41+
from ._typing import md_none
4142
from .compat import py39, py311
4243

4344
__all__ = [
@@ -543,7 +544,7 @@ def _assemble_message(text: str) -> _meta.PackageMetadata:
543544
@property
544545
def name(self) -> str:
545546
"""Return the 'Name' metadata for the distribution package."""
546-
return cast(PackageMetadata, self.metadata)['Name']
547+
return md_none(self.metadata)['Name']
547548

548549
@property
549550
def _normalized_name(self):
@@ -553,7 +554,7 @@ def _normalized_name(self):
553554
@property
554555
def version(self) -> str:
555556
"""Return the 'Version' metadata for the distribution package."""
556-
return cast(PackageMetadata, self.metadata)['Version']
557+
return md_none(self.metadata)['Version']
557558

558559
@property
559560
def entry_points(self) -> EntryPoints:
@@ -1125,7 +1126,7 @@ def packages_distributions() -> Mapping[str, list[str]]:
11251126
pkg_to_dist = collections.defaultdict(list)
11261127
for dist in distributions():
11271128
for pkg in _top_level_declared(dist) or _top_level_inferred(dist):
1128-
pkg_to_dist[pkg].append(cast(PackageMetadata, dist.metadata)['Name'])
1129+
pkg_to_dist[pkg].append(md_none(dist.metadata)['Name'])
11291130
return dict(pkg_to_dist)
11301131

11311132

‎importlib_metadata/_typing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import functools
2+
import typing
3+
4+
from ._meta import PackageMetadata
5+
6+
md_none = functools.partial(typing.cast, PackageMetadata)
7+
"""
8+
Suppress type errors for optional metadata.
9+
10+
Although Distribution.metadata can return None when metadata is corrupt
11+
and thus None, allow callers to assume it's not None and crash if
12+
that's the case.
13+
14+
# python/importlib_metadata#493
15+
"""

‎importlib_metadata/compat/py39.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
from __future__ import annotations
66

7-
from typing import TYPE_CHECKING, Any, cast
7+
from typing import TYPE_CHECKING, Any
88

99
if TYPE_CHECKING: # pragma: no cover
1010
# Prevent circular imports on runtime.
1111
from .. import Distribution, EntryPoint
1212
else:
1313
Distribution = EntryPoint = Any
1414

15-
from .._meta import PackageMetadata
15+
from .._typing import md_none
1616

1717

1818
def normalized_name(dist: Distribution) -> str | None:
@@ -25,7 +25,7 @@ def normalized_name(dist: Distribution) -> str | None:
2525
from .. import Prepared # -> delay to prevent circular imports.
2626

2727
return Prepared.normalize(
28-
getattr(dist, "name", None) or cast(PackageMetadata, dist.metadata)['Name']
28+
getattr(dist, "name", None) or md_none(dist.metadata)['Name']
2929
)
3030

3131

0 commit comments

Comments
 (0)