Skip to content

Commit

Permalink
Optionally disallow statement expressions, this is an experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
jstasiak committed Jun 4, 2020
1 parent 348a9d4 commit 8e8667b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3110,7 +3110,15 @@ def try_infer_partial_type_from_indexed_assignment(
del partial_types[var]

def visit_expression_stmt(self, s: ExpressionStmt) -> None:
self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
type_ = self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
proper_type = get_proper_type(type_)
if (self.options.disallow_unused_expressions and
not (self.current_node_deferred or
isinstance(s.expr, (EllipsisExpr,)) or
isinstance(proper_type, (NoneType, UninhabitedType)) or
isinstance(proper_type, Instance) and proper_type.last_known_value
)):
self.fail("Unused expression of type '{}'".format(proper_type), s)

def visit_return_stmt(self, s: ReturnStmt) -> None:
"""Type check a return statement."""
Expand Down
5 changes: 5 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ def add_invertible_flag(flag: str,
" non-overlapping types",
group=strictness_group)

add_invertible_flag('--disallow-unused-expressions', default=True, strict_flag=False,
help="Prohibit using expressions in statement context without using their"
"value",
group=strictness_group)

strict_help = "Strict mode; enables the following flags: {}".format(
", ".join(strict_flag_names))
strictness_group.add_argument(
Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BuildType:
"disallow_untyped_calls",
"disallow_untyped_decorators",
"disallow_untyped_defs",
"disallow_unused_expressions",
"follow_imports",
"follow_imports_for_stubs",
"ignore_errors",
Expand Down Expand Up @@ -111,6 +112,8 @@ def __init__(self) -> None:
# Disallow subclassing values of type 'Any'
self.disallow_subclassing_any = False

self.disallow_unused_expressions = False

# Also check typeshed for missing annotations
self.warn_incomplete_stub = False

Expand Down

0 comments on commit 8e8667b

Please sign in to comment.