Skip to content

Commit

Permalink
only rewrite typing.Callable in 3.10+
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jul 11, 2022
1 parent 6eecfe6 commit 0b9a569
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reorder_python_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions testing/generate-typing-pep585-rewrites
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/reorder_python_imports_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0b9a569

Please sign in to comment.