Skip to content

Commit 88c8cc6

Browse files
authored
More tweaks to support mypyc (#5487)
1 parent 5700456 commit 88c8cc6

File tree

5 files changed

+65
-36
lines changed

5 files changed

+65
-36
lines changed

mypy/checkstrformat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def compile_format_re() -> Pattern[str]:
3636

3737

3838
class ConversionSpecifier:
39-
def __init__(self, key: str, flags: str, width: str, precision: str, type: str) -> None:
39+
def __init__(self, key: Optional[str],
40+
flags: str, width: str, precision: str, type: str) -> None:
4041
self.key = key
4142
self.flags = flags
4243
self.width = width
@@ -179,6 +180,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
179180
if specifier.type == '%':
180181
# %% is allowed in mappings, no checking is required
181182
continue
183+
assert specifier.key is not None
182184
if specifier.key not in mapping:
183185
self.msg.key_not_in_mapping(specifier.key, replacements)
184186
return

mypy/errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class Errors:
106106
import_ctx = None # type: List[Tuple[str, int]]
107107

108108
# Path name prefix that is removed from all paths, if set.
109-
ignore_prefix = None # type: str
109+
ignore_prefix = None # type: Optional[str]
110110

111111
# Path to current file.
112-
file = None # type: str
112+
file = '' # type: str
113113

114114
# Ignore errors on these lines of each file.
115115
ignored_lines = None # type: Dict[str, Set[int]]
@@ -548,7 +548,7 @@ def __init__(self,
548548
self.module_with_blocker = module_with_blocker
549549

550550

551-
def remove_path_prefix(path: str, prefix: str) -> str:
551+
def remove_path_prefix(path: str, prefix: Optional[str]) -> str:
552552
"""If path starts with prefix, return copy of path with the prefix removed.
553553
Otherwise, return path. If path is None, return None.
554554
"""

mypy/indirection.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Iterable, List, Optional, Set
1+
from typing import Dict, Iterable, List, Optional, Set, Union
22

33
from mypy.types import SyntheticTypeVisitor
44
import mypy.types as types
@@ -22,9 +22,10 @@ def __init__(self) -> None:
2222
self.cache = {} # type: Dict[types.Type, Set[str]]
2323

2424
def find_modules(self, typs: Iterable[types.Type]) -> Set[str]:
25-
return self._visit(*typs)
25+
return self._visit(typs)
2626

27-
def _visit(self, *typs: types.Type) -> Set[str]:
27+
def _visit(self, typ_or_typs: Union[types.Type, Iterable[types.Type]]) -> Set[str]:
28+
typs = [typ_or_typs] if isinstance(typ_or_typs, types.Type) else typ_or_typs
2829
output = set() # type: Set[str]
2930
for typ in typs:
3031
if typ in self.cache:
@@ -36,10 +37,10 @@ def _visit(self, *typs: types.Type) -> Set[str]:
3637
return output
3738

3839
def visit_unbound_type(self, t: types.UnboundType) -> Set[str]:
39-
return self._visit(*t.args)
40+
return self._visit(t.args)
4041

4142
def visit_type_list(self, t: types.TypeList) -> Set[str]:
42-
return self._visit(*t.items)
43+
return self._visit(t.items)
4344

4445
def visit_callable_argument(self, t: types.CallableArgument) -> Set[str]:
4546
return self._visit(t.typ)
@@ -60,10 +61,10 @@ def visit_deleted_type(self, t: types.DeletedType) -> Set[str]:
6061
return set()
6162

6263
def visit_type_var(self, t: types.TypeVarType) -> Set[str]:
63-
return self._visit(*t.values) | self._visit(t.upper_bound)
64+
return self._visit(t.values) | self._visit(t.upper_bound)
6465

6566
def visit_instance(self, t: types.Instance) -> Set[str]:
66-
out = self._visit(*t.args)
67+
out = self._visit(t.args)
6768
if t.type:
6869
# Uses of a class depend on everything in the MRO,
6970
# as changes to classes in the MRO can add types to methods,
@@ -75,25 +76,25 @@ def visit_instance(self, t: types.Instance) -> Set[str]:
7576
return out
7677

7778
def visit_callable_type(self, t: types.CallableType) -> Set[str]:
78-
out = self._visit(*t.arg_types) | self._visit(t.ret_type)
79+
out = self._visit(t.arg_types) | self._visit(t.ret_type)
7980
if t.definition is not None:
8081
out.update(extract_module_names(t.definition.fullname()))
8182
return out
8283

8384
def visit_overloaded(self, t: types.Overloaded) -> Set[str]:
84-
return self._visit(*t.items()) | self._visit(t.fallback)
85+
return self._visit(t.items()) | self._visit(t.fallback)
8586

8687
def visit_tuple_type(self, t: types.TupleType) -> Set[str]:
87-
return self._visit(*t.items) | self._visit(t.fallback)
88+
return self._visit(t.items) | self._visit(t.fallback)
8889

8990
def visit_typeddict_type(self, t: types.TypedDictType) -> Set[str]:
90-
return self._visit(*t.items.values()) | self._visit(t.fallback)
91+
return self._visit(t.items.values()) | self._visit(t.fallback)
9192

9293
def visit_star_type(self, t: types.StarType) -> Set[str]:
9394
return set()
9495

9596
def visit_union_type(self, t: types.UnionType) -> Set[str]:
96-
return self._visit(*t.items)
97+
return self._visit(t.items)
9798

9899
def visit_partial_type(self, t: types.PartialType) -> Set[str]:
99100
return set()

mypy/server/subexpr.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,50 @@ class SubexpressionFinder(TraverserVisitor):
2121
def __init__(self) -> None:
2222
self.expressions = [] # type: List[Expression]
2323

24-
def _visit_leaf(self, o: Expression) -> None:
24+
def visit_int_expr(self, o: Expression) -> None:
2525
self.add(o)
2626

27-
visit_int_expr = _visit_leaf
28-
visit_name_expr = _visit_leaf
29-
visit_float_expr = _visit_leaf
30-
visit_str_expr = _visit_leaf
31-
visit_bytes_expr = _visit_leaf
32-
visit_unicode_expr = _visit_leaf
33-
visit_complex_expr = _visit_leaf
34-
visit_ellipsis = _visit_leaf
35-
visit_super_expr = _visit_leaf
36-
visit_type_var_expr = _visit_leaf
37-
visit_type_alias_expr = _visit_leaf
38-
visit_namedtuple_expr = _visit_leaf
39-
visit_typeddict_expr = _visit_leaf
40-
visit__promote_expr = _visit_leaf
41-
visit_newtype_expr = _visit_leaf
27+
def visit_name_expr(self, o: Expression) -> None:
28+
self.add(o)
29+
30+
def visit_float_expr(self, o: Expression) -> None:
31+
self.add(o)
32+
33+
def visit_str_expr(self, o: Expression) -> None:
34+
self.add(o)
35+
36+
def visit_bytes_expr(self, o: Expression) -> None:
37+
self.add(o)
38+
39+
def visit_unicode_expr(self, o: Expression) -> None:
40+
self.add(o)
41+
42+
def visit_complex_expr(self, o: Expression) -> None:
43+
self.add(o)
44+
45+
def visit_ellipsis(self, o: Expression) -> None:
46+
self.add(o)
47+
48+
def visit_super_expr(self, o: Expression) -> None:
49+
self.add(o)
50+
51+
def visit_type_var_expr(self, o: Expression) -> None:
52+
self.add(o)
53+
54+
def visit_type_alias_expr(self, o: Expression) -> None:
55+
self.add(o)
56+
57+
def visit_namedtuple_expr(self, o: Expression) -> None:
58+
self.add(o)
59+
60+
def visit_typeddict_expr(self, o: Expression) -> None:
61+
self.add(o)
62+
63+
def visit__promote_expr(self, o: Expression) -> None:
64+
self.add(o)
65+
66+
def visit_newtype_expr(self, o: Expression) -> None:
67+
self.add(o)
4268

4369
def visit_member_expr(self, e: MemberExpr) -> None:
4470
self.add(e)

mypy/typestate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,29 @@ class TypeState:
5151
# A blocking error will be generated in this case, since we can't proceed safely.
5252
# For the description of kinds of protocol dependencies and corresponding examples,
5353
# see _snapshot_protocol_deps.
54-
proto_deps = {} # type: Optional[Dict[str, Set[str]]]
54+
proto_deps = {} # type: ClassVar[Optional[Dict[str, Set[str]]]]
5555

5656
# Protocols (full names) a given class attempted to implement.
5757
# Used to calculate fine grained protocol dependencies and optimize protocol
5858
# subtype cache invalidation in fine grained mode. For example, if we pass a value
5959
# of type a.A to a function expecting something compatible with protocol p.P,
6060
# we'd have 'a.A' -> {'p.P', ...} in the map. This map is flushed after every incremental
6161
# update.
62-
_attempted_protocols = {} # type: Dict[str, Set[str]]
62+
_attempted_protocols = {} # type: ClassVar[Dict[str, Set[str]]]
6363
# We also snapshot protocol members of the above protocols. For example, if we pass
6464
# a value of type a.A to a function expecting something compatible with Iterable, we'd have
6565
# 'a.A' -> {'__iter__', ...} in the map. This map is also flushed after every incremental
6666
# update. This map is needed to only generate dependencies like <a.A.__iter__> -> <a.A>
6767
# instead of a wildcard to avoid unnecessarily invalidating classes.
68-
_checked_against_members = {} # type: Dict[str, Set[str]]
68+
_checked_against_members = {} # type: ClassVar[Dict[str, Set[str]]]
6969
# TypeInfos that appeared as a left type (subtype) in a subtype check since latest
7070
# dependency snapshot update. This is an optimisation for fine grained mode; during a full
7171
# run we only take a dependency snapshot at the very end, so this set will contain all
7272
# subtype-checked TypeInfos. After a fine grained update however, we can gather only new
7373
# dependencies generated from (typically) few TypeInfos that were subtype-checked
7474
# (i.e. appeared as r.h.s. in an assignment or an argument in a function call in
7575
# a re-checked target) during the update.
76-
_rechecked_types = set() # type: Set[TypeInfo]
76+
_rechecked_types = set() # type: ClassVar[Set[TypeInfo]]
7777

7878
@classmethod
7979
def reset_all_subtype_caches(cls) -> None:

0 commit comments

Comments
 (0)