Skip to content

[1.18 regression (typeshed)] ANY from unittest.mock is now reported as incompatible type in 1.18.1 #19841

@AVOstap

Description

@AVOstap

After upgrading to mypy 1.18.1, we started getting a new type error that was not present in 1.17.1.
Example

from unittest.mock import ANY

def func(param: str | None) -> None:
    pass

func(param=ANY)

Error (with 1.18.1)
error: Argument "param" to "func" has incompatible type "_ANY"; expected "str | None"

This code is obviously not meaningful by itself, but using ANY without an error was very helpful for testing when comparing complex structures.

Questions

  • Is this a deliberate change in behavior or a regression?
  • If this is now the expected behavior, would you recommend creating a TypedAny helper for tests, or is there a better workaround?

This seems like a breaking change for test code that relies on ANY being assignable to any type.

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