[issue45665] Problems caused by isinstance(list[int]) returning True

Serhiy Storchaka report at bugs.python.org
Fri Oct 29 04:41:23 EDT 2021


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

isinstance(x, type) returns True for instances of types.GenericAlias (like list[int]). While it may help in some cases related to typing, in many unrelated cases it causes problems if the value which is not a type passes checks for types.

Also, isinstance(x, type) been not equal to issubclass(type(x), type) can cause other problems. No matter what the result should be, it should be consistent.

There are many occurrences of isinstance(..., type) in the code.

$ find Lib -name '*.py' \! -path '*/test*' -exec egrep 'isinstance.*,
type\)' '{}' + | wc -l
55

And all of them can potentially be broken if pass a types.GenericAlias instance. Many of them are safe, but every case should be analyzed.

----------
dependencies: +Incorrect repr of InitVar of a type alias, help(list[int]) fails, inspect not capturing type annotations created by __class_getitem__, is_dataclass() does not work for dataclasses which are subclasses of types.GenericAlias, resolve_bases() and new_class() do not work with type alias of a built-in type
nosy: +gvanrossum, kj

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45665>
_______________________________________


More information about the Python-bugs-list mailing list