Skip to content

Commit 356454a

Browse files
authored
[clean-strict-optional] Clean-up the type checking files (#3957)
This makes another major piece of mypy `--strict-optional` clean: the type checking files. After this PR there will be only single exception left `semanal.py`.
1 parent 9e4f50f commit 356454a

File tree

13 files changed

+150
-134
lines changed

13 files changed

+150
-134
lines changed

mypy/applytype.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Dict
1+
from typing import List, Dict, Sequence, Optional
22

33
import mypy.subtypes
44
from mypy.sametypes import is_same_type
@@ -8,7 +8,7 @@
88
from mypy.nodes import Context
99

1010

11-
def apply_generic_arguments(callable: CallableType, types: List[Type],
11+
def apply_generic_arguments(callable: CallableType, orig_types: Sequence[Optional[Type]],
1212
msg: MessageBuilder, context: Context) -> CallableType:
1313
"""Apply generic type arguments to a callable type.
1414
@@ -18,10 +18,10 @@ def apply_generic_arguments(callable: CallableType, types: List[Type],
1818
Note that each type can be None; in this case, it will not be applied.
1919
"""
2020
tvars = callable.variables
21-
assert len(tvars) == len(types)
21+
assert len(tvars) == len(orig_types)
2222
# Check that inferred type variable values are compatible with allowed
2323
# values and bounds. Also, promote subtype values to allowed values.
24-
types = types[:]
24+
types = list(orig_types)
2525
for i, type in enumerate(types):
2626
values = callable.variables[i].values
2727
if values and type:
@@ -47,8 +47,9 @@ def apply_generic_arguments(callable: CallableType, types: List[Type],
4747
# Create a map from type variable id to target type.
4848
id_to_type = {} # type: Dict[TypeVarId, Type]
4949
for i, tv in enumerate(tvars):
50-
if types[i]:
51-
id_to_type[tv.id] = types[i]
50+
typ = types[i]
51+
if typ:
52+
id_to_type[tv.id] = typ
5253

5354
# Apply arguments to argument types.
5455
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]

mypy/binder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def pop_frame(self, can_skip: bool, fall_through: int) -> Frame:
212212

213213
def assign_type(self, expr: Expression,
214214
type: Type,
215-
declared_type: Type,
215+
declared_type: Optional[Type],
216216
restrict_any: bool = False) -> None:
217217
if not isinstance(expr, BindableTypes):
218218
return None

mypy/checker.py

Lines changed: 50 additions & 44 deletions
Large diffs are not rendered by default.

mypy/checkexpr.py

Lines changed: 72 additions & 51 deletions
Large diffs are not rendered by default.

mypy/checkmember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class B(A): pass
630630
631631
"""
632632
if isinstance(method, Overloaded):
633-
return cast(F, Overloaded([bind_self(c, method) for c in method.items()]))
633+
return cast(F, Overloaded([bind_self(c, original_type) for c in method.items()]))
634634
assert isinstance(method, CallableType)
635635
func = method
636636
if not func.arg_types:

mypy/constraints.py

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

3-
from typing import Iterable, List, Optional
3+
from typing import Iterable, List, Optional, Sequence
44

55
from mypy import experiments
66
from mypy.types import (
@@ -42,7 +42,7 @@ def __repr__(self) -> str:
4242

4343

4444
def infer_constraints_for_callable(
45-
callee: CallableType, arg_types: List[Optional[Type]], arg_kinds: List[int],
45+
callee: CallableType, arg_types: Sequence[Optional[Type]], arg_kinds: List[int],
4646
formal_to_actual: List[List[int]]) -> List[Constraint]:
4747
"""Infer type variable constraints for a callable and actual arguments.
4848

mypy/fastparse.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
885885
join_method.set_line(empty_string)
886886
result_expression = CallExpr(join_method,
887887
[strs_to_join],
888-
[ARG_POS])
888+
[ARG_POS],
889+
[None])
889890
return result_expression
890891

891892
# FormattedValue(expr value)
@@ -902,7 +903,8 @@ def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
902903
format_method.set_line(format_string)
903904
result_expression = CallExpr(format_method,
904905
[exp],
905-
[ARG_POS])
906+
[ARG_POS],
907+
[None])
906908
return result_expression
907909

908910
# Bytes(bytes s)

mypy/infer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Utilities for type argument inference."""
22

3-
from typing import List, Optional
3+
from typing import List, Optional, Sequence
44

55
from mypy.constraints import infer_constraints, infer_constraints_for_callable
66
from mypy.types import Type, TypeVarId, CallableType
@@ -9,7 +9,7 @@
99

1010

1111
def infer_function_type_arguments(callee_type: CallableType,
12-
arg_types: List[Optional[Type]],
12+
arg_types: Sequence[Optional[Type]],
1313
arg_kinds: List[int],
1414
formal_to_actual: List[List[int]],
1515
strict: bool = True) -> List[Optional[Type]]:

mypy/messages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def enable_errors(self) -> None:
165165
def is_errors(self) -> bool:
166166
return self.errors.is_errors()
167167

168-
def report(self, msg: str, context: Context, severity: str,
168+
def report(self, msg: str, context: Optional[Context], severity: str,
169169
file: Optional[str] = None, origin: Optional[Context] = None,
170170
offset: int = 0) -> None:
171171
"""Report an error or note (unless disabled)."""
@@ -175,7 +175,7 @@ def report(self, msg: str, context: Context, severity: str,
175175
msg.strip(), severity=severity, file=file, offset=offset,
176176
origin_line=origin.get_line() if origin else None)
177177

178-
def fail(self, msg: str, context: Context, file: Optional[str] = None,
178+
def fail(self, msg: str, context: Optional[Context], file: Optional[str] = None,
179179
origin: Optional[Context] = None) -> None:
180180
"""Report an error message (unless disabled)."""
181181
self.report(msg, context, 'error', file=file, origin=origin)
@@ -641,7 +641,7 @@ def invalid_index_type(self, index_type: Type, expected_type: Type, base_str: st
641641
self.format(index_type), base_str, self.format(expected_type)), context)
642642

643643
def too_few_arguments(self, callee: CallableType, context: Context,
644-
argument_names: List[str]) -> None:
644+
argument_names: Optional[Sequence[Optional[str]]]) -> None:
645645
if (argument_names is not None and not all(k is None for k in argument_names)
646646
and len(argument_names) >= 1):
647647
diff = [k for k in callee.arg_names if k not in argument_names]
@@ -695,7 +695,7 @@ def duplicate_argument_value(self, callee: CallableType, index: int,
695695
format(capitalize(callable_name(callee)),
696696
callee.arg_names[index]), context)
697697

698-
def does_not_return_value(self, callee_type: Type, context: Context) -> None:
698+
def does_not_return_value(self, callee_type: Optional[Type], context: Context) -> None:
699699
"""Report an error about use of an unusable type."""
700700
name = None # type: Optional[str]
701701
if isinstance(callee_type, FunctionLike):

mypy/myunit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, name: str, suite: 'Optional[Suite]' = None,
112112
self.name = name
113113
self.suite = suite
114114
self.old_cwd = None # type: Optional[str]
115-
self.tmpdir = None # type: Optional[tempfile.TemporaryDirectory]
115+
self.tmpdir = None # type: Optional[tempfile.TemporaryDirectory[str]]
116116

117117
def run(self) -> None:
118118
if self.func:

0 commit comments

Comments
 (0)