Skip to content

Commit

Permalink
Fix issue #1593 & Fix issue #1592: isort doesn't work on Python 3.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Nov 11, 2020
1 parent cd38767 commit 8c1158b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions isort/io.py
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8c1158b

Please sign in to comment.