Skip to content

Commit 0c084dc

Browse files
authored
Merge branch 'master' into refine-enum-branch-analysis
2 parents 4cb5572 + 95ac93f commit 0c084dc

File tree

129 files changed

+4009
-3911
lines changed

Some content is hidden

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

129 files changed

+4009
-3911
lines changed

docs/source/command_line.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@ Miscellaneous
622622
have many scripts that import a large package, the behavior enabled
623623
by this flag is often more convenient.)
624624

625+
``--new-semantic-analyzer``
626+
This flag switches to an improved, experimental implementation of
627+
the *semantic analyzer* (the part of mypy that binds Python
628+
names to definitions). The old and the new semantic analyzers
629+
mostly behave identically. The new semantic analyzer is better at
630+
handling import cycles and forward references to definitions. It
631+
also fixes inconsistencies between the daemon and non-daemon modes,
632+
and it detects additional error conditions.
633+
634+
Likely, the next mypy release will use the new semantic analyzer by
635+
default, and the old semantic analyzer will be removed in the next
636+
release after that.
637+
625638
.. _PEP 420: https://www.python.org/dev/peps/pep-0420/
626639

627640
.. _PEP 561: https://www.python.org/dev/peps/pep-0561/

docs/source/common_issues.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ to see the types of all local variables at once. Example:
456456
b = 'one'
457457
reveal_locals()
458458
# Revealed local types are:
459-
# a: builtins.int
460-
# b: builtins.str
459+
# a: builtins.int
460+
# b: builtins.str
461461
.. note::
462462

463463
``reveal_type`` and ``reveal_locals`` are only understood by mypy and

docs/source/config_file.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Miscellaneous strictness flags
294294
Allows variables to be redefined with an arbitrary type, as long as the redefinition
295295
is in the same block and nesting level as the original definition.
296296

297-
``implicit-reexport`` (bool, default True)
297+
``implicit_reexport`` (bool, default True)
298298
By default, imported values to a module are treated as exported and mypy allows
299299
other modules to import them. When false, mypy will not re-export unless
300300
the item is imported using from-as. Note that mypy treats stub files as if this
@@ -456,3 +456,7 @@ Miscellaneous
456456

457457
``verbosity`` (integer, default 0)
458458
Controls how much debug output will be generated. Higher numbers are more verbose.
459+
460+
``new_semantic_analyzer`` (bool, default False)
461+
Enables the experimental new semantic analyzer.
462+
(See :ref:`The mypy command line <command-line>` for more information.)

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: 6 additions & 13 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
@@ -1876,20 +1872,17 @@ def patch_dependency_parents(self) -> None:
18761872
details.
18771873
18781874
However, this patching process can occur after `a` has been parsed and
1879-
serialized during increment mode. Consequently, we need to repeat this
1875+
serialized during incremental mode. Consequently, we need to repeat this
18801876
patch when deserializing a cached file.
18811877
18821878
This function should be called only when processing fresh SCCs -- the
18831879
semantic analyzer will perform this patch for us when processing stale
18841880
SCCs.
18851881
"""
1886-
Analyzer = Union[SemanticAnalyzerPass2, NewSemanticAnalyzer] # noqa
1887-
if self.manager.options.new_semantic_analyzer:
1888-
analyzer = self.manager.new_semantic_analyzer # type: Analyzer
1889-
else:
1882+
if not self.manager.options.new_semantic_analyzer:
18901883
analyzer = self.manager.semantic_analyzer
1891-
for dep in self.dependencies:
1892-
analyzer.add_submodules_to_parent_modules(dep, True)
1884+
for dep in self.dependencies:
1885+
analyzer.add_submodules_to_parent_modules(dep, True)
18931886

18941887
def fix_suppressed_dependencies(self, graph: Graph) -> None:
18951888
"""Corrects whether dependencies are considered stale in silent mode.

mypy/checker.py

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

1314
from mypy.errors import Errors, report_internal_error
1415
from mypy.nodes import (
@@ -69,11 +70,6 @@
6970
from mypy import state
7071
from mypy.traverser import has_return_statement
7172

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

7975
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/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def __init__(self, prog: str) -> None:
108108
help="Only produce suggestions that cause no errors")
109109
p.add_argument('--no-any', action='store_true',
110110
help="Only produce suggestions that don't contain Any")
111+
p.add_argument('--try-text', action='store_true',
112+
help="Try using unicode wherever str is inferred")
111113
p.add_argument('--callsites', action='store_true',
112114
help="Find callsites instead of suggesting a type")
113115

@@ -363,7 +365,7 @@ def do_suggest(args: argparse.Namespace) -> None:
363365
"""
364366
response = request(args.status_file, 'suggest', function=args.function,
365367
json=args.json, callsites=args.callsites, no_errors=args.no_errors,
366-
no_any=args.no_any)
368+
no_any=args.no_any, try_text=args.try_text)
367369
check_output(response, verbose=False, junit_xml=None, perf_stats_file=None)
368370

369371

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':

mypy/dmypy_util.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
import json
77

88
from typing import Any
9+
from typing_extensions import Final
910

1011
from mypy.ipc import IPCBase
1112

12-
MYPY = False
13-
if MYPY:
14-
from typing_extensions import Final
15-
1613
DEFAULT_STATUS_FILE = '.dmypy.json' # type: Final
1714

1815

mypy/errors.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
from collections import OrderedDict, defaultdict
55

66
from typing import Tuple, List, TypeVar, Set, Dict, Optional, TextIO
7+
from typing_extensions import Final
78

89
from mypy.scope import Scope
910
from mypy.options import Options
1011
from mypy.version import __version__ as mypy_version
1112

12-
MYPY = False
13-
if MYPY:
14-
from typing_extensions import Final
15-
1613
T = TypeVar('T')
1714
allowed_duplicates = ['@overload', 'Got:', 'Expected:'] # type: Final
1815

@@ -619,7 +616,7 @@ def report_internal_error(err: Exception,
619616
# Print "INTERNAL ERROR" message.
620617
print('{}error: INTERNAL ERROR --'.format(prefix),
621618
'Please try using mypy master on Github:\n'
622-
'https://mypy.rtfd.io/en/latest/common_issues.html#using-development-mypy-build',
619+
'https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build',
623620
file=stderr)
624621
if options.show_traceback:
625622
print('Please report a bug at https://github.com/python/mypy/issues',

mypy/fastparse.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import re
22
import sys
33

4+
import typing # for typing.Type, which conflicts with types.Type
45
from typing import (
56
Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, overload, Set
67
)
7-
MYPY = False
8-
if MYPY:
9-
import typing # for typing.Type, which conflicts with types.Type
10-
from typing_extensions import Final, Literal
8+
from typing_extensions import Final, Literal
119

1210
from mypy.sharedparse import (
1311
special_function_elide_names, argument_elide_name,

mypy/fastparse2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
"""
1717
import sys
1818

19+
import typing # for typing.Type, which conflicts with types.Type
1920
from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, Set
20-
MYPY = False
21-
if MYPY:
22-
import typing # for typing.Type, which conflicts with types.Type
23-
from typing_extensions import Final, Literal
21+
from typing_extensions import Final, Literal
2422

2523
from mypy.sharedparse import (
2624
special_function_elide_names, argument_elide_name,

mypy/find_sources.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
import os.path
44

55
from typing import List, Sequence, Set, Tuple, Optional, Dict
6+
from typing_extensions import Final
67

78
from mypy.modulefinder import BuildSource, PYTHON_EXTENSIONS
89
from mypy.fscache import FileSystemCache
910
from mypy.options import Options
1011

11-
MYPY = False
12-
if MYPY:
13-
from typing_extensions import Final
14-
1512
PY_EXTENSIONS = tuple(PYTHON_EXTENSIONS) # type: Final
1613

1714

mypy/interpreted_plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""Hack for handling non-mypyc compiled plugins with a mypyc-compiled mypy"""
22

33
from typing import Optional, Callable, Any, Dict, List, Tuple
4+
from typing_extensions import TYPE_CHECKING
5+
46
from mypy.options import Options
57
from mypy.types import Type, CallableType
68
from mypy.nodes import SymbolTableNode, MypyFile
79
from mypy.lookup import lookup_fully_qualified
810

9-
MYPY = False
10-
if MYPY:
11+
if TYPE_CHECKING:
1112
import mypy.plugin
1213

1314

mypy/ipc.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
import tempfile
1212

1313
from typing import Optional, Callable
14-
15-
MYPY = False
16-
if MYPY:
17-
from typing import Type
18-
from typing_extensions import Final
14+
from typing_extensions import Final, Type
1915

2016
from types import TracebackType
2117

0 commit comments

Comments
 (0)