-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract localize functions into their own module.
- Loading branch information
Showing
3 changed files
with
50 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
import email.message | ||
import importlib.metadata | ||
import warnings | ||
from typing import cast | ||
|
||
import importlib_metadata._adapters | ||
|
||
|
||
def dist( | ||
dist: importlib_metadata.Distribution | importlib.metadata.Distribution, | ||
) -> importlib_metadata.Distribution: | ||
""" | ||
Ensure dist is an :class:`importlib_metadata.Distribution`. | ||
""" | ||
if isinstance(dist, importlib_metadata.Distribution): | ||
return dist | ||
if isinstance(dist, importlib.metadata.PathDistribution): | ||
return importlib_metadata.PathDistribution( | ||
cast(importlib_metadata._meta.SimplePath, dist._path) | ||
) | ||
# workaround for when pytest has replaced importlib_metadata | ||
# https://github.com/python/importlib_metadata/pull/505#issuecomment-2344329001 | ||
if dist.__class__.__module__ != 'importlib_metadata': | ||
warnings.warn(f"Unrecognized distribution subclass {dist.__class__}") | ||
return cast(importlib_metadata.Distribution, dist) | ||
|
||
|
||
def metadata( | ||
input: importlib_metadata._adapters.Message | email.message.Message, | ||
) -> importlib_metadata._adapters.Message: | ||
if isinstance(input, importlib_metadata._adapters.Message): | ||
return input | ||
return importlib_metadata._adapters.Message(input) | ||
|
||
|
||
def package_path( | ||
input: importlib_metadata.PackagePath | importlib.metadata.PackagePath, | ||
) -> importlib_metadata.PackagePath: | ||
if isinstance(input, importlib_metadata.PackagePath): | ||
return input | ||
replacement = importlib_metadata.PackagePath(input) | ||
vars(replacement).update(vars(input)) | ||
return replacement |