Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use external importlib_metadata package when python < 3.8 #751

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ install_requires=
requests >= 2.20
requests-toolbelt >= 0.8.0, != 0.9.0
tqdm >= 4.14
importlib_metadata >= 3.6
importlib_metadata >= 3.6; python_version < "3.8"
keyring >= 15.1
rfc3986 >= 1.4.0
colorama >= 0.4.3
Expand Down
5 changes: 4 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import sys
from contextlib import contextmanager

import pretend
Expand Down Expand Up @@ -94,8 +95,10 @@ def test_make_user_agent_string(default_repo):
"requests/",
"requests-toolbelt/",
"pkginfo/",
"importlib_metadata/",
)
if sys.version_info < (3, 8):
packages += ("importlib_metadata/",)

assert all(p in user_agent for p in packages)


Expand Down
11 changes: 9 additions & 2 deletions twine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@

__copyright__ = "Copyright 2019 Donald Stufft and individual contributors"

import importlib_metadata
import sys

metadata = importlib_metadata.metadata("twine")
if sys.version_info < (3, 8):
import importlib_metadata

metadata = importlib_metadata.metadata("twine")
else:
from importlib.metadata import metadata

metadata = metadata("twine")


__title__ = metadata["name"]
Expand Down
13 changes: 10 additions & 3 deletions twine/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import sys
from typing import Any, List, Tuple

from importlib_metadata import entry_points
from importlib_metadata import version
if sys.version_info < (3, 8):
from importlib_metadata import entry_points
from importlib_metadata import version
else:
from importlib.metadata import entry_points, version

import twine

Expand All @@ -24,12 +28,15 @@

def list_dependencies_and_versions() -> List[Tuple[str, str]]:
deps = (
"importlib_metadata",
"pkginfo",
"requests",
"requests-toolbelt",
"tqdm",
)

if sys.version_info < (3, 8):
deps += ("importlib_metadata",)

return [(dep, version(dep)) for dep in deps] # type: ignore[no-untyped-call] # python/importlib_metadata#288 # noqa: E501


Expand Down
9 changes: 7 additions & 2 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
import os
import re
import subprocess
import sys
from typing import Dict, NamedTuple, Optional, Sequence, Tuple, Union

import importlib_metadata
if sys.version_info < (3, 8):
from importlib_metadata import Distribution
else:
from importlib.metadata import Distribution

import pkginfo

from twine import exceptions
Expand Down Expand Up @@ -111,7 +116,7 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":

py_version: Optional[str]
if dtype == "bdist_egg":
(dist,) = importlib_metadata.Distribution.discover( # type: ignore[no-untyped-call] # python/importlib_metadata#288 # noqa: E501
(dist,) = Distribution.discover( # type: ignore[no-untyped-call] # python/importlib_metadata#288 # noqa: E501
path=[filename]
)
py_version = dist.metadata["Version"]
Expand Down