Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23dba19

Browse files
authored
Use some more dogfooding (#7027)
This PR does essentially two (related) things: * `TYPE_CHECKING` is the official way for type-checking-only imports to avoid import cycles. This PR removes all uses of `if MYPY: ...` except for couple absolutely necessary files that are imported from `setup.py`. * `typing_extensions` was designed in part as a drop-in replacement for `typing` on Python versions where some names are not yet available in `typing`. I propose that mypy code adopts these itself. IMO this makes code look tidier (also it is 170 negative lines).
1 parent 47e68e2 commit 23dba19

59 files changed

Lines changed: 101 additions & 278 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

mypy/binder.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
21
from contextlib import contextmanager
32
from collections import defaultdict
43

5-
MYPY = False
6-
if MYPY:
7-
from typing import DefaultDict
4+
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
5+
from typing_extensions import DefaultDict
86

97
from mypy.types import Type, AnyType, PartialType, UnionType, TypeOfAny, NoneType
108
from mypy.subtypes import is_subtype
@@ -35,10 +33,7 @@ def __init__(self) -> None:
3533
self.unreachable = False
3634

3735

38-
if MYPY:
39-
# This is the type of stored assignments for union type rvalues.
40-
# We use 'if MYPY: ...' since typing-3.5.1 does not have 'DefaultDict'
41-
Assigns = DefaultDict[Expression, List[Tuple[Type, Optional[Type]]]]
36+
Assigns = DefaultDict[Expression, List[Tuple[Type, Optional[Type]]]]
4237

4338

4439
class ConditionalTypeBinder:

mypy/build.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525

2626
from typing import (AbstractSet, Any, Dict, Iterable, Iterator, List,
2727
Mapping, NamedTuple, Optional, Set, Tuple, Union, Callable, TextIO)
28-
MYPY = False
29-
if MYPY:
30-
from typing import ClassVar
31-
from typing_extensions import Final
32-
28+
from typing_extensions import ClassVar, Final, TYPE_CHECKING
3329
from mypy_extensions import TypedDict
3430

3531
from mypy.nodes import MypyFile, ImportBase, Import, ImportFrom, ImportAll, SymbolTable
@@ -45,7 +41,7 @@
4541
from mypy.util import (
4642
DecodeError, decode_python_encoding, is_sub_path, get_mypy_comments, module_prefix
4743
)
48-
if MYPY:
44+
if TYPE_CHECKING:
4945
from mypy.report import Reports # Avoid unconditional slow import
5046
from mypy import moduleinfo
5147
from mypy.fixup import fixup_module

mypy/checker.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator, Iterable,
99
Sequence
1010
)
11+
from typing_extensions import Final
1112

1213
from mypy.errors import Errors, report_internal_error
1314
from mypy.nodes import (
@@ -68,11 +69,6 @@
6869
from mypy import state
6970
from mypy.traverser import has_return_statement
7071

71-
MYPY = False
72-
if MYPY:
73-
from typing_extensions import Final
74-
75-
7672
T = TypeVar('T')
7773

7874
DEFAULT_LAST_PASS = 1 # type: Final # Pass numbers start at 0

mypy/checkexpr.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from typing import (
66
cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator
77
)
8-
MYPY = False
9-
if MYPY:
10-
from typing import ClassVar
11-
from typing_extensions import Final
8+
from typing_extensions import ClassVar, Final
129

1310
from mypy.errors import report_internal_error
1411
from mypy.typeanal import (

mypy/checkmember.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Type checking of attribute access"""
22

33
from typing import cast, Callable, List, Optional, TypeVar, Union
4+
from typing_extensions import TYPE_CHECKING
45

56
from mypy.types import (
67
Type, Instance, AnyType, TupleType, TypedDictType, CallableType, FunctionLike, TypeVarDef,
@@ -25,8 +26,7 @@
2526
from mypy import meet
2627
from mypy.typeops import tuple_fallback
2728

28-
MYPY = False
29-
if MYPY: # import for forward declaration only
29+
if TYPE_CHECKING: # import for forward declaration only
3030
import mypy.checker
3131

3232
from mypy import state

mypy/checkstrformat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44

55
from typing import cast, List, Tuple, Dict, Callable, Union, Optional, Pattern
6+
from typing_extensions import Final, TYPE_CHECKING
67

78
from mypy.types import (
89
Type, AnyType, TupleType, Instance, UnionType, TypeOfAny
@@ -11,12 +12,10 @@
1112
StrExpr, BytesExpr, UnicodeExpr, TupleExpr, DictExpr, Context, Expression, StarExpr
1213
)
1314

14-
MYPY = False
15-
if MYPY:
15+
if TYPE_CHECKING:
1616
# break import cycle only needed for mypy
1717
import mypy.checker
1818
import mypy.checkexpr
19-
from typing_extensions import Final
2019
from mypy import message_registry
2120
from mypy.messages import MessageBuilder
2221

mypy/config_parser.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
import re
77
import sys
88

9-
from mypy import defaults
10-
from mypy.options import Options, PER_MODULE_OPTIONS
11-
129
from typing import Any, Dict, List, Mapping, Optional, Tuple, TextIO
10+
from typing_extensions import Final
1311

14-
15-
MYPY = False
16-
if MYPY:
17-
from typing_extensions import Final
12+
from mypy import defaults
13+
from mypy.options import Options, PER_MODULE_OPTIONS
1814

1915

2016
def parse_version(v: str) -> Tuple[int, int]:

mypy/constraints.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Type inference constraints."""
22

33
from typing import Iterable, List, Optional, Sequence
4+
from typing_extensions import Final
45

56
from mypy.types import (
67
CallableType, Type, TypeVisitor, UnboundType, AnyType, NoneType, TypeVarType, Instance,
@@ -15,11 +16,6 @@
1516
from mypy.nodes import COVARIANT, CONTRAVARIANT
1617
from mypy.argmap import ArgTypeExpander
1718

18-
MYPY = False
19-
if MYPY:
20-
from typing_extensions import Final
21-
22-
2319
SUBTYPE_OF = 0 # type: Final[int]
2420
SUPERTYPE_OF = 1 # type: Final[int]
2521

mypy/defaults.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22

3-
MYPY = False
4-
if MYPY:
5-
from typing_extensions import Final
3+
from typing_extensions import Final
64

75
PYTHON2_VERSION = (2, 7) # type: Final
86
PYTHON3_VERSION = (3, 6) # type: Final

mypy/dmypy_server.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from contextlib import redirect_stderr, redirect_stdout
1818

1919
from typing import AbstractSet, Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple
20+
from typing_extensions import Final
2021

2122
import mypy.build
2223
import mypy.errors
@@ -33,11 +34,6 @@
3334
from mypy.typestate import reset_global_state
3435
from mypy.version import __version__
3536

36-
37-
MYPY = False
38-
if MYPY:
39-
from typing_extensions import Final
40-
4137
MEM_PROFILE = False # type: Final # If True, dump memory profile after initialization
4238

4339
if sys.platform == 'win32':

0 commit comments

Comments
 (0)