gh-89828: Do not relay the __class__ attribute in GenericAlias (GH-93754)

list[int].__class__ returned type, and isinstance(list[int], type)
returned True. It caused numerous problems in code that checks
isinstance(x, type).
(cherry picked from commit f9433fff47)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-06-18 07:41:25 -07:00 committed by GitHub
parent 2eff55cd46
commit 579e985e75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 20 deletions

View file

@ -843,12 +843,11 @@ def singledispatch(func):
return get_origin(cls) in {Union, types.UnionType}
def _is_valid_dispatch_type(cls):
if isinstance(cls, type) and not isinstance(cls, GenericAlias):
if isinstance(cls, type):
return True
from typing import get_args
return (_is_union_type(cls) and
all(isinstance(arg, type) and not isinstance(arg, GenericAlias)
for arg in get_args(cls)))
all(isinstance(arg, type) for arg in get_args(cls)))
def register(cls, func=None):
"""generic_func.register(cls, func) -> func