Skip to content

Type inference seems to ignore reachability analysis #6850

@tapetersen

Description

@tapetersen

Describe the bug
Not important as such in any specific case but wanted to report.

While pyright detects that a section of code is unreachable it still uses these statements for type inference of a variable.

Code or Screenshots
In the following code pyright excludes the possibility of a=5 since it detects the overwrite later but it has also marked that code as unreachable

image

# pyright: strict

from typing import Any, Literal
from typing_extensions import assert_type, Never


def no_return() -> Never:
    raise Exception()

class CtxMgr:
    def __enter__(self):
        return self

    def __exit__(self, *exc_info: Any) -> bool:
        return True

a = None
with CtxMgr():
    a = 5
    no_return()
    a = 10

assert a is not None
assert_type(a, Literal[5])

playground link

VS Code extension or command-line
Pylance language server 2023.12.1 (pyright a78e2b1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions