Skip to content

ambiguity on using union of type[generic type] and callable in mypy==0.981 #13756

Closed
@Yun-Kim

Description

@Yun-Kim

Bug Report
I have a function which expects either a callable function or a type of generic type (i.e. float, str, int, ...) as an argument. However in mypy == 0.981, the mypy type checker strictly assumes that a type of type is a callable first and foremost (to be fair, it is technically a callable since it can also be used as a constructor). This results in an error even if I've passed in a type of type as I've intended.

To Reproduce

Here is my code that is erroring:

from typing import Callable, Type, TypeVar, Union


T = TypeVar("T")


def accepts_type_or_callable(name, value, value_type_or_callable):
    # type: (str, T, Union[Type[T], Callable[[Union[str, T, None]], T]]) -> T
    return value


accepts_type_or_callable("Test Name", 14.5, float)

Expected Behavior
No errors. I don't see any errors when using mypy <= 0.971.

Actual Behavior
When using mypy == 0.981, I get this mypy error:

error: Argument 3 to "accepts_type_or_callable" has incompatible type "Type[float]"; expected "Callable[[Union[str, float, None]], float]"

For some reason, mypy expects a float instead of Type[float] as I've specified. I'd love to get some insight on why it changes this expectation.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: N/A

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