Skip to content

Commit 22bb567

Browse files
committed
Fix type errors where metadata could be None.
1 parent 57f31d7 commit 22bb567

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

importlib_metadata/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def _assemble_message(text: str) -> _meta.PackageMetadata:
543543
@property
544544
def name(self) -> str:
545545
"""Return the 'Name' metadata for the distribution package."""
546-
return self.metadata['Name']
546+
return cast(PackageMetadata, self.metadata)['Name']
547547

548548
@property
549549
def _normalized_name(self):
@@ -553,7 +553,7 @@ def _normalized_name(self):
553553
@property
554554
def version(self) -> str:
555555
"""Return the 'Version' metadata for the distribution package."""
556-
return self.metadata['Version']
556+
return cast(PackageMetadata, self.metadata)['Version']
557557

558558
@property
559559
def entry_points(self) -> EntryPoints:
@@ -1050,7 +1050,7 @@ def distributions(**kwargs) -> Iterable[Distribution]:
10501050
return Distribution.discover(**kwargs)
10511051

10521052

1053-
def metadata(distribution_name: str) -> _meta.PackageMetadata:
1053+
def metadata(distribution_name: str) -> _meta.PackageMetadata | None:
10541054
"""Get the metadata for the named package.
10551055
10561056
:param distribution_name: The name of the distribution package to query.
@@ -1125,7 +1125,7 @@ def packages_distributions() -> Mapping[str, list[str]]:
11251125
pkg_to_dist = collections.defaultdict(list)
11261126
for dist in distributions():
11271127
for pkg in _top_level_declared(dist) or _top_level_inferred(dist):
1128-
pkg_to_dist[pkg].append(dist.metadata['Name'])
1128+
pkg_to_dist[pkg].append(cast(PackageMetadata, dist.metadata)['Name'])
11291129
return dict(pkg_to_dist)
11301130

11311131

importlib_metadata/compat/py39.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
from __future__ import annotations
66

7-
from typing import TYPE_CHECKING, Any
7+
from typing import TYPE_CHECKING, Any, cast
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
16+
1517

1618
def normalized_name(dist: Distribution) -> str | None:
1719
"""
@@ -22,7 +24,9 @@ def normalized_name(dist: Distribution) -> str | None:
2224
except AttributeError:
2325
from .. import Prepared # -> delay to prevent circular imports.
2426

25-
return Prepared.normalize(getattr(dist, "name", None) or dist.metadata['Name'])
27+
return Prepared.normalize(
28+
getattr(dist, "name", None) or cast(PackageMetadata, dist.metadata)['Name']
29+
)
2630

2731

2832
def ep_matches(ep: EntryPoint, **params) -> bool:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy