Skip to content

Commit 03be6c8

Browse files
committed
gh-149985: Remove future annotations and if TYPE_CHECKING in pyrepl
`from __future__ import annotations` is soft-deprecated now that evaluation of annotations is always deferred. `if TYPE_CHECKING` can often be replaced with lazy imports.
1 parent 0ed497a commit 03be6c8

19 files changed

Lines changed: 28 additions & 89 deletions

Lib/_pyrepl/_module_completer.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import importlib
42
import os
53
import pkgutil
@@ -15,12 +13,9 @@
1513
from tokenize import TokenInfo
1614
from .fancycompleter import safe_getattr
1715

18-
TYPE_CHECKING = False
19-
20-
if TYPE_CHECKING:
21-
from types import ModuleType
22-
from typing import Any, Iterable, Iterator, Mapping
23-
from .types import CompletionAction
16+
lazy from types import ModuleType
17+
lazy from typing import Any, Iterable, Iterator, Mapping
18+
lazy from .types import CompletionAction
2419

2520

2621
HARDCODED_SUBMODULES = {

Lib/_pyrepl/commands.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2020
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2121

22-
from __future__ import annotations
2322
import os
2423
import time
25-
from typing import TYPE_CHECKING
2624

2725
# Categories of actions:
2826
# killing
@@ -37,8 +35,7 @@
3735
from .trace import trace
3836

3937
# types
40-
if TYPE_CHECKING:
41-
from .historical_reader import HistoricalReader
38+
lazy from .historical_reader import HistoricalReader
4239

4340

4441
class Command:

Lib/_pyrepl/completing_reader.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from __future__ import annotations
2222

2323
from dataclasses import dataclass, field
24-
from typing import TYPE_CHECKING
2524

2625
import re
2726
from . import commands, console, reader
@@ -30,9 +29,7 @@
3029

3130

3231
# types
33-
Command = commands.Command
34-
if TYPE_CHECKING:
35-
from .types import CommandName, CompletionAction, Keymap, KeySpec
32+
lazy from .types import CompletionAction, Keymap
3633

3734

3835
def prefix(wordlist: list[str], j: int = 0) -> str:
@@ -285,7 +282,7 @@ def collect_keymap(self) -> Keymap:
285282
return super().collect_keymap() + (
286283
(r'\t', 'complete'),)
287284

288-
def after_command(self, cmd: Command) -> None:
285+
def after_command(self, cmd: commands.Command) -> None:
289286
super().after_command(cmd)
290287
if not isinstance(cmd, (complete, self_insert)):
291288
self.cmpltn_reset()

Lib/_pyrepl/console.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1818
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1919

20-
from __future__ import annotations
21-
2220
import os
2321
import _colorize
2422

@@ -29,15 +27,13 @@
2927
from dataclasses import dataclass
3028
import re
3129
import sys
32-
from typing import TYPE_CHECKING
3330

3431
from .render import RenderedScreen
3532
from .trace import trace
3633

37-
if TYPE_CHECKING:
38-
from typing import Callable, IO
39-
40-
from .types import CursorXY
34+
lazy from collections.abc import Callable
35+
lazy from typing import IO
36+
lazy from .types import CursorXY
4137

4238

4339
@dataclass

Lib/_pyrepl/content.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from dataclasses import dataclass
42

53
from .utils import ColorSpan, StyleRef, THEME, iter_display_chars, unbracket, wlen

Lib/_pyrepl/fancy_termios.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919

2020
import termios
2121

22-
23-
TYPE_CHECKING = False
24-
25-
if TYPE_CHECKING:
26-
from typing import cast
27-
else:
28-
cast = lambda typ, val: val
22+
from typing import cast
2923

3024

3125
class TermState:

Lib/_pyrepl/fancycompleter.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
#
44
# All Rights Reserved
55
"""Colorful tab completion for Python prompt"""
6-
from __future__ import annotations
7-
86
from _colorize import ANSIColors, get_colors, get_theme
97
import rlcompleter
108
import keyword
119
import types
1210

13-
TYPE_CHECKING = False
14-
15-
if TYPE_CHECKING:
16-
from typing import Any
17-
from _colorize import Theme
11+
lazy from typing import Any
12+
lazy from _colorize import Theme
1813

1914

20-
def safe_getattr(obj, name):
15+
def safe_getattr(obj: object, name: str) -> object:
2116
# Mirror rlcompleter's safeguards so completion does not
2217
# call properties or reify lazy module attributes.
2318
if isinstance(getattr(type(obj), name, None), property):
@@ -35,7 +30,7 @@ def colorize_matches(names: list[str], values: list[Any], theme: Theme) -> list[
3530
for name, obj in zip(names, values)
3631
]
3732

38-
def _color_for_obj(name: str, value: Any, theme: Theme) -> str:
33+
def _color_for_obj(name: str, value: object, theme: Theme) -> str:
3934
t = type(value)
4035
color = _color_by_type(t, theme)
4136
return f"{color}{name}{ANSIColors.RESET}"

Lib/_pyrepl/historical_reader.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1818
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1919

20-
from __future__ import annotations
21-
2220
from contextlib import contextmanager
2321
from dataclasses import dataclass, field
2422

2523
from . import commands, input
2624
from .reader import Reader
2725

28-
29-
if False:
30-
from .types import SimpleContextManager, KeySpec, CommandName
26+
lazy from .types import SimpleContextManager, KeySpec, CommandName
3127

3228

3329
isearch_keymap: tuple[tuple[KeySpec, CommandName], ...] = tuple(

Lib/_pyrepl/input.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@
3333
# [meta-key] is identified with [esc key]. We demand that any console
3434
# class does quite a lot towards emulating a unix terminal.
3535

36-
from __future__ import annotations
37-
3836
from abc import ABC, abstractmethod
3937
import unicodedata
4038
from collections import deque
41-
from typing import TYPE_CHECKING
42-
4339

4440
# types
45-
if TYPE_CHECKING:
46-
from .types import EventTuple
41+
lazy from .types import EventTuple
4742

4843

4944
class InputTranslator(ABC):

Lib/_pyrepl/layout.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Wrap content lines to the terminal width before rendering."""
22

3-
from __future__ import annotations
4-
53
from dataclasses import dataclass
64
from typing import Self
75

86
from .content import ContentFragment, ContentLine
9-
from .types import CursorXY, ScreenInfoRow
7+
8+
lazy from .types import CursorXY, ScreenInfoRow
109

1110

1211
@dataclass(frozen=True, slots=True)

0 commit comments

Comments
 (0)