Mypy: Version 0.780 does not recognize python internal types as `typing.Type` anymore

Created on 12 Jun 2020  路  5Comments  路  Source: python/mypy

Since upgrading from version 0.770 to 0.780 we saw issues, where types used during runtime were recognized as object and not as typing.Type anymore.

  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy

# test.py
from typing import Type, Union

def method(type: Type) -> None:
    pass

method(int)
method(Union[int, str])
  • What is the actual behavior/output?
> mypy test.py # mypy==0.780
test.py:8: error: Argument 1 to "method" has incompatible type "object"; expected "Type[Any]"
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
> mypy test.py # mypy==0.770
Success: no issues found in 1 source file
  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

    • Python version 3.8.2

    • failing mypy versions: 0.780 + latest master

    • passing mypy version: 0.770

Most helpful comment

@JukkaL That's not really satisfying isn't it? I would like to use something that expresses that the values in the mapping are type-like, not instances. Using Any or object is basically saying "I don't care about the type". I want mypy to warn that any of the following entries are not allowed:

mapping: Dict[str, ?] = {
    "foo": "some string",
    "baz": SomeClass(),
}

All 5 comments

This is intended, at runtime Union[int, str] is not a subclass of type (on most newer version of Python anymore).

But the documentation says the following:

The only legal parameters for Type are classes, Any, type variables, and unions of any of these types. For example:

def new_non_team_user(user_class: Type[Union[BaseUser, ProUser]]): ...

Type[Any] is equivalent to Type which in turn is equivalent to type, which is the root of Python鈥檚 metaclass hierarchy.

The same happens with Optional[str] etc.. If Type is not the correct type to use, what should be used instead? How should I annotate mapping in this example?:

mapping: Dict[str, ?] = {
    "foo": str,
    "bar": Optional[str],
    "baz": SomeClass,

@hmvp You need to use object or Any.

@JukkaL That's not really satisfying isn't it? I would like to use something that expresses that the values in the mapping are type-like, not instances. Using Any or object is basically saying "I don't care about the type". I want mypy to warn that any of the following entries are not allowed:

mapping: Dict[str, ?] = {
    "foo": "some string",
    "baz": SomeClass(),
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

squarewave24 picture squarewave24  路  3Comments

Stiivi picture Stiivi  路  3Comments

arquolo picture arquolo  路  3Comments

takeda picture takeda  路  3Comments

PeterJCLaw picture PeterJCLaw  路  3Comments