From bd74b083ff1952c49b5fcb2b139195f3a6792200 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Tue, 13 Jul 2021 08:30:31 -0500 Subject: [PATCH 01/14] Complete the annotations: `pip/_internal/utils` Convert these type hint commentaries into proper annotations. --- src/pip/_internal/utils/appdirs.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/pip/_internal/utils/appdirs.py b/src/pip/_internal/utils/appdirs.py index db974dad635..a8403b7dee4 100644 --- a/src/pip/_internal/utils/appdirs.py +++ b/src/pip/_internal/utils/appdirs.py @@ -12,13 +12,11 @@ from pip._vendor import appdirs as _appdirs -def user_cache_dir(appname): - # type: (str) -> str +def user_cache_dir(appname: str) -> str: return _appdirs.user_cache_dir(appname, appauthor=False) -def user_config_dir(appname, roaming=True): - # type: (str, bool) -> str +def user_config_dir(appname: str, roaming: bool = True) -> str: path = _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming) if _appdirs.system == "darwin" and not os.path.isdir(path): path = os.path.expanduser("~/.config/") @@ -29,8 +27,7 @@ def user_config_dir(appname, roaming=True): # for the discussion regarding site_config_dir locations # see -def site_config_dirs(appname): - # type: (str) -> List[str] +def site_config_dirs(appname: str) -> List[str]: dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True) if _appdirs.system not in ["win32", "darwin"]: # always look in /etc directly as well From ad4f8be41e088cb12818eeb61c5a3e4989ec0531 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Tue, 13 Jul 2021 08:39:44 -0500 Subject: [PATCH 02/14] Add the NEWS fragment This is the news entry for my pull request. --- news/10159.trivial.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/10159.trivial.rst diff --git a/news/10159.trivial.rst b/news/10159.trivial.rst new file mode 100644 index 00000000000..f52e7baeb19 --- /dev/null +++ b/news/10159.trivial.rst @@ -0,0 +1 @@ +Complete the type annotations from ``pip/_internal/utils``. From c628bc2c1d301bfc10ffbd18ac84c3d1711a8379 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Tue, 13 Jul 2021 08:42:45 -0500 Subject: [PATCH 03/14] Complete type annotations: `pip/_internal/utils` Fix them on "compat.py". --- src/pip/_internal/utils/compat.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pip/_internal/utils/compat.py b/src/pip/_internal/utils/compat.py index 1fb2dc729e0..3f4d300cef0 100644 --- a/src/pip/_internal/utils/compat.py +++ b/src/pip/_internal/utils/compat.py @@ -11,8 +11,7 @@ logger = logging.getLogger(__name__) -def has_tls(): - # type: () -> bool +def has_tls() -> bool: try: import _ssl # noqa: F401 # ignore unused @@ -25,8 +24,7 @@ def has_tls(): return IS_PYOPENSSL -def get_path_uid(path): - # type: (str) -> int +def get_path_uid(path: str) -> int: """ Return path's uid. From da9943536f3b672b7c69183b36f1c8afb4a519fc Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Tue, 13 Jul 2021 08:50:47 -0500 Subject: [PATCH 04/14] Complete the annotations: `pip/_internal/utils` Fix them at "compatibility_tags.py". --- src/pip/_internal/utils/compatibility_tags.py | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/pip/_internal/utils/compatibility_tags.py b/src/pip/_internal/utils/compatibility_tags.py index 14fe51c1a51..f1c0f063370 100644 --- a/src/pip/_internal/utils/compatibility_tags.py +++ b/src/pip/_internal/utils/compatibility_tags.py @@ -21,14 +21,12 @@ _osx_arch_pat = re.compile(r"(.+)_(\d+)_(\d+)_(.+)") -def version_info_to_nodot(version_info): - # type: (Tuple[int, ...]) -> str +def version_info_to_nodot(version_info: Tuple[int, ...]) -> str: # Only use up to the first two numbers. return "".join(map(str, version_info[:2])) -def _mac_platforms(arch): - # type: (str) -> List[str] +def _mac_platforms(arch: str) -> List[str]: match = _osx_arch_pat.match(arch) if match: name, major, minor, actual_arch = match.groups() @@ -48,8 +46,7 @@ def _mac_platforms(arch): return arches -def _custom_manylinux_platforms(arch): - # type: (str) -> List[str] +def _custom_manylinux_platforms(arch: str) -> List[str]: arches = [arch] arch_prefix, arch_sep, arch_suffix = arch.partition("_") if arch_prefix == "manylinux2014": @@ -70,8 +67,7 @@ def _custom_manylinux_platforms(arch): return arches -def _get_custom_platforms(arch): - # type: (str) -> List[str] +def _get_custom_platforms(arch: str) -> List[str]: arch_prefix, arch_sep, arch_suffix = arch.partition("_") if arch.startswith("macosx"): arches = _mac_platforms(arch) @@ -82,8 +78,7 @@ def _get_custom_platforms(arch): return arches -def _expand_allowed_platforms(platforms): - # type: (Optional[List[str]]) -> Optional[List[str]] +def _expand_allowed_platforms(platforms: Optional[List[str]]) -> Optional[List[str]]: if not platforms: return None @@ -100,16 +95,16 @@ def _expand_allowed_platforms(platforms): return result -def _get_python_version(version): - # type: (str) -> PythonVersion +def _get_python_version(version: str) -> "PythonVersion": if len(version) > 1: return int(version[0]), int(version[1:]) else: return (int(version[0]),) -def _get_custom_interpreter(implementation=None, version=None): - # type: (Optional[str], Optional[str]) -> str +def _get_custom_interpreter( + implementation: Optional[str] = None, version: Optional[str] = None +) -> str: if implementation is None: implementation = interpreter_name() if version is None: @@ -118,12 +113,11 @@ def _get_custom_interpreter(implementation=None, version=None): def get_supported( - version=None, # type: Optional[str] - platforms=None, # type: Optional[List[str]] - impl=None, # type: Optional[str] - abis=None, # type: Optional[List[str]] -): - # type: (...) -> List[Tag] + version: Optional[str] = None, + platforms: Optional[List[str]] = None, + impl: Optional[str] = None, + abis: Optional[List[str]] = None, +) -> List[Tag]: """Return a list of supported tags for each version specified in `versions`. @@ -136,9 +130,9 @@ def get_supported( :param abis: specify a list of abis you want valid tags for, or None. If None, use the local interpreter abi. """ - supported = [] # type: List[Tag] + supported: List[Tag] = [] - python_version = None # type: Optional[PythonVersion] + python_version: Optional["PythonVersion"] = None if version is not None: python_version = _get_python_version(version) From 9a5abfef0ae18bea9b828503ae25fa12048154a5 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Tue, 13 Jul 2021 09:05:52 -0500 Subject: [PATCH 05/14] Complete the annotations: `pip/_internal/utils` Fix them at "datetime.py". --- src/pip/_internal/utils/datetime.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pip/_internal/utils/datetime.py b/src/pip/_internal/utils/datetime.py index b638646c8bb..8668b3b0ec1 100644 --- a/src/pip/_internal/utils/datetime.py +++ b/src/pip/_internal/utils/datetime.py @@ -4,8 +4,7 @@ import datetime -def today_is_later_than(year, month, day): - # type: (int, int, int) -> bool +def today_is_later_than(year: int, month: int, day: int) -> bool: today = datetime.date.today() given = datetime.date(year, month, day) From 15fd8518c8a7d1e25953f3d49c490acbd7cdee11 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Thu, 15 Jul 2021 08:28:35 -0500 Subject: [PATCH 06/14] Fix the annotations: `pip/_internal/utils` Fix them on "deprecation.py". --- src/pip/_internal/utils/deprecation.py | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/pip/_internal/utils/deprecation.py b/src/pip/_internal/utils/deprecation.py index b62b3fb6509..c8c40feef51 100644 --- a/src/pip/_internal/utils/deprecation.py +++ b/src/pip/_internal/utils/deprecation.py @@ -17,19 +17,18 @@ class PipDeprecationWarning(Warning): pass -_original_showwarning = None # type: Any +_original_showwarning: Any = None # Warnings <-> Logging Integration def _showwarning( - message, # type: Union[Warning, str] - category, # type: Type[Warning] - filename, # type: str - lineno, # type: int - file=None, # type: Optional[TextIO] - line=None, # type: Optional[str] -): - # type: (...) -> None + message: Union[Warning, str], + category: Type[Warning], + filename: str, + lineno: int, + file: Optional[TextIO] = None, + line: Optional[str] = None, +) -> None: if file is not None: if _original_showwarning is not None: _original_showwarning(message, category, filename, lineno, file, line) @@ -42,8 +41,7 @@ def _showwarning( _original_showwarning(message, category, filename, lineno, file, line) -def install_warning_logger(): - # type: () -> None +def install_warning_logger() -> None: # Enable our Deprecation Warnings warnings.simplefilter("default", PipDeprecationWarning, append=True) @@ -54,8 +52,12 @@ def install_warning_logger(): warnings.showwarning = _showwarning -def deprecated(reason, replacement, gone_in, issue=None): - # type: (str, Optional[str], Optional[str], Optional[int]) -> None +def deprecated( + reason: str, + replacement: Optional[str], + gone_in: Optional[str], + issue: Optional[int] = None +) -> None: """Helper to deprecate existing functionality. reason: From 2115b850ad00bacbf87b6cc4ac530b1748486a49 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Thu, 15 Jul 2021 08:34:51 -0500 Subject: [PATCH 07/14] Fix a bad annotation It required a comma. --- src/pip/_internal/utils/deprecation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/utils/deprecation.py b/src/pip/_internal/utils/deprecation.py index c8c40feef51..57dbdbdca4e 100644 --- a/src/pip/_internal/utils/deprecation.py +++ b/src/pip/_internal/utils/deprecation.py @@ -56,7 +56,7 @@ def deprecated( reason: str, replacement: Optional[str], gone_in: Optional[str], - issue: Optional[int] = None + issue: Optional[int] = None, ) -> None: """Helper to deprecate existing functionality. From 8791d4fafbc3b3cacb3382216ad5f2020db4f2dd Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Thu, 15 Jul 2021 21:33:06 -0500 Subject: [PATCH 08/14] Fix the annotations: `pip/internal/utils` Fix them at "direct_url_helpers.py". --- src/pip/_internal/utils/direct_url_helpers.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/pip/_internal/utils/direct_url_helpers.py b/src/pip/_internal/utils/direct_url_helpers.py index eb50ac42be8..de11fad2599 100644 --- a/src/pip/_internal/utils/direct_url_helpers.py +++ b/src/pip/_internal/utils/direct_url_helpers.py @@ -18,8 +18,7 @@ logger = logging.getLogger(__name__) -def direct_url_as_pep440_direct_reference(direct_url, name): - # type: (DirectUrl, str) -> str +def direct_url_as_pep440_direct_reference(direct_url: DirectUrl, name: str) -> str: """Convert a DirectUrl to a pip requirement string.""" direct_url.validate() # if invalid, this is a pip bug requirement = name + " @ " @@ -42,8 +41,9 @@ def direct_url_as_pep440_direct_reference(direct_url, name): return requirement -def direct_url_from_link(link, source_dir=None, link_is_in_wheel_cache=False): - # type: (Link, Optional[str], bool) -> DirectUrl +def direct_url_from_link( + link: Link, source_dir: Optional[str] = None, link_is_in_wheel_cache: bool = False +) -> DirectUrl: if link.is_vcs: vcs_backend = vcs.get_backend_for_scheme(link.scheme) assert vcs_backend @@ -92,8 +92,7 @@ def direct_url_from_link(link, source_dir=None, link_is_in_wheel_cache=False): ) -def dist_get_direct_url(dist): - # type: (Distribution) -> Optional[DirectUrl] +def dist_get_direct_url(dist: Distribution) -> Optional[DirectUrl]: """Obtain a DirectUrl from a pkg_resource.Distribution. Returns None if the distribution has no `direct_url.json` metadata, From 958dfafe7b5f4fa0b86c94a567427755fea8066a Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 10:36:06 -0500 Subject: [PATCH 09/14] Fix the annotations: `pip/_internal/utils` Fix them at "distutils_args.py". --- src/pip/_internal/utils/distutils_args.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pip/_internal/utils/distutils_args.py b/src/pip/_internal/utils/distutils_args.py index e886c8884d0..e4aa5b827f6 100644 --- a/src/pip/_internal/utils/distutils_args.py +++ b/src/pip/_internal/utils/distutils_args.py @@ -22,8 +22,7 @@ _distutils_getopt = FancyGetopt(_options) # type: ignore -def parse_distutils_args(args): - # type: (List[str]) -> Dict[str, str] +def parse_distutils_args(args: List[str]) -> Dict[str, str]: """Parse provided arguments, returning an object that has the matched arguments. From f81b280044462217faed6c32350ba0ca95a6a6bc Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 10:39:42 -0500 Subject: [PATCH 10/14] Fix the annotations: `pip/_internal/utils` Fix them at "encoding.py". --- src/pip/_internal/utils/encoding.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pip/_internal/utils/encoding.py b/src/pip/_internal/utils/encoding.py index 7c8893d559e..1c73f6c9a5d 100644 --- a/src/pip/_internal/utils/encoding.py +++ b/src/pip/_internal/utils/encoding.py @@ -4,7 +4,7 @@ import sys from typing import List, Tuple -BOMS = [ +BOMS: List[Tuple[bytes, str]] = [ (codecs.BOM_UTF8, "utf-8"), (codecs.BOM_UTF16, "utf-16"), (codecs.BOM_UTF16_BE, "utf-16-be"), @@ -12,13 +12,12 @@ (codecs.BOM_UTF32, "utf-32"), (codecs.BOM_UTF32_BE, "utf-32-be"), (codecs.BOM_UTF32_LE, "utf-32-le"), -] # type: List[Tuple[bytes, str]] +] ENCODING_RE = re.compile(br"coding[:=]\s*([-\w.]+)") -def auto_decode(data): - # type: (bytes) -> str +def auto_decode(data: bytes) -> str: """Check a bytes string for a BOM to correctly detect the encoding Fallback to locale.getpreferredencoding(False) like open() on Python3""" From 19c3ffd62c8c5c6b777a066d056f999450666fce Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 10:42:13 -0500 Subject: [PATCH 11/14] Fix the annotations: `pip/_internal/utils` Fix them at "entrypoints.py". --- src/pip/_internal/utils/entrypoints.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pip/_internal/utils/entrypoints.py b/src/pip/_internal/utils/entrypoints.py index 879bf21ac5f..d839f2c1e27 100644 --- a/src/pip/_internal/utils/entrypoints.py +++ b/src/pip/_internal/utils/entrypoints.py @@ -4,8 +4,7 @@ from pip._internal.cli.main import main -def _wrapper(args=None): - # type: (Optional[List[str]]) -> int +def _wrapper(args: Optional[List[str]]=None) -> int: """Central wrapper for all old entrypoints. Historically pip has had several entrypoints defined. Because of issues From 70e12a4173a2a13031de3e9013509df3aece7c1c Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 11:02:05 -0500 Subject: [PATCH 12/14] Fix an annotation The black tests were failing on this annotation. --- src/pip/_internal/utils/entrypoints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/utils/entrypoints.py b/src/pip/_internal/utils/entrypoints.py index d839f2c1e27..1504a12916b 100644 --- a/src/pip/_internal/utils/entrypoints.py +++ b/src/pip/_internal/utils/entrypoints.py @@ -4,7 +4,7 @@ from pip._internal.cli.main import main -def _wrapper(args: Optional[List[str]]=None) -> int: +def _wrapper(args: Optional[List[str]] = None) -> int: """Central wrapper for all old entrypoints. Historically pip has had several entrypoints defined. Because of issues From 2e832e0821a3e583e2b2671db88e717554317850 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 11:08:39 -0500 Subject: [PATCH 13/14] Fix the annotations: `pip/_internal/utils` Fix them at "filesystem.py". --- src/pip/_internal/utils/filesystem.py | 35 +++++++++------------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/pip/_internal/utils/filesystem.py b/src/pip/_internal/utils/filesystem.py index 177a6b4fbb5..891c989ac94 100644 --- a/src/pip/_internal/utils/filesystem.py +++ b/src/pip/_internal/utils/filesystem.py @@ -15,8 +15,7 @@ from pip._internal.utils.misc import format_size -def check_path_owner(path): - # type: (str) -> bool +def check_path_owner(path: str) -> bool: # If we don't have a way to check the effective uid of this process, then # we'll just assume that we own the directory. if sys.platform == "win32" or not hasattr(os, "geteuid"): @@ -43,8 +42,7 @@ def check_path_owner(path): return False # assume we don't own the path -def copy2_fixed(src, dest): - # type: (str, str) -> None +def copy2_fixed(src: str, dest: str) -> None: """Wrap shutil.copy2() but map errors copying socket files to SpecialFileError as expected. @@ -67,14 +65,12 @@ def copy2_fixed(src, dest): raise -def is_socket(path): - # type: (str) -> bool +def is_socket(path: str) -> bool: return stat.S_ISSOCK(os.lstat(path).st_mode) @contextmanager -def adjacent_tmp_file(path, **kwargs): - # type: (str, **Any) -> Iterator[BinaryIO] +def adjacent_tmp_file(path: str, **kwargs: Any) -> Iterator[BinaryIO]: """Return a file-like object pointing to a tmp file next to path. The file is created securely and is ensured to be written to disk @@ -106,8 +102,7 @@ def adjacent_tmp_file(path, **kwargs): # test_writable_dir and _test_writable_dir_win are copied from Flit, # with the author's agreement to also place them under pip's license. -def test_writable_dir(path): - # type: (str) -> bool +def test_writable_dir(path: str) -> bool: """Check if a directory is writable. Uses os.access() on POSIX, tries creating files on Windows. @@ -125,8 +120,7 @@ def test_writable_dir(path): return _test_writable_dir_win(path) -def _test_writable_dir_win(path): - # type: (str) -> bool +def _test_writable_dir_win(path: str) -> bool: # os.access doesn't work on Windows: http://bugs.python.org/issue2528 # and we can't use tempfile: http://bugs.python.org/issue22107 basename = "accesstest_deleteme_fishfingers_custard_" @@ -154,32 +148,28 @@ def _test_writable_dir_win(path): raise OSError("Unexpected condition testing for writable directory") -def find_files(path, pattern): - # type: (str, str) -> List[str] +def find_files(path: str, pattern: str) -> List[str]: """Returns a list of absolute paths of files beneath path, recursively, with filenames which match the UNIX-style shell glob pattern.""" - result = [] # type: List[str] + result: List[str] = [] for root, _, files in os.walk(path): matches = fnmatch.filter(files, pattern) result.extend(os.path.join(root, f) for f in matches) return result -def file_size(path): - # type: (str) -> Union[int, float] +def file_size(path:str) -> Union[int, float]: # If it's a symlink, return 0. if os.path.islink(path): return 0 return os.path.getsize(path) -def format_file_size(path): - # type: (str) -> str +def format_file_size(path: str) -> str: return format_size(file_size(path)) -def directory_size(path): - # type: (str) -> Union[int, float] +def directory_size(path: str) -> Union[int, float]: size = 0.0 for root, _dirs, files in os.walk(path): for filename in files: @@ -188,6 +178,5 @@ def directory_size(path): return size -def format_directory_size(path): - # type: (str) -> str +def format_directory_size(path: str) -> str: return format_size(directory_size(path)) From 8c95dc4be234446fa0081b3e1fc9bd9491a52f8b Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Mon, 19 Jul 2021 11:15:04 -0500 Subject: [PATCH 14/14] Fix an annotation It was written incorrectly. --- src/pip/_internal/utils/filesystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/utils/filesystem.py b/src/pip/_internal/utils/filesystem.py index 891c989ac94..b7e6191abe6 100644 --- a/src/pip/_internal/utils/filesystem.py +++ b/src/pip/_internal/utils/filesystem.py @@ -158,7 +158,7 @@ def find_files(path: str, pattern: str) -> List[str]: return result -def file_size(path:str) -> Union[int, float]: +def file_size(path: str) -> Union[int, float]: # If it's a symlink, return 0. if os.path.islink(path): return 0