Skip to content

Remove some "if type is None" style checks #7120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ def join_simple(declaration: Optional[Type], s: Type, t: Type) -> Type:
s, t = t, s

value = t.accept(TypeJoinVisitor(s))

if value is None:
# XXX this code path probably should be avoided.
# It seems to happen when a line (x = y) is a type error, and
# it's not clear that assuming that x is arbitrary afterward
# is a good idea.
return declaration

if declaration is None or is_subtype(value, declaration):
return value

Expand Down
9 changes: 4 additions & 5 deletions mypy/newsemanal/semanal_classprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ def check_protocol_status(info: TypeInfo, errors: Errors) -> None:
"""Check that all classes in MRO of a protocol are protocols"""
if info.is_protocol:
for type in info.bases:
if not isinstance(type, Instance) or not type.type.is_protocol:
if type.type.fullname() != 'builtins.object':
def report(message: str, severity: str) -> None:
errors.report(info.line, info.column, message, severity=severity)
report('All bases of a protocol must be protocols', 'error')
if not type.type.is_protocol and type.type.fullname() != 'builtins.object':
def report(message: str, severity: str) -> None:
errors.report(info.line, info.column, message, severity=severity)
report('All bases of a protocol must be protocols', 'error')


def calculate_class_vars(info: TypeInfo) -> None:
Expand Down
5 changes: 2 additions & 3 deletions mypy/semanal_pass3.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ def visit_class_def(self, tdef: ClassDef) -> None:
self.analyze_types(types, tdef.info)
for type in tdef.info.bases:
if tdef.info.is_protocol:
if not isinstance(type, Instance) or not type.type.is_protocol:
if type.type.fullname() != 'builtins.object':
self.fail('All bases of a protocol must be protocols', tdef)
if not type.type.is_protocol and type.type.fullname() != 'builtins.object':
self.fail('All bases of a protocol must be protocols', tdef)
# Recompute MRO now that we have analyzed all modules, to pick
# up superclasses of bases imported from other modules in an
# import loop. (Only do so if we succeeded the first time.)
Expand Down
5 changes: 1 addition & 4 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,7 @@ def __init__(self, module: Optional[str], graph: Graph) -> None:
self.graph = graph

def visit_instance(self, t: Instance) -> str:
if t.type is not None:
s = t.type.fullname() or t.type.name() or None
else:
s = None
s = t.type.fullname() or t.type.name() or None
if s is None:
return '<???>'
if s in reverse_builtin_aliases:
Expand Down
14 changes: 3 additions & 11 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1947,10 +1947,7 @@ def visit_deleted_type(self, t: DeletedType) -> str:
return "<Deleted '{}'>".format(t.source)

def visit_instance(self, t: Instance) -> str:
if t.type is not None:
s = t.type.fullname() or t.type.name() or '<???>'
else:
s = '<?>'
s = t.type.fullname() or t.type.name() or '<???>'
if t.erased:
s += '*'
if t.args != []:
Expand Down Expand Up @@ -2081,10 +2078,7 @@ def list_str(self, a: List[Type]) -> str:
"""
res = []
for t in a:
if isinstance(t, Type):
res.append(t.accept(self))
else:
res.append(str(t))
res.append(t.accept(self))
return ', '.join(res)


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


def is_named_instance(t: Type, fullname: str) -> bool:
return (isinstance(t, Instance) and
t.type is not None and
t.type.fullname() == fullname)
return isinstance(t, Instance) and t.type.fullname() == fullname


def copy_type(t: Type) -> Type:
Expand Down