Skip to content

Wrong type hint after unpacking/swapping #9731

Open
@NiklasRosenstein

Description

@NiklasRosenstein

Bug Report

Unpacking a tuple in the below scenario results in wrong assumed types.

To Reproduce

import typing as t

def spam(val: t.Union[t.Type, None] = None) -> None:

  def eggs(type_: t.Type) -> None:
    pass

  reveal_type(val)
  if isinstance(val, type):
    reveal_type(val)
    val, foo = None, val
    reveal_type(val)
    reveal_type(foo)
    return eggs(foo)

Expected Behavior

After the line val, foo = None, val, I would expect that

  1. val has type None (ok)
  2. foo has type Type[Any]
  3. the call to eggs() passes

Actual Behavior

$ mypy test.py 
test.py:9: note: Revealed type is 'Union[Type[Any], None]'
test.py:11: note: Revealed type is 'Type[Any]'
test.py:13: note: Revealed type is 'None'
test.py:14: note: Revealed type is 'None'
test.py:15: error: Argument 1 to "eggs" has incompatible type "None"; expected "Type[Any]"
Found 1 error in 1 file (checked 1 source file)

This does not match with my expectations 2 and 3.

Your Environment

Python 3.9
Mypy 0.790
MacOS 10.14.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions