Skip to content

Commit

Permalink
Delete unused backports and other various cleanups (#13344)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilevkivskyi committed Aug 6, 2022
1 parent 48ae9d5 commit 11fe165
Show file tree
Hide file tree
Showing 37 changed files with 138 additions and 205 deletions.
20 changes: 0 additions & 20 deletions mypy/backports.py

This file was deleted.

3 changes: 2 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,8 +2336,9 @@ def type_check_second_pass(self) -> bool:
return False
t0 = time_ref()
with self.wrap_context():
return self.type_checker().check_second_pass()
result = self.type_checker().check_second_pass()
self.time_spent_us += time_spent_us(t0)
return result

def finish_passes(self) -> None:
assert self.tree is not None, "Internal error: method must be called on parsed file only"
Expand Down
3 changes: 1 addition & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fnmatch
import itertools
from collections import defaultdict
from contextlib import contextmanager
from contextlib import contextmanager, nullcontext
from typing import (
AbstractSet,
Any,
Expand All @@ -29,7 +29,6 @@

import mypy.checkexpr
from mypy import errorcodes as codes, message_registry, nodes, operators
from mypy.backports import nullcontext
from mypy.binder import ConditionalTypeBinder, get_declaration
from mypy.checkmember import (
MemberContext,
Expand Down
22 changes: 7 additions & 15 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import mypy.errorcodes as codes
from mypy import applytype, erasetype, join, message_registry, nodes, operators, types
from mypy.argmap import ArgTypeExpander, map_actuals_to_formals, map_formals_to_actuals
from mypy.backports import OrderedDict
from mypy.checkmember import analyze_member_access, type_object_type
from mypy.checkstrformat import StringFormatterChecker
from mypy.erasetype import erase_type, remove_instance_last_known_values, replace_meta_vars
Expand Down Expand Up @@ -624,7 +623,7 @@ def check_typeddict_call(
item_names = cast(List[str], arg_names)
item_args = args
return self.check_typeddict_call_with_kwargs(
callee, OrderedDict(zip(item_names, item_args)), context
callee, dict(zip(item_names, item_args)), context
)

if len(args) == 1 and arg_kinds[0] == ARG_POS:
Expand All @@ -638,14 +637,12 @@ def check_typeddict_call(

if len(args) == 0:
# ex: EmptyDict()
return self.check_typeddict_call_with_kwargs(callee, OrderedDict(), context)
return self.check_typeddict_call_with_kwargs(callee, {}, context)

self.chk.fail(message_registry.INVALID_TYPEDDICT_ARGS, context)
return AnyType(TypeOfAny.from_error)

def validate_typeddict_kwargs(
self, kwargs: DictExpr
) -> "Optional[OrderedDict[str, Expression]]":
def validate_typeddict_kwargs(self, kwargs: DictExpr) -> "Optional[Dict[str, Expression]]":
item_args = [item[1] for item in kwargs.items]

item_names = [] # List[str]
Expand All @@ -662,7 +659,7 @@ def validate_typeddict_kwargs(
return None
else:
item_names.append(literal_value)
return OrderedDict(zip(item_names, item_args))
return dict(zip(item_names, item_args))

def match_typeddict_call_with_dict(
self, callee: TypedDictType, kwargs: DictExpr, context: Context
Expand All @@ -685,7 +682,7 @@ def check_typeddict_call_with_dict(
return AnyType(TypeOfAny.from_error)

def check_typeddict_call_with_kwargs(
self, callee: TypedDictType, kwargs: "OrderedDict[str, Expression]", context: Context
self, callee: TypedDictType, kwargs: Dict[str, Expression], context: Context
) -> Type:
if not (callee.required_keys <= set(kwargs.keys()) <= set(callee.items.keys())):
expected_keys = [
Expand Down Expand Up @@ -3137,10 +3134,7 @@ def check_op(
base_type = get_proper_type(base_type)
if isinstance(base_type, UnionType):
left_variants = [
item
for item in flatten_nested_unions(
base_type.relevant_items(), handle_type_alias_type=True
)
item for item in flatten_nested_unions(base_type.relevant_items())
]
right_type = self.accept(arg)

Expand Down Expand Up @@ -3184,9 +3178,7 @@ def check_op(
if isinstance(right_type, UnionType):
right_variants = [
(item, TempNode(item, context=context))
for item in flatten_nested_unions(
right_type.relevant_items(), handle_type_alias_type=True
)
for item in flatten_nested_unions(right_type.relevant_items())
]

all_results = []
Expand Down
2 changes: 0 additions & 2 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,6 @@ def generate_types_from_names(self, type_names: List[str]) -> List[Type]:
# Some built in types are not defined in all test cases
if not name.startswith("builtins."):
raise e
pass

return types

def update_type_map(
Expand Down
80 changes: 40 additions & 40 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing_extensions import Final, Literal, NoReturn

from mypy import errorcodes as codes
from mypy.backports import OrderedDict
from mypy.errorcodes import IMPORT, ErrorCode
from mypy.message_registry import ErrorMessage
from mypy.options import Options
Expand Down Expand Up @@ -154,7 +153,8 @@ def __enter__(self) -> "ErrorWatcher":
return self

def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> Literal[False]:
assert self == self.errors._watchers.pop()
last = self.errors._watchers.pop()
assert last == self
return False

def on_error(self, file: str, info: ErrorInfo) -> bool:
Expand Down Expand Up @@ -279,11 +279,11 @@ def __init__(
self.initialize()

def initialize(self) -> None:
self.error_info_map = OrderedDict()
self.error_info_map = {}
self.flushed_files = set()
self.import_ctx = []
self.function_or_member = [None]
self.ignored_lines = OrderedDict()
self.ignored_lines = {}
self.used_ignored_lines = defaultdict(lambda: defaultdict(list))
self.ignored_files = set()
self.only_once_messages = set()
Expand Down Expand Up @@ -606,43 +606,43 @@ def clear_errors_in_targets(self, path: str, targets: Set[str]) -> None:
self.has_blockers.remove(path)

def generate_unused_ignore_errors(self, file: str) -> None:
if is_typeshed_file(file) or file in self.ignored_files:
return
ignored_lines = self.ignored_lines[file]
if not is_typeshed_file(file) and file not in self.ignored_files:
ignored_lines = self.ignored_lines[file]
used_ignored_lines = self.used_ignored_lines[file]
for line, ignored_codes in ignored_lines.items():
used_ignored_codes = used_ignored_lines[line]
unused_ignored_codes = set(ignored_codes) - set(used_ignored_codes)
# `ignore` is used
if len(ignored_codes) == 0 and len(used_ignored_codes) > 0:
continue
# All codes appearing in `ignore[...]` are used
if len(ignored_codes) > 0 and len(unused_ignored_codes) == 0:
continue
# Display detail only when `ignore[...]` specifies more than one error code
unused_codes_message = ""
if len(ignored_codes) > 1 and len(unused_ignored_codes) > 0:
unused_codes_message = f"[{', '.join(sorted(unused_ignored_codes))}]"
message = f'Unused "type: ignore{unused_codes_message}" comment'
# Don't use report since add_error_info will ignore the error!
info = ErrorInfo(
self.import_context(),
file,
self.current_module(),
None,
None,
line,
-1,
line,
-1,
"error",
message,
None,
False,
False,
False,
)
self._add_error_info(file, info)
used_ignored_lines = self.used_ignored_lines[file]
for line, ignored_codes in ignored_lines.items():
used_ignored_codes = used_ignored_lines[line]
unused_ignored_codes = set(ignored_codes) - set(used_ignored_codes)
# `ignore` is used
if len(ignored_codes) == 0 and len(used_ignored_codes) > 0:
continue
# All codes appearing in `ignore[...]` are used
if len(ignored_codes) > 0 and len(unused_ignored_codes) == 0:
continue
# Display detail only when `ignore[...]` specifies more than one error code
unused_codes_message = ""
if len(ignored_codes) > 1 and len(unused_ignored_codes) > 0:
unused_codes_message = f"[{', '.join(sorted(unused_ignored_codes))}]"
message = f'Unused "type: ignore{unused_codes_message}" comment'
# Don't use report since add_error_info will ignore the error!
info = ErrorInfo(
self.import_context(),
file,
self.current_module(),
None,
None,
line,
-1,
line,
-1,
"error",
message,
None,
False,
False,
False,
)
self._add_error_info(file, info)

def generate_ignore_without_code_errors(
self, file: str, is_warning_unused_ignores: bool
Expand Down
1 change: 0 additions & 1 deletion mypy/inspections.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def find_module_by_fullname(fullname: str, modules: Dict[str, State]) -> Optiona
mod = modules.get(head)
if mod is not None:
return mod
return None


class SearchVisitor(ExtendedTraverserVisitor):
Expand Down
19 changes: 8 additions & 11 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import List, Optional, Tuple

import mypy.typeops
from mypy.backports import OrderedDict
from mypy.maptype import map_instance_to_supertype
from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT
from mypy.state import state
Expand Down Expand Up @@ -447,16 +446,14 @@ def visit_tuple_type(self, t: TupleType) -> ProperType:

def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
if isinstance(self.s, TypedDictType):
items = OrderedDict(
[
(item_name, s_item_type)
for (item_name, s_item_type, t_item_type) in self.s.zip(t)
if (
is_equivalent(s_item_type, t_item_type)
and (item_name in t.required_keys) == (item_name in self.s.required_keys)
)
]
)
items = {
item_name: s_item_type
for (item_name, s_item_type, t_item_type) in self.s.zip(t)
if (
is_equivalent(s_item_type, t_item_type)
and (item_name in t.required_keys) == (item_name in self.s.required_keys)
)
}
fallback = self.s.create_anonymous_fallback()
# We need to filter by items.keys() since some required keys present in both t and
# self.s might be missing from the join if the types are incompatible.
Expand Down
2 changes: 1 addition & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def add_invertible_flag(
flag, action="store_false" if default else "store_true", dest=dest, help=help
)
dest = arg.dest
arg = group.add_argument(
group.add_argument(
inverse,
action="store_true" if default else "store_false",
dest=dest,
Expand Down
3 changes: 1 addition & 2 deletions mypy/meet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Callable, List, Optional, Tuple

from mypy import join
from mypy.backports import OrderedDict
from mypy.erasetype import erase_type
from mypy.maptype import map_instance_to_supertype
from mypy.state import state
Expand Down Expand Up @@ -776,7 +775,7 @@ def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
# at least one of s_item_type and t_item_type is not None
assert t_item_type is not None
item_list.append((item_name, t_item_type))
items = OrderedDict(item_list)
items = dict(item_list)
fallback = self.s.create_anonymous_fallback()
required_keys = t.required_keys | self.s.required_keys
return TypedDictType(items, required_keys, fallback)
Expand Down
3 changes: 1 addition & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from typing_extensions import Final

from mypy import errorcodes as codes, message_registry
from mypy.backports import OrderedDict
from mypy.erasetype import erase_type
from mypy.errorcodes import ErrorCode
from mypy.errors import ErrorInfo, Errors, ErrorWatcher
Expand Down Expand Up @@ -1511,7 +1510,7 @@ def reveal_type(self, typ: Type, context: Context) -> None:
def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -> None:
# To ensure that the output is predictable on Python < 3.6,
# use an ordered dictionary sorted by variable name
sorted_locals = OrderedDict(sorted(type_map.items(), key=lambda t: t[0]))
sorted_locals = dict(sorted(type_map.items(), key=lambda t: t[0]))
if sorted_locals:
self.note("Revealed local types are:", context)
for k, v in sorted_locals.items():
Expand Down
3 changes: 1 addition & 2 deletions mypy/mro.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ def linearize_hierarchy(
return info.mro
bases = info.direct_base_classes()
if not bases and info.fullname != "builtins.object" and obj_type is not None:
# Second pass in import cycle, add a dummy `object` base class,
# Probably an error, add a dummy `object` base class,
# otherwise MRO calculation may spuriously fail.
# MRO will be re-calculated for real in the third pass.
bases = [obj_type().type]
lin_bases = []
for base in bases:
Expand Down
Loading

0 comments on commit 11fe165

Please sign in to comment.