Skip to content

Commit 3b68f47

Browse files
committed
Cast record file to typing.IO[str] to appease mypy for python 2
1 parent bd07bfb commit 3b68f47

File tree

1 file changed

+8
-7
lines changed
  • src/pip/_internal/operations/install

1 file changed

+8
-7
lines changed

src/pip/_internal/operations/install/wheel.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import compileall
1111
import contextlib
1212
import csv
13-
import io
1413
import logging
1514
import os.path
1615
import re
@@ -25,7 +24,7 @@
2524
from pip._vendor import pkg_resources
2625
from pip._vendor.distlib.scripts import ScriptMaker
2726
from pip._vendor.distlib.util import get_export_entry
28-
from pip._vendor.six import PY3, StringIO
27+
from pip._vendor.six import StringIO
2928

3029
from pip._internal.exceptions import InstallationError
3130
from pip._internal.locations import get_major_minor_version
@@ -39,6 +38,7 @@
3938

4039
if MYPY_CHECK_RUNNING:
4140
from email.message import Message
41+
import typing # noqa F401
4242
from typing import (
4343
Dict, List, Optional, Sequence, Tuple, Any,
4444
Iterable, Iterator, Callable, Set,
@@ -602,12 +602,13 @@ def _generate_file(path, **kwargs):
602602
lib_dir=lib_dir)
603603
with _generate_file(record_path, **csv_io_kwargs('w')) as record_file:
604604

605-
# For Python 3, we create the file in text mode, hence we
606-
# cast record_file to io.StringIO
607-
if PY3:
608-
record_file = cast(io.StringIO, record_file)
605+
# The mypy type inference for record_file is different for
606+
# Python 3 (typing.IO[Any]) and Python 2 (typing.BinaryIO),
607+
# leading us to explicitly cast to typing.IO[str] as a workaround
608+
# for bad Python 2 behaviour
609+
record_file_obj = cast('typing.IO[str]', record_file)
609610

610-
writer = csv.writer(record_file)
611+
writer = csv.writer(record_file_obj)
611612
writer.writerows(sorted_outrows(rows)) # sort to simplify testing
612613

613614

0 commit comments

Comments
 (0)