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

Replace the remaining uses of pkg_resources with packaging and importlib_metadata. #736

Merged
merged 19 commits into from
Mar 2, 2021
Merged
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
1 change: 1 addition & 0 deletions changelog/736.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminated dependency on Setuptools/pkg_resources and replaced with packaging and importlib_metadata.
7 changes: 3 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ strict_equality = True
; https://github.com/tartley/colorama/issues/206
ignore_missing_imports = True

[mypy-packaging]
ignore_missing_imports = True

[mypy-pkginfo]
; https://bugs.launchpad.net/pkginfo/+bug/1876591
ignore_missing_imports = True
Expand All @@ -37,10 +40,6 @@ ignore_missing_imports = True
[mypy-rfc3986]
ignore_missing_imports = True

[mypy-setuptools]
; https://github.com/python/typeshed/issues/2171
ignore_missing_imports = True

[mypy-tqdm]
; https://github.com/tqdm/tqdm/issues/260
ignore_missing_imports = True
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install_requires=
readme_renderer >= 21.0
requests >= 2.20
requests-toolbelt >= 0.8.0, != 0.9.0
setuptools >= 0.7.0
packaging >= 16.2
tqdm >= 4.14
importlib_metadata >= 3.6
keyring >= 15.1
Expand Down
Binary file added tests/fixtures/twine-3.3.0-py3.9.egg
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,8 @@ def test_malformed_from_file(monkeypatch):
package_file.PackageFile.from_filename(filename, comment=None)

assert "Invalid distribution file" in err.value.args[0]


def test_package_from_egg():
filename = "tests/fixtures/twine-3.3.0-py3.9.egg"
package_file.PackageFile.from_filename(filename, comment=None)
9 changes: 8 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ def test_make_user_agent_string(default_repo):
assert "User-Agent" in default_repo.session.headers

user_agent = default_repo.session.headers["User-Agent"]
packages = ("twine/", "requests/", "requests-toolbelt/", "pkginfo/", "setuptools/")
packages = (
"twine/",
"requests/",
"requests-toolbelt/",
"pkginfo/",
"importlib_metadata/",
"packaging/",
)
assert all(p in user_agent for p in packages)


Expand Down
60 changes: 0 additions & 60 deletions twine/_installed.py

This file was deleted.

23 changes: 10 additions & 13 deletions twine/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@
import argparse
from typing import Any, List, Tuple

import pkginfo
import requests
import requests_toolbelt
import setuptools
import tqdm
from importlib_metadata import entry_points
from importlib_metadata import version

import twine
from twine import _installed

args = argparse.Namespace()


def list_dependencies_and_versions() -> List[Tuple[str, str]]:
return [
("pkginfo", _installed.Installed(pkginfo).version),
("requests", requests.__version__),
("setuptools", setuptools.__version__),
jaraco marked this conversation as resolved.
Show resolved Hide resolved
("requests-toolbelt", requests_toolbelt.__version__),
("tqdm", tqdm.__version__),
]
deps = (
"importlib_metadata",
"packaging",
"pkginfo",
"requests",
"requests-toolbelt",
"tqdm",
)
return [(dep, version(dep)) for dep in deps] # type: ignore[no-untyped-call] # python/importlib_metadata#288 # noqa: E501
jaraco marked this conversation as resolved.
Show resolved Hide resolved


def dep_versions() -> str:
Expand Down
11 changes: 7 additions & 4 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import subprocess
from typing import Dict, NamedTuple, Optional, Sequence, Tuple, Union

import pkg_resources
import importlib_metadata
import packaging.utils
import pkginfo

from twine import exceptions
Expand Down Expand Up @@ -58,7 +59,7 @@ def __init__(
self.metadata = metadata
self.python_version = python_version
self.filetype = filetype
self.safe_name = pkg_resources.safe_name(metadata.name)
self.safe_name = packaging.utils.canonicalize_name(metadata.name)
self.signed_filename = self.filename + ".asc"
self.signed_basefilename = self.basefilename + ".asc"
self.gpg_signature: Optional[Tuple[str, bytes]] = None
Expand Down Expand Up @@ -99,8 +100,10 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":

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