From 0b9a56948bcbfae25e53b16c8ef55feb2e765bd2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 11 Jul 2022 04:58:31 -0700 Subject: [PATCH] only rewrite typing.Callable in 3.10+ --- reorder_python_imports.py | 2 +- testing/generate-typing-pep585-rewrites | 3 +++ tests/reorder_python_imports_test.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/reorder_python_imports.py b/reorder_python_imports.py index 4f7d5f7..2e7d25b 100644 --- a/reorder_python_imports.py +++ b/reorder_python_imports.py @@ -480,7 +480,6 @@ def _fix_file( 'typing=collections.abc:AsyncIterator', 'typing=collections.abc:Awaitable', 'typing=collections.abc:ByteString', - 'typing=collections.abc:Callable', 'typing=collections.abc:Collection', 'typing=collections.abc:Container', 'typing=collections.abc:Coroutine', @@ -507,6 +506,7 @@ def _fix_file( 'typing.re=re:Match', 'typing.re=re:Pattern', )) +REPLACES[(3, 10)].add('typing=collections.abc:Callable') # END GENERATED # GENERATED VIA generate-python-future-info diff --git a/testing/generate-typing-pep585-rewrites b/testing/generate-typing-pep585-rewrites index 3b38e48..5b9f17d 100755 --- a/testing/generate-typing-pep585-rewrites +++ b/testing/generate-typing-pep585-rewrites @@ -15,6 +15,8 @@ def main() -> int: if hasattr(value, '__origin__') # TODO: still can't support symbol renaming if value.__origin__.__name__ == name + # https://github.com/python/cpython/issues/87131 + if name != 'Callable' ] + [ f'typing.re={value.__origin__.__module__}:{name}' for name, value in vars(typing.re).items() @@ -28,6 +30,7 @@ def main() -> int: for rename in sorted(renames, key=lambda s: s.split('=')): print(f' {rename!r},') print('))') + print("REPLACES[(3, 10)].add('typing=collections.abc:Callable')") print('# END GENERATED') return 0 diff --git a/tests/reorder_python_imports_test.py b/tests/reorder_python_imports_test.py index 7e26516..ef399e0 100644 --- a/tests/reorder_python_imports_test.py +++ b/tests/reorder_python_imports_test.py @@ -1178,6 +1178,14 @@ def test_py39_plus_rewrites_pep585_imports(tmpdir): assert f.read() == 'from collections.abc import Sequence\n' +def test_typing_callable_not_rewritten_until_3_10(tmpdir): + f = tmpdir.join('f.py') + f.write('from typing import Callable\n') + assert not main((str(f), '--py39-plus')) + assert main((str(f), '--py310-plus')) + assert f.read() == 'from collections.abc import Callable\n' + + @pytest.mark.parametrize('opt', ('--add-import', '--remove-import')) @pytest.mark.parametrize('s', ('syntax error', '"import os"')) def test_invalid_add_remove_syntaxes(tmpdir, capsys, opt, s):