Skip to content

Commit

Permalink
Use some more dogfooding (python#7027)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ilevkivskyi authored Jun 20, 2019
1 parent 47e68e2 commit 23dba19
Show file tree
Hide file tree
Showing 59 changed files with 101 additions and 278 deletions.
11 changes: 3 additions & 8 deletions mypy/binder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
from contextlib import contextmanager
from collections import defaultdict

MYPY = False
if MYPY:
from typing import DefaultDict
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
from typing_extensions import DefaultDict

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


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


class ConditionalTypeBinder:
Expand Down
8 changes: 2 additions & 6 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@

from typing import (AbstractSet, Any, Dict, Iterable, Iterator, List,
Mapping, NamedTuple, Optional, Set, Tuple, Union, Callable, TextIO)
MYPY = False
if MYPY:
from typing import ClassVar
from typing_extensions import Final

from typing_extensions import ClassVar, Final, TYPE_CHECKING
from mypy_extensions import TypedDict

from mypy.nodes import MypyFile, ImportBase, Import, ImportFrom, ImportAll, SymbolTable
Expand All @@ -45,7 +41,7 @@
from mypy.util import (
DecodeError, decode_python_encoding, is_sub_path, get_mypy_comments, module_prefix
)
if MYPY:
if TYPE_CHECKING:
from mypy.report import Reports # Avoid unconditional slow import
from mypy import moduleinfo
from mypy.fixup import fixup_module
Expand Down
6 changes: 1 addition & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator, Iterable,
Sequence
)
from typing_extensions import Final

from mypy.errors import Errors, report_internal_error
from mypy.nodes import (
Expand Down Expand Up @@ -68,11 +69,6 @@
from mypy import state
from mypy.traverser import has_return_statement

MYPY = False
if MYPY:
from typing_extensions import Final


T = TypeVar('T')

DEFAULT_LAST_PASS = 1 # type: Final # Pass numbers start at 0
Expand Down
5 changes: 1 addition & 4 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from typing import (
cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator
)
MYPY = False
if MYPY:
from typing import ClassVar
from typing_extensions import Final
from typing_extensions import ClassVar, Final

from mypy.errors import report_internal_error
from mypy.typeanal import (
Expand Down
4 changes: 2 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Type checking of attribute access"""

from typing import cast, Callable, List, Optional, TypeVar, Union
from typing_extensions import TYPE_CHECKING

from mypy.types import (
Type, Instance, AnyType, TupleType, TypedDictType, CallableType, FunctionLike, TypeVarDef,
Expand All @@ -25,8 +26,7 @@
from mypy import meet
from mypy.typeops import tuple_fallback

MYPY = False
if MYPY: # import for forward declaration only
if TYPE_CHECKING: # import for forward declaration only
import mypy.checker

from mypy import state
Expand Down
5 changes: 2 additions & 3 deletions mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from typing import cast, List, Tuple, Dict, Callable, Union, Optional, Pattern
from typing_extensions import Final, TYPE_CHECKING

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

MYPY = False
if MYPY:
if TYPE_CHECKING:
# break import cycle only needed for mypy
import mypy.checker
import mypy.checkexpr
from typing_extensions import Final
from mypy import message_registry
from mypy.messages import MessageBuilder

Expand Down
10 changes: 3 additions & 7 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
import re
import sys

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

from typing import Any, Dict, List, Mapping, Optional, Tuple, TextIO
from typing_extensions import Final


MYPY = False
if MYPY:
from typing_extensions import Final
from mypy import defaults
from mypy.options import Options, PER_MODULE_OPTIONS


def parse_version(v: str) -> Tuple[int, int]:
Expand Down
6 changes: 1 addition & 5 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Type inference constraints."""

from typing import Iterable, List, Optional, Sequence
from typing_extensions import Final

from mypy.types import (
CallableType, Type, TypeVisitor, UnboundType, AnyType, NoneType, TypeVarType, Instance,
Expand All @@ -15,11 +16,6 @@
from mypy.nodes import COVARIANT, CONTRAVARIANT
from mypy.argmap import ArgTypeExpander

MYPY = False
if MYPY:
from typing_extensions import Final


SUBTYPE_OF = 0 # type: Final[int]
SUPERTYPE_OF = 1 # type: Final[int]

Expand Down
4 changes: 1 addition & 3 deletions mypy/defaults.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

MYPY = False
if MYPY:
from typing_extensions import Final
from typing_extensions import Final

PYTHON2_VERSION = (2, 7) # type: Final
PYTHON3_VERSION = (3, 6) # type: Final
Expand Down
6 changes: 1 addition & 5 deletions mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from contextlib import redirect_stderr, redirect_stdout

from typing import AbstractSet, Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple
from typing_extensions import Final

import mypy.build
import mypy.errors
Expand All @@ -33,11 +34,6 @@
from mypy.typestate import reset_global_state
from mypy.version import __version__


MYPY = False
if MYPY:
from typing_extensions import Final

MEM_PROFILE = False # type: Final # If True, dump memory profile after initialization

if sys.platform == 'win32':
Expand Down
5 changes: 1 addition & 4 deletions mypy/dmypy_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import json

from typing import Any
from typing_extensions import Final

from mypy.ipc import IPCBase

MYPY = False
if MYPY:
from typing_extensions import Final

DEFAULT_STATUS_FILE = '.dmypy.json' # type: Final


Expand Down
5 changes: 1 addition & 4 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
from collections import OrderedDict, defaultdict

from typing import Tuple, List, TypeVar, Set, Dict, Optional, TextIO
from typing_extensions import Final

from mypy.scope import Scope
from mypy.options import Options
from mypy.version import __version__ as mypy_version

MYPY = False
if MYPY:
from typing_extensions import Final

T = TypeVar('T')
allowed_duplicates = ['@overload', 'Got:', 'Expected:'] # type: Final

Expand Down
6 changes: 2 additions & 4 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import re
import sys

import typing # for typing.Type, which conflicts with types.Type
from typing import (
Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, overload, Set
)
MYPY = False
if MYPY:
import typing # for typing.Type, which conflicts with types.Type
from typing_extensions import Final, Literal
from typing_extensions import Final, Literal

from mypy.sharedparse import (
special_function_elide_names, argument_elide_name,
Expand Down
6 changes: 2 additions & 4 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
"""
import sys

import typing # for typing.Type, which conflicts with types.Type
from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, Set
MYPY = False
if MYPY:
import typing # for typing.Type, which conflicts with types.Type
from typing_extensions import Final, Literal
from typing_extensions import Final, Literal

from mypy.sharedparse import (
special_function_elide_names, argument_elide_name,
Expand Down
5 changes: 1 addition & 4 deletions mypy/find_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import os.path

from typing import List, Sequence, Set, Tuple, Optional, Dict
from typing_extensions import Final

from mypy.modulefinder import BuildSource, PYTHON_EXTENSIONS
from mypy.fscache import FileSystemCache
from mypy.options import Options

MYPY = False
if MYPY:
from typing_extensions import Final

PY_EXTENSIONS = tuple(PYTHON_EXTENSIONS) # type: Final


Expand Down
5 changes: 3 additions & 2 deletions mypy/interpreted_plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Hack for handling non-mypyc compiled plugins with a mypyc-compiled mypy"""

from typing import Optional, Callable, Any, Dict, List, Tuple
from typing_extensions import TYPE_CHECKING

from mypy.options import Options
from mypy.types import Type, CallableType
from mypy.nodes import SymbolTableNode, MypyFile
from mypy.lookup import lookup_fully_qualified

MYPY = False
if MYPY:
if TYPE_CHECKING:
import mypy.plugin


Expand Down
6 changes: 1 addition & 5 deletions mypy/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
import tempfile

from typing import Optional, Callable

MYPY = False
if MYPY:
from typing import Type
from typing_extensions import Final
from typing_extensions import Final, Type

from types import TracebackType

Expand Down
5 changes: 1 addition & 4 deletions mypy/literals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional, Union, Any, Tuple, Iterable
from typing_extensions import Final

from mypy.nodes import (
Expression, ComparisonExpr, OpExpr, MemberExpr, UnaryExpr, StarExpr, IndexExpr, LITERAL_YES,
Expand All @@ -11,10 +12,6 @@
)
from mypy.visitor import ExpressionVisitor

MYPY = False
if MYPY:
from typing_extensions import Final

# [Note Literals and literal_hash]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
Expand Down
7 changes: 1 addition & 6 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time

from typing import Any, Dict, List, Optional, Tuple, TextIO
from typing_extensions import Final

from mypy import build
from mypy import defaults
Expand All @@ -22,13 +23,7 @@

from mypy.version import __version__

MYPY = False
if MYPY:
from typing_extensions import Final


orig_stat = os.stat # type: Final

MEM_PROFILE = False # type: Final # If True, dump memory profile


Expand Down
10 changes: 2 additions & 8 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
add a method to MessageBuilder and call this instead.
"""

MYPY = False
if MYPY:
from typing_extensions import Final

from typing_extensions import Final

# Invalid types

INVALID_TYPE_RAW_ENUM_VALUE = "Invalid type: try using Literal[{}.{}] instead?" # type: Final


# Type checker error message constants --

# Type checker error message constants
NO_RETURN_VALUE_EXPECTED = 'No return value expected' # type: Final
MISSING_RETURN_STATEMENT = 'Missing return statement' # type: Final
INVALID_IMPLICIT_RETURN = 'Implicit return in function which does not return' # type: Final
Expand Down
6 changes: 1 addition & 5 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from textwrap import dedent

from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
from typing_extensions import Final

from mypy.erasetype import erase_type
from mypy.errors import Errors
Expand All @@ -32,11 +33,6 @@
from mypy.util import unmangle
from mypy import message_registry

MYPY = False
if MYPY:
from typing_extensions import Final


ARG_CONSTRUCTOR_NAMES = {
ARG_POS: "Arg",
ARG_OPT: "DefaultArg",
Expand Down
5 changes: 1 addition & 4 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import sys

from typing import Dict, List, NamedTuple, Optional, Set, Tuple

MYPY = False
if MYPY:
from typing_extensions import Final
from typing_extensions import Final

from mypy.defaults import PYTHON3_VERSION_MIN
from mypy.fscache import FileSystemCache
Expand Down
Loading

0 comments on commit 23dba19

Please sign in to comment.