Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.780 does not recognize python internal types as typing.Type anymore #8992

Closed
na-kialo opened this issue Jun 12, 2020 · 5 comments
Closed

Comments

@na-kialo
Copy link

na-kialo commented Jun 12, 2020

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
@ilevkivskyi
Copy link
Member

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

@aachurin
Copy link

aachurin commented Jun 16, 2020

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’s metaclass hierarchy.

@hmvp
Copy link

hmvp commented Jun 17, 2020

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,

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 19, 2020

@hmvp You need to use object or Any.

@hmvp
Copy link

hmvp commented Jun 22, 2020

@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(),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants