From 51ac7a455f93897f4e6ba6b8678329030257bf9f Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 10 Oct 2019 06:42:59 +0200 Subject: Coding detection closer to PEP 263 --- isort/compat.py | 6 +++--- test_isort.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/isort/compat.py b/isort/compat.py index 0af5d44a..9e3e6e52 100644 --- a/isort/compat.py +++ b/isort/compat.py @@ -12,17 +12,17 @@ from isort.isort import _SortImports def determine_file_encoding(file_path: Path, default: str = "utf-8") -> str: # see https://www.python.org/dev/peps/pep-0263/ - pattern = re.compile(br"coding[:=]\s*([-\w.]+)") + pattern = re.compile(br"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)") coding = default with file_path.open("rb") as f: for line_number, line in enumerate(f, 1): + if line_number > 2: + break groups = re.findall(pattern, line) if groups: coding = groups[0].decode("ascii") break - if line_number > 2: - break return coding diff --git a/test_isort.py b/test_isort.py index ae5eee89..7ca611f4 100644 --- a/test_isort.py +++ b/test_isort.py @@ -2031,6 +2031,28 @@ def test_other_file_encodings(tmpdir) -> None: ) +def test_encoding_not_in_comment(tmpdir) -> None: + """Test that 'encoding' not in a comment is ignored""" + tmp_fname = tmpdir.join("test_encoding.py") + file_contents = "class Foo\n coding: latin1\n\ns = u'ã'\n".format("utf8") + tmp_fname.write_binary(file_contents.encode("utf8")) + assert ( + SortImports(file_path=str(tmp_fname), settings_path=os.getcwd()).output + == file_contents + ) + + +def test_encoding_not_in_first_two_lines(tmpdir) -> None: + """Test that 'encoding' not in the first two lines is ignored""" + tmp_fname = tmpdir.join("test_encoding.py") + file_contents = "\n\n# -*- coding: latin1\n\ns = u'ã'\n".format("utf8") + tmp_fname.write_binary(file_contents.encode("utf8")) + assert ( + SortImports(file_path=str(tmp_fname), settings_path=os.getcwd()).output + == file_contents + ) + + def test_comment_at_top_of_file() -> None: """Test to ensure isort correctly handles top of file comments""" test_input = ( -- cgit v1.2.1