Skip to content

False negative with a constrained generic type in the presence of Any #9186

@russelldavis

Description

@russelldavis
from typing import Any, AnyStr
def foo(arg1: AnyStr, arg2: AnyStr) -> None: ...
bar: Any = ""
foo(1, "") # Error correctly reported
foo(b"", bar) # No error (correct)
foo(1, bar) # No error (false negative)

In the presence of Any, I would expect mypy to allow any of the values of the constraint (in this case, str or bytes), but not any arbitrary type.

Playground link: https://mypy-play.net/?mypy=latest&python=3.8&flags=strict&gist=6970b49fa90d7767b0ef26c9f977e182

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions