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

isinstance(x, Type) not accepted #3060

Open
JukkaL opened this issue Mar 27, 2017 · 6 comments
Open

isinstance(x, Type) not accepted #3060

JukkaL opened this issue Mar 27, 2017 · 6 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-type-narrowing Conditional type narrowing / binder topic-usability

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 27, 2017

isinstance(x, Type) generates in an error, even though it should probably be considered equivalent to isinstance(x, type). Example:

from typing import Type

def f(x: Type[int]) -> None:
    if isinstance(x, Type):  # Argument 2 to "isinstance" has incompatible type "object"; 
                             # expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"
        pass
@pkch
Copy link
Contributor

pkch commented Mar 28, 2017

I almost fixed it inside #3070, but then I realized that the required change to typeshed (from Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]] to Union[object, Tuple[Union[object, Tuple[Any, ...]], ...]]) effectively eliminates type checking of the 2nd argument to isinstance. That would be quite disappointing.

What else can we do?

Map Type to be something other than object during type inference? Perhaps a special singleton class TypeObject?

@gvanrossum
Copy link
Member

gvanrossum commented Mar 28, 2017

Yeah, that would be equivalent to making the type object. But there's special-casing for isinstance() calls in mypy -- maybe we can expand that to manually check that the recursive tuple is valid? Or maybe we can implement recursive type aliases, at last (#731).

@pkch
Copy link
Contributor

pkch commented Mar 29, 2017

I guess a narrow fix could be to redefine Type in typing.pyi as class Type: ... instead of Type = object(). This would pass the type check for isinstance(x, Type).

We would still have to add some code elsewhere to make @JukkaL example pass, since at present Type[int] is not recognized as a subtype of Type, so mypy thinks that everything after isinstance(x, Type) is unreachable.

@gvanrossum
Copy link
Member

Is this about mypy or runtime? Making the behaviors of isinstance()/issubclass() match between static checks and runtime can be tricky.

@pkch
Copy link
Contributor

pkch commented Mar 29, 2017

I think mypy. It seems Python runtime returns True on isinstance(c, Type) whenever c is of type type. This is precisely what mypy should do as well (of course, assuming it can statically infer that c of type type).

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Jun 15, 2019
@ilevkivskyi
Copy link
Member

On current master the error is even worse:

Argument 2 to "isinstance" has incompatible type "_SpecialForm";
expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"

samuelcolvin pushed a commit to pydantic/pydantic that referenced this issue Sep 16, 2019
…#808)

* Add support for Type[T] typehints when arbitrary_types_allowe==True.

* Add documentation.

* Let black do its magic.

* Ignore mypy warning - see here: python/mypy#3060

* Prettify docs.

* Change Changelog.

* Refactor and simplify check for Type[T].

* Black again. ^^ - Really need pre-commit hooks.

* Update pydantic/validators.py

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Rename arbitrary_class to class.

* Black.

* Add type hints.

* Make private function public.

* Add support for bare Type.

* Black again.

* Update docs.

* CO_ct not meant for export.

* Fix get_class for Python3.6

* Update error message of ClassError.

* Use relative import.

* Incorporate typing feedback (both versions are fine with mypy).

* Move from issubclass to lenient_issubclass.

* correct docs
callahantiff added a commit to callahantiff/OMOP2OBO that referenced this issue Jul 17, 2020
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Mar 19, 2022
@AlexWaygood AlexWaygood added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Mar 27, 2022
mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 22, 2024
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#3060
mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 26, 2024
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#3060
mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 26, 2024
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#3060
mthuurne added a commit to ProtixIT/django-model-utils that referenced this issue Mar 27, 2024
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#3060
cr313 added a commit to cr313/model-utils that referenced this issue Apr 19, 2024
This is slightly more compact, but the main motivation is to work
around the following mypy bug:

python/mypy#3060
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-type-narrowing Conditional type narrowing / binder topic-usability
Projects
None yet
Development

No branches or pull requests

6 participants