|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pyupgrade._data import Settings |
| 6 | +from pyupgrade._main import _fix_plugins |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + ('s', 'version'), |
| 11 | + ( |
| 12 | + pytest.param( |
| 13 | + 'from collections.abc import Generator\n' |
| 14 | + 'def f() -> Generator[int, None, None]: yield 1\n', |
| 15 | + (3, 12), |
| 16 | + id='not 3.13+, no __future__.annotations', |
| 17 | + ), |
| 18 | + pytest.param( |
| 19 | + 'from __future__ import annotations\n' |
| 20 | + 'from collections.abc import Generator\n' |
| 21 | + 'def f() -> Generator[int]: yield 1\n', |
| 22 | + (3, 12), |
| 23 | + id='already converted!', |
| 24 | + ), |
| 25 | + pytest.param( |
| 26 | + 'from __future__ import annotations\n' |
| 27 | + 'from collections.abc import Generator\n' |
| 28 | + 'def f() -> Generator[int, int, None]: yield 1\n' |
| 29 | + 'def g() -> Generator[int, int, int]: yield 1\n', |
| 30 | + (3, 12), |
| 31 | + id='non-None send/return type', |
| 32 | + ), |
| 33 | + ), |
| 34 | +) |
| 35 | +def test_fix_pep696_noop(s, version): |
| 36 | + assert _fix_plugins(s, settings=Settings(min_version=version)) == s |
| 37 | + |
| 38 | + |
| 39 | +def test_fix_pep696_noop_keep_runtime_typing(): |
| 40 | + settings = Settings(min_version=(3, 12), keep_runtime_typing=True) |
| 41 | + s = '''\ |
| 42 | +from __future__ import annotations |
| 43 | +from collections.abc import Generator |
| 44 | +def f() -> Generator[int, None, None]: yield 1 |
| 45 | +''' |
| 46 | + assert _fix_plugins(s, settings=settings) == s |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + ('s', 'expected'), |
| 51 | + ( |
| 52 | + pytest.param( |
| 53 | + 'from __future__ import annotations\n' |
| 54 | + 'from typing import Generator\n' |
| 55 | + 'def f() -> Generator[int, None, None]: yield 1\n', |
| 56 | +
|
| 57 | + 'from __future__ import annotations\n' |
| 58 | + 'from collections.abc import Generator\n' |
| 59 | + 'def f() -> Generator[int]: yield 1\n', |
| 60 | +
|
| 61 | + id='typing.Generator', |
| 62 | + ), |
| 63 | + pytest.param( |
| 64 | + 'from __future__ import annotations\n' |
| 65 | + 'from typing_extensions import Generator\n' |
| 66 | + 'def f() -> Generator[int, None, None]: yield 1\n', |
| 67 | +
|
| 68 | + 'from __future__ import annotations\n' |
| 69 | + 'from typing_extensions import Generator\n' |
| 70 | + 'def f() -> Generator[int]: yield 1\n', |
| 71 | +
|
| 72 | + id='typing_extensions.Generator', |
| 73 | + ), |
| 74 | + pytest.param( |
| 75 | + 'from __future__ import annotations\n' |
| 76 | + 'from collections.abc import Generator\n' |
| 77 | + 'def f() -> Generator[int, None, None]: yield 1\n', |
| 78 | +
|
| 79 | + 'from __future__ import annotations\n' |
| 80 | + 'from collections.abc import Generator\n' |
| 81 | + 'def f() -> Generator[int]: yield 1\n', |
| 82 | +
|
| 83 | + id='collections.abc.Generator', |
| 84 | + ), |
| 85 | + pytest.param( |
| 86 | + 'from __future__ import annotations\n' |
| 87 | + 'from collections.abc import AsyncGenerator\n' |
| 88 | + 'async def f() -> AsyncGenerator[int, None]: yield 1\n', |
| 89 | +
|
| 90 | + 'from __future__ import annotations\n' |
| 91 | + 'from collections.abc import AsyncGenerator\n' |
| 92 | + 'async def f() -> AsyncGenerator[int]: yield 1\n', |
| 93 | +
|
| 94 | + id='collections.abc.AsyncGenerator', |
| 95 | + ), |
| 96 | + ), |
| 97 | +) |
| 98 | +def test_fix_pep696_with_future_annotations(s, expected): |
| 99 | + assert _fix_plugins(s, settings=Settings(min_version=(3, 12))) == expected |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + ('s', 'expected'), |
| 104 | + ( |
| 105 | + pytest.param( |
| 106 | + 'from collections.abc import Generator\n' |
| 107 | + 'def f() -> Generator[int, None, None]: yield 1\n', |
| 108 | +
|
| 109 | + 'from collections.abc import Generator\n' |
| 110 | + 'def f() -> Generator[int]: yield 1\n', |
| 111 | +
|
| 112 | + id='Generator', |
| 113 | + ), |
| 114 | + pytest.param( |
| 115 | + 'from collections.abc import AsyncGenerator\n' |
| 116 | + 'async def f() -> AsyncGenerator[int, None]: yield 1\n', |
| 117 | +
|
| 118 | + 'from collections.abc import AsyncGenerator\n' |
| 119 | + 'async def f() -> AsyncGenerator[int]: yield 1\n', |
| 120 | +
|
| 121 | + id='AsyncGenerator', |
| 122 | + ), |
| 123 | + ), |
| 124 | +) |
| 125 | +def test_fix_pep696_with_3_13(s, expected): |
| 126 | + assert _fix_plugins(s, settings=Settings(min_version=(3, 13))) == expected |
0 commit comments