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])
> 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)
> mypy test.py # mypy==0.770
Success: no issues found in 1 source file
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(),
}
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
Anyorobjectis basically saying "I don't care about the type". I want mypy to warn that any of the following entries are not allowed: