Skip to content

Commit

Permalink
Use the stdlib importlib.metadata when available (#1024)
Browse files Browse the repository at this point in the history
* Use the stdlib importlib.metadata when available

The use of importlib_metadata relies on features that were introduced in
python 3.10. Aside for that, it should be fine to use the stdlib
directly.

Fixes #773

* Add news fragment.

* Import the full email.utils module as used.

---------

Co-authored-by: Jason R. Coombs <[email protected]>
  • Loading branch information
eli-schwartz and jaraco authored Jun 26, 2024
1 parent e29791d commit 5bf3f38
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/1024.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only require ``importlib_metadata`` on older Pythons.
8 changes: 6 additions & 2 deletions twine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@

__copyright__ = "Copyright 2019 Donald Stufft and individual contributors"

import email
import email.utils
import sys

import importlib_metadata
if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

metadata = importlib_metadata.metadata("twine")

Expand Down
14 changes: 10 additions & 4 deletions twine/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
# limitations under the License.
import argparse
import logging.config
import sys
from typing import Any, List, Tuple

import importlib_metadata
if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

import rich
import rich.highlighter
import rich.logging
Expand Down Expand Up @@ -70,14 +75,15 @@ def configure_output() -> None:


def list_dependencies_and_versions() -> List[Tuple[str, str]]:
deps = (
"importlib-metadata",
deps = [
"keyring",
"pkginfo",
"requests",
"requests-toolbelt",
"urllib3",
)
]
if sys.version_info < (3, 10):
deps.append("importlib-metadata")
return [(dep, importlib_metadata.version(dep)) for dep in deps]


Expand Down
7 changes: 6 additions & 1 deletion twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
import os
import re
import subprocess
import sys
from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union, cast

import importlib_metadata
if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

import pkginfo
from rich import print

Expand Down

0 comments on commit 5bf3f38

Please sign in to comment.