Skip to content

type is not recalculated when it was previously a List[Any] #8540

@dpinol

Description

@dpinol
  • Are you reporting a bug, or opening a feature request?
    A bug
  • Please insert below the code you are checking with mypy,
from typing import Any

def get_any() -> Any:
    return {}

def fun(message_json) -> int:
    messages = [get_any()]
    messages = get_any()
    if not isinstance(messages, list):
        print(messages)
    return 2
  • What is the actual behavior/output?
:12 error: Statement is unreachable  [unreachable]
            print(messages)
  • What is the behavior/output you expect?
    I would expect one the following:
    a) Either I get an error because "messages" is reassigned to Any afetr it was a list in the previous line
    b) Or I don't get the "unreachable" error because before the "if", "messages" can indeed not be a list

Curiously with the code below, I don't get any error

from typing import Any

def get_any() -> Any:
    return {}

def fun(message_json) -> int:
    messages = [3] # <=== only this line changes
    messages = get_any()
    if not isinstance(messages, list):
        print(messages)
    return 2

  • What are the versions of mypy and Python you are using?
    0.770 (with 0.761 I got the same error message but with "misc" code)

  • What are the mypy flags you are using? (For example --strict-optional)
    --show-error-codes --warn-unreachable

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions