From 8c1158b3f472efe47879e72f96e690ea73b1d065 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Tue, 10 Nov 2020 23:49:04 -0800 Subject: [PATCH] Fix issue #1593 & Fix issue #1592: isort doesn't work on Python 3.6.0 --- isort/io.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/isort/io.py b/isort/io.py index 7ff2807d2..a002bc89b 100644 --- a/isort/io.py +++ b/isort/io.py @@ -4,14 +4,16 @@ from contextlib import contextmanager from io import BytesIO, StringIO, TextIOWrapper from pathlib import Path -from typing import Callable, Iterator, NamedTuple, TextIO, Union +from typing import Callable, Iterator, TextIO, Union +from isort._future import dataclass from isort.exceptions import UnsupportedEncoding _ENCODING_PATTERN = re.compile(br"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)") -class File(NamedTuple): +@dataclass(frozen=True) +class File: stream: TextIO path: Path encoding: str @@ -26,7 +28,11 @@ def detect_encoding(filename: str, readline: Callable[[], bytes]): @staticmethod def from_contents(contents: str, filename: str) -> "File": encoding = File.detect_encoding(filename, BytesIO(contents.encode("utf-8")).readline) - return File(StringIO(contents), path=Path(filename).resolve(), encoding=encoding) + return File( # type: ignore + stream=StringIO(contents), + path=Path(filename).resolve(), + encoding=encoding + ) @property def extension(self): @@ -55,7 +61,7 @@ def read(filename: Union[str, Path]) -> Iterator["File"]: stream = None try: stream = File._open(file_path) - yield File(stream=stream, path=file_path, encoding=stream.encoding) + yield File(stream=stream, path=file_path, encoding=stream.encoding) # type: ignore finally: if stream is not None: stream.close()