Skip to content

Commit

Permalink
Use the stdlib importlib.metadata when available
Browse files Browse the repository at this point in the history
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 pypa#773
  • Loading branch information
eli-schwartz committed Oct 26, 2023
1 parent 96674db commit bbd46bd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ install_requires=
requests >= 2.20
requests-toolbelt >= 0.8.0, != 0.9.0
urllib3 >= 1.26.0
importlib-metadata >= 3.6
importlib-metadata >= 3.6; python_version<"3.10"
keyring >= 15.1
rfc3986 >= 1.4.0
rich >= 12.0.0
Expand Down
7 changes: 6 additions & 1 deletion twine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@

__copyright__ = "Copyright 2019 Donald Stufft and individual contributors"

import importlib_metadata
import sys

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 @@ -17,9 +17,14 @@
import os
import re
import subprocess
import sys
from typing import Dict, 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 bbd46bd

Please sign in to comment.