Skip to content

Commit c3f279e

Browse files
Refactor: migrate imports from typing_extensions to typing (partial #8043)
1 parent 7440c77 commit c3f279e

File tree

10 files changed

+65
-16
lines changed

10 files changed

+65
-16
lines changed

.gitignore

118 Bytes
Binary file not shown.

mypy/checker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
cast,
1919
overload,
2020
)
21-
from typing import TypeGuard
22-
from typing_extensions import TypeAlias
21+
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
2322

2423
import mypy.checkexpr
2524
from mypy import errorcodes as codes, join, message_registry, nodes, operators

mypy/checkexpr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from collections.abc import Iterable, Iterator, Sequence
1010
from contextlib import contextmanager, nullcontext
1111
from typing import Callable, ClassVar, Final, Optional, cast, overload
12-
from typing import assert_never
13-
from typing_extensions import TypeAlias
12+
from typing_extensions import TypeAlias as _TypeAlias, assert_never
1413

1514
import mypy.checker
1615
import mypy.errorcodes as codes

mypy/config_parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
from collections.abc import Mapping, MutableMapping, Sequence
1717
from typing import Any, Callable, Final, TextIO, Union
18-
from typing import Never
19-
from typing_extensions import TypeAlias
18+
from typing_extensions import Never, TypeAlias
2019

2120
from mypy import defaults
2221
from mypy.options import PER_MODULE_OPTIONS, Options

mypy/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections.abc import Iterable, Sequence
66
from typing import TYPE_CHECKING, Final, cast
7-
from typing import TypeGuard
7+
from typing_extensions import TypeGuard
88

99
import mypy.subtypes
1010
import mypy.typeops

mypy/errors.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
from collections import defaultdict
77
from collections.abc import Iterable, Iterator
88
from itertools import chain
9-
from typing import Callable, Final, NoReturn, Optional, TextIO, TypeVar
10-
from typing import Literal
11-
from typing_extensions import Self, TypeAlias
9+
from typing import Callable, Final, Literal, NoReturn, Optional, TextIO, TypeVar
10+
from typing_extensions import Self, TypeAlias as _TypeAlias
1211

1312
from mypy import errorcodes as codes
1413
from mypy.error_formatter import ErrorFormatter

mypy/nodes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from collections.abc import Iterator, Sequence
1010
from enum import Enum, unique
1111
from typing import TYPE_CHECKING, Any, Callable, Final, Optional, TypeVar, Union, cast
12-
from typing import TypeGuard
13-
from typing_extensions import TypeAlias
12+
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
1413

1514
from mypy_extensions import trait
1615

mypy/semanal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
from collections.abc import Collection, Iterable, Iterator
5454
from contextlib import contextmanager
5555
from typing import Any, Callable, Final, TypeVar, cast
56-
from typing import TypeGuard
57-
from typing_extensions import TypeAlias
56+
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
5857

5958
from mypy import errorcodes as codes, message_registry
6059
from mypy.constant_fold import constant_fold_expr

mypy/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from abc import abstractmethod
77
from collections.abc import Iterable, Sequence
88
from typing import TYPE_CHECKING, Any, ClassVar, Final, NewType, TypeVar, Union, cast, overload
9-
from typing import TypeGuard
10-
from typing_extensions import Self, TypeAlias
9+
from typing_extensions import Self, TypeAlias as _TypeAlias, TypeGuard
1110

1211
import mypy.nodes
1312
from mypy.bogus_type import Bogus

refactor_typing_imports.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import re
3+
4+
SAFE_TO_MOVE = {
5+
"Literal",
6+
"Final",
7+
"Protocol",
8+
"TypedDict",
9+
"SupportsIndex",
10+
"Never",
11+
"TypeGuard",
12+
"assert_never",
13+
}
14+
15+
KEEP_IN_EXT = {"Self", "TypeAlias", "TypeAliasType"}
16+
17+
18+
def process_file(path: str):
19+
changed = False
20+
with open(path, encoding="utf-8") as f:
21+
content = f.read()
22+
23+
# Divide imports
24+
matches = re.findall(r"from typing_extensions import (.+)", content)
25+
for match in matches:
26+
parts = [p.strip().split(" as ")[0] for p in match.split(",")]
27+
move = [p for p in parts if p in SAFE_TO_MOVE]
28+
keep = [p for p in parts if p in KEEP_IN_EXT]
29+
30+
# Se houver itens para mover
31+
if move:
32+
old_line = f"from typing_extensions import {match}"
33+
new_parts = [", ".join(keep)] if keep else []
34+
if new_parts:
35+
new_line_ext = f"from typing_extensions import {', '.join(keep)}"
36+
else:
37+
new_line_ext = ""
38+
39+
new_line_typ = f"from typing import {', '.join(move)}"
40+
41+
replacement = new_line_typ + ("\n" + new_line_ext if new_line_ext else "")
42+
content = content.replace(old_line, replacement)
43+
changed = True
44+
45+
if changed:
46+
with open(path, "w", encoding="utf-8") as f:
47+
f.write(content)
48+
print(f"✅ Updated {path}")
49+
50+
51+
for root, _, files in os.walk("mypy/mypy"):
52+
for file in files:
53+
if not file.endswith(".py"):
54+
continue
55+
path = os.path.join(root, file)
56+
process_file(path)

0 commit comments

Comments
 (0)