Skip to content

The [unreachable] check fails for module scope code and allows unreachable raising of Exceptions #11331

Description

@harahu

Bug Report

There are seemingly trivial cases of unreachable code not being detected at module level, or when the code consists of raise statements.

To Reproduce

fromtypingimportFinalfromtypingimportNoReturndeffoo() ->NoReturn:
raiseException("foo")
defbar() ->NoReturn:
foo()
# Should trigger [unreachable] but doesn'traiseException("I shouldn't pass static analysis")
defbaz() ->NoReturn:
raiseException("baz")
# Should trigger [unreachable] but doesn'traiseException("I shouldn't pass static analysis")
defqux() ->None:
foo()
# Should trigger [unreachable] but doesn'traiseException("I shouldn't pass static analysis")
foo()
# Should trigger [unreachable] but doesn'traiseException("foo")
# Should trigger [unreachable] but doesn't(!)MEANING: Final[int] =42

Expected Behavior

As seen in my example, I would expect the optional [unreachable] check to detect the following examples of unreachable code:

  • General module level code following an exception being raised directly, or a NoReturn callable being called.
  • Unreachable exception raising (occurring after some prior exception having been raised)

Actual Behavior

The example above type checks with warn_unreachable = True.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
python_version = 3.8
pretty = True
warn_redundant_casts = True
warn_unused_ignores = True
ignore_missing_imports = True
strict_optional = True
no_implicit_optional = True
show_error_context = True
show_column_numbers = True
disallow_untyped_calls = True
disallow_untyped_defs = True
warn_return_any = True
warn_unreachable = True
show_none_errors = True
show_error_codes = True
follow_imports = normal
namespace_packages = True
explicit_package_bases = True
  • Python version used: 3.8.10
  • Operating system and version:
    Ubuntu 21.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions