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

Remove redundant Python 2.7 code #9335

Merged
merged 7 commits into from
Dec 23, 2020
Merged

Remove redundant Python 2.7 code #9335

merged 7 commits into from
Dec 23, 2020

Conversation

hugovk
Copy link
Contributor

@hugovk hugovk commented Dec 22, 2020

For #8802.

This removes some redundant code now that Python 2.7 is no longer supported.

The bulk removes most of the use of six, except for these where I couldn't figure out the type ignores on the replacements:

from pip._vendor.six.moves import xmlrpc_client  # type: ignore
from pip._vendor.six.moves import collections_abc  # type: ignore

And also ensure_binary, ensure_text, ensure_str, reraise, perhaps these should be copied (and simplified) as utility functions?

I also didn't touch any src/pip/_vendor code.

There's definitely more Python 2 stuff that can be removed, but I didn't want this big PR to get any bigger.

@uranusjr
Copy link
Member

The type: ignore thing on those two moves is due to Mypy not correctly linting them. You can replace them directly with xmlrpc.client and collection.abc.

@hugovk
Copy link
Contributor Author

hugovk commented Dec 22, 2020

For example, with:

diff --git a/src/pip/_internal/resolution/resolvelib/found_candidates.py b/src/pip/_internal/resolution/resolvelib/found_candidates.py
index a669e8936..db40bf86b 100644
--- a/src/pip/_internal/resolution/resolvelib/found_candidates.py
+++ b/src/pip/_internal/resolution/resolvelib/found_candidates.py
@@ -1,7 +1,7 @@
 import itertools
 import operator
 
-from pip._vendor.six.moves import collections_abc  # type: ignore
+import collections.abc  # type: ignore
 
 from pip._internal.utils.compat import lru_cache
 from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -46,7 +46,7 @@ def _insert_installed(installed, others):
     return iter(candidates)
 
 
-class FoundCandidates(collections_abc.Sequence):
+class FoundCandidates(collections.abc.Sequence):
     """A lazy sequence to provide candidates to the resolver.
 
     The intended usage is to return this from `find_matches()` so the resolver

I get:

$ pre-commit run --all-files mypy
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

src/pip/_internal/resolution/resolvelib/found_candidates.py:49: error:
Missing
type
parameters
for
generic
type
"Sequence"
    ...ass FoundCandidates(collections.abc.Seque...
                           ^
src/pip/_internal/resolution/resolvelib/found_candidates.py:67: error:
Signature
of
"__getitem__"
incompatible
with
supertype
"Sequence"
        def __getitem__(self, index):
        ^

@uranusjr
Copy link
Member

Ah OK, this one’s special because on Python 3 we need to use protocol to satisfy Mypy. But let’s not touch code for the new resolver for now, since there are more likely fixes we need to backport to 20.3 before 21.0 can drop, and adding converting code there would make backporting difficult.

Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for picking this up! <3

src/pip/_internal/operations/prepare.py Outdated Show resolved Hide resolved
src/pip/_internal/pyproject.py Outdated Show resolved Hide resolved
src/pip/_internal/utils/compat.py Outdated Show resolved Hide resolved
src/pip/_internal/vcs/bazaar.py Show resolved Hide resolved
Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick response! This is ready to go IMO, but a few inline nitpicks below. Feel free to ignore them! ^.^

src/pip/_internal/cli/progress_bars.py Show resolved Hide resolved
src/pip/_internal/cli/progress_bars.py Outdated Show resolved Hide resolved
tests/functional/test_download.py Outdated Show resolved Hide resolved
tests/functional/test_install.py Outdated Show resolved Hide resolved
Copy link
Contributor

@jdufresne jdufresne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Returns a tuple (x, y) representing the width(x) and the height(y)
in characters of the terminal window.
"""
return tuple(shutil.get_terminal_size()) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shutil.get_terminal_size() already returns a tuple. Perhaps this compat shim could be removed entirely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this was to work around a Typeshed declaration bug. That was fixed upstream since linting jobs are passing without issues.

Comment on lines +53 to +54
shlex.quote(str(arg)) if isinstance(arg, HiddenText)
else shlex.quote(arg) for arg in args
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above this code is partially outdated now and perhaps the solution is too. Thoughts on?

Suggested change
shlex.quote(str(arg)) if isinstance(arg, HiddenText)
else shlex.quote(arg) for arg in args
shlex.quote(str(arg)) for arg in args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion makes sense based on the comment. What do the maintainers think (who may have more context)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion looks good for me. In retrospect, we could have implemented __unicode__ on HiddenText and used six.text_type here (it would make the intent more obvious), but that’s all history now.

@@ -166,7 +158,7 @@ def tmpdir(request, tmpdir):
# py.path.remove() uses str paths on Python 2 and cannot
# handle non-ASCII file names. This works around the problem by
# passing a unicode object to rmtree().
shutil.rmtree(six.text_type(tmpdir), ignore_errors=True)
shutil.rmtree(str(tmpdir), ignore_errors=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above this line is outdated and perhaps the solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe just shutil.rmtree(tmpdir, ignore_errors=True)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either that or simply tmpdir.remove().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmpdir.remove(ignore_errors=True), rather.

tests/lib/__init__.py Outdated Show resolved Hide resolved
Comment on lines 84 to +86
u"pip = pip._internal.main:pip",
u"pip:pip = pip._internal.main:pip",
pytest.param(u"進入點 = 套件.模組:函式", marks=skip_if_python2),
u"進入點 = 套件.模組:函式",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The u-prefix could be dropped. That work could also be handled separately if preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this could be done with pyupgrade.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll get to that after this, I imagine.

@jdufresne
Copy link
Contributor

jdufresne commented Dec 22, 2020

Without Python 2 support, the wheel is not universal.

diff --git a/setup.cfg b/setup.cfg
index 84a959a32..10210196d 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -101,8 +101,5 @@ exclude_lines =
     # Can be set to exclude e.g. `if PY2:` on Python 3
     ${PIP_CI_COVERAGE_EXCLUDES}
 
-[bdist_wheel]
-universal = 1
-
 [metadata]
 license_file = LICENSE.txt

Edit: Done in #9339

@pradyunsg
Copy link
Member

I think this PR is mergable as is, and we can iterate on this stuff as we move forward. :)

@pradyunsg
Copy link
Member

🌱

@hugovk hugovk deleted the rm-2 branch December 23, 2020 17:50
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants