Skip to content

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

Closed

Description

@na-kialo

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

Activity

  1. ilevkivskyi commented on Jun 12, 2020

    @ilevkivskyi
    Member

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

  2. aachurin commented on Jun 16, 2020

    @aachurin

    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.

  3. hmvp commented on Jun 17, 2020

    @hmvp

    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,
    
  4. JukkaL commented on Jun 19, 2020

    @JukkaL
    Collaborator

    @hmvp You need to use object or Any.

  5. hmvp commented on Jun 22, 2020

    @hmvp

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions