Skip to content

Using numpy types conditionally gives confusing [assignment] error #16875

Closed
@cebtenzzre

Description

@cebtenzzre

Bug Report

When running strict mode on a simple example with numpy types, mypy will generate a confusing error message for [assignment] in which the expression type and the variable type are apparently the same, yet it fails anyway.

To Reproduce

import random

import numpy as np

def foo() -> None:
    if random.randrange(2):
        new_dtype = np.float16
    else:
        new_dtype = np.float32

Expected Behavior

If there is something wrong with the above code, I would expect mypy to tell me what the specific mismatch is.

Actual Behavior

$ mypy --strict repr.py
repr.py:9: error: Incompatible types in assignment (expression has type
"type[floating[Any]]", variable has type "type[floating[Any]]")  [assignment]
            new_dtype = np.float32
                        ^~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

Why would type[floating[Any]] not match type[floating[Any]]?

Your Environment

  • Mypy version used: mypy 1.9.0+dev.b956e6a57c4dd36d670097a3eccf7dc092348fec (compiled: no)
  • Python version used: Python 3.11.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions