Skip to content

Commit e2118b0

Browse files
gh-149985: Remove future annotations and if TYPE_CHECKING in pyrepl (#149992)
`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 7cc3334 commit e2118b0

22 files changed

Lines changed: 32 additions & 118 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/_threading_handler.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
1-
from __future__ import annotations
2-
31
from dataclasses import dataclass, field
42
import traceback
53

64

7-
TYPE_CHECKING = False
8-
if TYPE_CHECKING:
9-
from threading import Thread
10-
from types import TracebackType
11-
from typing import Protocol
12-
13-
class ExceptHookArgs(Protocol):
14-
@property
15-
def exc_type(self) -> type[BaseException]: ...
16-
@property
17-
def exc_value(self) -> BaseException | None: ...
18-
@property
19-
def exc_traceback(self) -> TracebackType | None: ...
20-
@property
21-
def thread(self) -> Thread | None: ...
22-
23-
class ShowExceptions(Protocol):
24-
def __call__(self) -> int: ...
25-
def add(self, s: str) -> None: ...
26-
27-
from .reader import Reader
5+
lazy from threading import ExceptHookArgs
6+
lazy from .reader import Reader
287

298

309
def install_threading_hook(reader: Reader) -> None:

Lib/_pyrepl/base_eventqueue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def push(self, char: int | bytes) -> None:
7979
if isinstance(k, dict):
8080
self.keymap = k
8181
else:
82-
self.insert(Event('key', k, self.buf.take_bytes())) # type: ignore[attr-defined]
82+
self.insert(Event('key', k, self.buf.take_bytes()))
8383
self.keymap = self.compiled_keymap
8484

8585
elif self.buf and self.buf[0] == 27: # escape
@@ -89,7 +89,7 @@ def push(self, char: int | bytes) -> None:
8989
trace('unrecognized escape sequence, propagating...')
9090
self.keymap = self.compiled_keymap
9191
self.insert(Event('key', '\033', b'\033'))
92-
for _c in self.buf.take_bytes()[1:]: # type: ignore[attr-defined]
92+
for _c in self.buf.take_bytes()[1:]:
9393
self.push(_c)
9494

9595
else:
@@ -98,5 +98,5 @@ def push(self, char: int | bytes) -> None:
9898
except UnicodeError:
9999
return
100100
else:
101-
self.insert(Event('key', decoded, self.buf.take_bytes())) # type: ignore[attr-defined]
101+
self.insert(Event('key', decoded, self.buf.take_bytes()))
102102
self.keymap = self.compiled_keymap

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1919
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2020

21-
from __future__ import annotations
22-
2321
from dataclasses import dataclass, field
24-
from typing import TYPE_CHECKING
2522

2623
import re
2724
from . import commands, console, reader
@@ -30,9 +27,7 @@
3027

3128

3229
# types
33-
Command = commands.Command
34-
if TYPE_CHECKING:
35-
from .types import CompletionAction, Keymap
30+
lazy from .types import CompletionAction, Keymap
3631

3732

3833
def prefix(wordlist: list[str], j: int = 0) -> str:
@@ -285,7 +280,7 @@ def collect_keymap(self) -> Keymap:
285280
return super().collect_keymap() + (
286281
(r'\t', 'complete'),)
287282

288-
def after_command(self, cmd: Command) -> None:
283+
def after_command(self, cmd: commands.Command) -> None:
289284
super().after_command(cmd)
290285
if not isinstance(cmd, (complete, self_insert)):
291286
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(

0 commit comments

Comments
 (0)