From 00da85f354539b0d2ed66a999c4adce69729acbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Tue, 3 Dec 2019 15:51:27 +0100 Subject: [PATCH 1/9] rewrote common_dir_prefix using pathlib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/stubutil.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index d21ba4059913..4bffe513e49b 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -6,6 +6,7 @@ import subprocess import re from contextlib import contextmanager +from pathlib import Path from typing import Optional, Tuple, List, Iterator, Union from typing_extensions import overload @@ -247,11 +248,8 @@ def remove_misplaced_type_comments(source: Union[str, bytes]) -> Union[str, byte def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - cur = os.path.dirname(paths[0]) - for path in paths[1:]: - while True: - path = os.path.dirname(path) - if (cur + '/').startswith(path + '/'): - cur = path - break - return cur or '.' + cur = Path(paths[0]) + for parent in cur.parents: + if all(parent in Path(path).parents for path in paths[1:]): + return str(parent) + return '.' From 86ab3ca0cb3fe451d329e66d4cbe6e89df4fc8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Tue, 3 Dec 2019 17:22:47 +0100 Subject: [PATCH 2/9] use pathlib to normalize expected paths in common_dir_prefix test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/test/teststubgen.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 3566f03fb9a1..fa4fc0cf6ae9 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -6,6 +6,8 @@ import re import unittest from types import ModuleType +from pathlib import Path + from typing import Any, List, Tuple, Optional @@ -449,15 +451,15 @@ def test_common_dir_prefix(self) -> None: assert common_dir_prefix([]) == '.' assert common_dir_prefix(['x.pyi']) == '.' assert common_dir_prefix(['./x.pyi']) == '.' - assert common_dir_prefix(['foo/bar/x.pyi']) == 'foo/bar' + assert common_dir_prefix(['foo/bar/x.pyi']) == str(Path('foo/bar')) assert common_dir_prefix(['foo/bar/x.pyi', - 'foo/bar/y.pyi']) == 'foo/bar' + 'foo/bar/y.pyi']) == str(Path('foo/bar')) assert common_dir_prefix(['foo/bar/x.pyi', 'foo/y.pyi']) == 'foo' assert common_dir_prefix(['foo/x.pyi', 'foo/bar/y.pyi']) == 'foo' assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/y.pyi']) == 'foo' assert common_dir_prefix(['foo/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo' - assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' - assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' + assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == str(Path('foo/bar')) + assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == str(Path('foo/bar')) class StubgenHelpersSuite(unittest.TestCase): From 65c6d667b992f615359536a9c0a75af4efb89a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Wed, 4 Dec 2019 18:50:08 +0100 Subject: [PATCH 3/9] replaced pathlib usage with os.path, added windows-specific test for common_dir_prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/stubutil.py | 15 +++++++++------ mypy/test/teststubgen.py | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index 4bffe513e49b..ad9dfad00687 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -2,11 +2,11 @@ import sys import os.path +import itertools import json import subprocess import re from contextlib import contextmanager -from pathlib import Path from typing import Optional, Tuple, List, Iterator, Union from typing_extensions import overload @@ -248,8 +248,11 @@ def remove_misplaced_type_comments(source: Union[str, bytes]) -> Union[str, byte def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - cur = Path(paths[0]) - for parent in cur.parents: - if all(parent in Path(path).parents for path in paths[1:]): - return str(parent) - return '.' + + def common_part(parts): + return all(x == parts[0] for x in parts) + + dirs = [os.path.dirname(p) for p in paths] + parts = zip(*(path.split(os.path.sep) for path in dirs)) + common_parts = (p[0] for p in itertools.takewhile(common_part, parts)) + return os.path.join(*common_parts) or '.' diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index fa4fc0cf6ae9..66303f255b0f 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -6,8 +6,8 @@ import re import unittest from types import ModuleType -from pathlib import Path +import pytest from typing import Any, List, Tuple, Optional @@ -447,19 +447,34 @@ def h(): assert_equal(remove_misplaced_type_comments(original), dest) - def test_common_dir_prefix(self) -> None: + @pytest.mark.skipif(sys.platform == 'win32', reason='Tests building the paths common ancestor on *nix') + def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix([]) == '.' assert common_dir_prefix(['x.pyi']) == '.' assert common_dir_prefix(['./x.pyi']) == '.' - assert common_dir_prefix(['foo/bar/x.pyi']) == str(Path('foo/bar')) + assert common_dir_prefix(['foo/bar/x.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', - 'foo/bar/y.pyi']) == str(Path('foo/bar')) + 'foo/bar/y.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', 'foo/y.pyi']) == 'foo' assert common_dir_prefix(['foo/x.pyi', 'foo/bar/y.pyi']) == 'foo' assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/y.pyi']) == 'foo' assert common_dir_prefix(['foo/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo' - assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == str(Path('foo/bar')) - assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == str(Path('foo/bar')) + assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' + assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' + + @pytest.mark.skipif(sys.platform != 'win32', reason='Tests building the paths common ancestor on Windows') + def test_common_dir_prefix_win(self) -> None: + assert common_dir_prefix(['x.pyi']) == '.' + assert common_dir_prefix([r'.\x.pyi']) == '.' + assert common_dir_prefix([r'foo\bar\x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', + r'foo\bar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\x.pyi', 'foo\bar\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\zar\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\bar\zar\y.pyi']) == r'foo\bar' class StubgenHelpersSuite(unittest.TestCase): From 16eab287d0680cb2aebc1ad8adbe582451a5c5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Wed, 4 Dec 2019 19:10:25 +0100 Subject: [PATCH 4/9] add missing typings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/stubutil.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index ad9dfad00687..e9371a32d10b 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -8,7 +8,7 @@ import re from contextlib import contextmanager -from typing import Optional, Tuple, List, Iterator, Union +from typing import Optional, Tuple, List, Iterator, Sequence, Union from typing_extensions import overload from mypy.moduleinspect import ModuleInspect, InspectError @@ -249,7 +249,7 @@ def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - def common_part(parts): + def common_part(parts: Sequence[str]) -> bool: return all(x == parts[0] for x in parts) dirs = [os.path.dirname(p) for p in paths] From 4b27f98612fd68ae2622dc256698c3ec384c32d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Wed, 4 Dec 2019 19:11:29 +0100 Subject: [PATCH 5/9] fix flake8 warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/test/teststubgen.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 66303f255b0f..09d943e8f3af 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -7,9 +7,8 @@ import unittest from types import ModuleType -import pytest - from typing import Any, List, Tuple, Optional +import pytest # type: ignore # no pytest in typeshed from mypy.test.helpers import ( assert_equal, assert_string_arrays_equal, local_sys_path_set @@ -447,7 +446,8 @@ def h(): assert_equal(remove_misplaced_type_comments(original), dest) - @pytest.mark.skipif(sys.platform == 'win32', reason='Tests building the paths common ancestor on *nix') + @pytest.mark.skipif(sys.platform == 'win32', + reason='Tests building the paths common ancestor on *nix') def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix([]) == '.' assert common_dir_prefix(['x.pyi']) == '.' @@ -462,7 +462,8 @@ def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' - @pytest.mark.skipif(sys.platform != 'win32', reason='Tests building the paths common ancestor on Windows') + @pytest.mark.skipif(sys.platform != 'win32', + reason='Tests building the paths common ancestor on Windows') def test_common_dir_prefix_win(self) -> None: assert common_dir_prefix(['x.pyi']) == '.' assert common_dir_prefix([r'.\x.pyi']) == '.' @@ -470,7 +471,7 @@ def test_common_dir_prefix_win(self) -> None: assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar' assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\y.pyi']) == 'foo' - assert common_dir_prefix([r'foo\x.pyi', 'foo\bar\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\y.pyi']) == 'foo' assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\y.pyi']) == 'foo' assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\zar\y.pyi']) == 'foo' assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar' From ebaff04c495b6f803ec67afe9d75a610c478cc6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Sun, 15 Dec 2019 15:43:26 +0100 Subject: [PATCH 6/9] switch to unittest.skipIf instead of pytest marks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/test/teststubgen.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 09d943e8f3af..e03217bff034 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -8,7 +8,6 @@ from types import ModuleType from typing import Any, List, Tuple, Optional -import pytest # type: ignore # no pytest in typeshed from mypy.test.helpers import ( assert_equal, assert_string_arrays_equal, local_sys_path_set @@ -446,8 +445,8 @@ def h(): assert_equal(remove_misplaced_type_comments(original), dest) - @pytest.mark.skipif(sys.platform == 'win32', - reason='Tests building the paths common ancestor on *nix') + @unittest.skipIf(sys.platform == 'win32', + 'Tests building the paths common ancestor on *nix') def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix([]) == '.' assert common_dir_prefix(['x.pyi']) == '.' @@ -462,8 +461,8 @@ def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' - @pytest.mark.skipif(sys.platform != 'win32', - reason='Tests building the paths common ancestor on Windows') + @unittest.skipIf(sys.platform != 'win32', + 'Tests building the paths common ancestor on Windows') def test_common_dir_prefix_win(self) -> None: assert common_dir_prefix(['x.pyi']) == '.' assert common_dir_prefix([r'.\x.pyi']) == '.' From b043393b32add1334e6c1fd8df08a8899b7d7ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Sun, 15 Dec 2019 15:58:34 +0100 Subject: [PATCH 7/9] extend existing test for mixed separators in path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/test/teststubgen.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index e03217bff034..d5fb7bdf14cc 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -460,6 +460,8 @@ def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix(['foo/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo' assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' + assert common_dir_prefix([r'foo/bar\x.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar' @unittest.skipIf(sys.platform != 'win32', 'Tests building the paths common ancestor on Windows') @@ -475,6 +477,9 @@ def test_common_dir_prefix_win(self) -> None: assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\zar\y.pyi']) == 'foo' assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar' assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\bar\zar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo/bar\x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo/bar/x.pyi']) == r'foo\bar' class StubgenHelpersSuite(unittest.TestCase): From c8eb92b8654bf0f2a74bd1227bfbaf9c654928ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Sun, 15 Dec 2019 15:59:27 +0100 Subject: [PATCH 8/9] normalize paths + use os-specific separator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/stubutil.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index e9371a32d10b..bbad6c6f7b00 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -2,13 +2,12 @@ import sys import os.path -import itertools import json import subprocess import re from contextlib import contextmanager -from typing import Optional, Tuple, List, Iterator, Sequence, Union +from typing import Optional, Tuple, List, Iterator, Union from typing_extensions import overload from mypy.moduleinspect import ModuleInspect, InspectError @@ -249,10 +248,11 @@ def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - def common_part(parts: Sequence[str]) -> bool: - return all(x == parts[0] for x in parts) - - dirs = [os.path.dirname(p) for p in paths] - parts = zip(*(path.split(os.path.sep) for path in dirs)) - common_parts = (p[0] for p in itertools.takewhile(common_part, parts)) - return os.path.join(*common_parts) or '.' + cur = os.path.dirname(os.path.normpath(paths[0])) + for path in paths[1:]: + while True: + path = os.path.dirname(os.path.normpath(path)) + if (cur + os.sep).startswith(path + os.sep): + cur = path + break + return cur or '.' From 23f36468fd94896c94c7f40a9a45e6048a168795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Sun, 15 Dec 2019 16:02:14 +0100 Subject: [PATCH 9/9] reduce diff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oleg Höfling --- mypy/stubutil.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index bbad6c6f7b00..5772d3fc9981 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -247,7 +247,6 @@ def remove_misplaced_type_comments(source: Union[str, bytes]) -> Union[str, byte def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - cur = os.path.dirname(os.path.normpath(paths[0])) for path in paths[1:]: while True: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy