Skip to content

Commit ad6b4f6

Browse files
Michael0x2aJukkaL
authored andcommitted
Remove some "if type is None" style checks (#7120)
This pull request is a continuation of my quest to make mypy cleanly type-check under the `--warn-unreachable` flag. It removes some unnecessary checks we're performing against Type-related variables. Specifically: 1. We had some old code that checked if `t.accept(TypeJoinVisitor(s))` would return None. I believe this check was added back in 2014: after auditing our type visitors, it no longer seems relevant today. 2. We had a few unnecessary `if some_instance.type is not None` style checks. I believe we always set the TypeInfo when constructing Instance classes these days, so this check also seems unnecessary. 3. Finally, we had a "if TypeInfo.bases does not actually contain Instances" style check around some protocol-related code. I'm not really sure why, but TypeInfo.bases is annotated to be of type `List[Instance]` so it should be fine to remove these checks as well.
1 parent d1b6f7f commit ad6b4f6

File tree

5 files changed

+10
-31
lines changed

5 files changed

+10
-31
lines changed

mypy/join.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ def join_simple(declaration: Optional[Type], s: Type, t: Type) -> Type:
4848
s, t = t, s
4949

5050
value = t.accept(TypeJoinVisitor(s))
51-
52-
if value is None:
53-
# XXX this code path probably should be avoided.
54-
# It seems to happen when a line (x = y) is a type error, and
55-
# it's not clear that assuming that x is arbitrary afterward
56-
# is a good idea.
57-
return declaration
58-
5951
if declaration is None or is_subtype(value, declaration):
6052
return value
6153

mypy/newsemanal/semanal_classprop.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ def check_protocol_status(info: TypeInfo, errors: Errors) -> None:
113113
"""Check that all classes in MRO of a protocol are protocols"""
114114
if info.is_protocol:
115115
for type in info.bases:
116-
if not isinstance(type, Instance) or not type.type.is_protocol:
117-
if type.type.fullname() != 'builtins.object':
118-
def report(message: str, severity: str) -> None:
119-
errors.report(info.line, info.column, message, severity=severity)
120-
report('All bases of a protocol must be protocols', 'error')
116+
if not type.type.is_protocol and type.type.fullname() != 'builtins.object':
117+
def report(message: str, severity: str) -> None:
118+
errors.report(info.line, info.column, message, severity=severity)
119+
report('All bases of a protocol must be protocols', 'error')
121120

122121

123122
def calculate_class_vars(info: TypeInfo) -> None:

mypy/semanal_pass3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ def visit_class_def(self, tdef: ClassDef) -> None:
176176
self.analyze_types(types, tdef.info)
177177
for type in tdef.info.bases:
178178
if tdef.info.is_protocol:
179-
if not isinstance(type, Instance) or not type.type.is_protocol:
180-
if type.type.fullname() != 'builtins.object':
181-
self.fail('All bases of a protocol must be protocols', tdef)
179+
if not type.type.is_protocol and type.type.fullname() != 'builtins.object':
180+
self.fail('All bases of a protocol must be protocols', tdef)
182181
# Recompute MRO now that we have analyzed all modules, to pick
183182
# up superclasses of bases imported from other modules in an
184183
# import loop. (Only do so if we succeeded the first time.)

mypy/suggestions.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,7 @@ def __init__(self, module: Optional[str], graph: Graph) -> None:
542542
self.graph = graph
543543

544544
def visit_instance(self, t: Instance) -> str:
545-
if t.type is not None:
546-
s = t.type.fullname() or t.type.name() or None
547-
else:
548-
s = None
545+
s = t.type.fullname() or t.type.name() or None
549546
if s is None:
550547
return '<???>'
551548
if s in reverse_builtin_aliases:

mypy/types.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,10 +1947,7 @@ def visit_deleted_type(self, t: DeletedType) -> str:
19471947
return "<Deleted '{}'>".format(t.source)
19481948

19491949
def visit_instance(self, t: Instance) -> str:
1950-
if t.type is not None:
1951-
s = t.type.fullname() or t.type.name() or '<???>'
1952-
else:
1953-
s = '<?>'
1950+
s = t.type.fullname() or t.type.name() or '<???>'
19541951
if t.erased:
19551952
s += '*'
19561953
if t.args != []:
@@ -2081,10 +2078,7 @@ def list_str(self, a: List[Type]) -> str:
20812078
"""
20822079
res = []
20832080
for t in a:
2084-
if isinstance(t, Type):
2085-
res.append(t.accept(self))
2086-
else:
2087-
res.append(str(t))
2081+
res.append(t.accept(self))
20882082
return ', '.join(res)
20892083

20902084

@@ -2101,9 +2095,7 @@ def strip_type(typ: Type) -> Type:
21012095

21022096

21032097
def is_named_instance(t: Type, fullname: str) -> bool:
2104-
return (isinstance(t, Instance) and
2105-
t.type is not None and
2106-
t.type.fullname() == fullname)
2098+
return isinstance(t, Instance) and t.type.fullname() == fullname
21072099

21082100

21092101
def copy_type(t: Type) -> Type:

0 commit comments

Comments
 (0)