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

Unexpected TypeError with type alias+issubclass+ABC #89489

Open
AMDmi3 mannequin opened this issue Sep 29, 2021 · 3 comments
Open

Unexpected TypeError with type alias+issubclass+ABC #89489

AMDmi3 mannequin opened this issue Sep 29, 2021 · 3 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@AMDmi3
Copy link
Mannequin

AMDmi3 mannequin commented Sep 29, 2021

BPO 45326
Nosy @gvanrossum, @corona10, @AMDmi3, @Fidget-Spinner, @akulakov, @burritoatspoton

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-09-29.21:14:09.774>
labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
title = 'Unexpected TypeError with type alias+issubclass+ABC'
updated_at = <Date 2022-02-28.01:52:34.626>
user = 'https://github.com/AMDmi3'

bugs.python.org fields:

activity = <Date 2022-02-28.01:52:34.626>
actor = 'andrei.avk'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-09-29.21:14:09.774>
creator = 'AMDmi3'
dependencies = []
files = []
hgrepos = []
issue_num = 45326
keywords = []
message_count = 2.0
messages = ['402914', '414170']
nosy_count = 6.0
nosy_names = ['gvanrossum', 'corona10', 'AMDmi3', 'kj', 'andrei.avk', 'burrito']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45326'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

@AMDmi3
Copy link
Mannequin Author

AMDmi3 mannequin commented Sep 29, 2021

Here's a curious problem. issubclass() check of a type against an ABC-derived class raises TypeError claiming that type is not a class, however inspect.isclass() says it's a class, and issubclass() check against a simple class works fine:

from abc import ABC

class C1:
    pass

issubclass(dict[str, str], C1)  # False

class C2(ABC):
    pass

issubclass(dict[str, str], C2)  # TypeError: issubclass() arg 1 must be a class

I've ran into this problem while using inspect module to look for subclasses of a specific ABC in a module which may also contain type aliases, and after converting a type alias from Dict[str, str] to modern dict[str, str] I've got an unexpected crash in this code:

    if inspect.isclass(member) and issubclass(member, superclass):

Not sure which is the culprit, ABC or how dict[]-style type aliases are implemented.

@AMDmi3 AMDmi3 mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir labels Sep 29, 2021
@corona10 corona10 added 3.10 only security fixes 3.11 only security fixes labels Sep 30, 2021
@akulakov
Copy link
Contributor

This error was added in https://bugs.python.org/issue33018 . See some discussion on that issue.

Note that first arg needs to be a type (i.e. instance of type) to avoid this error:

[ins] In [41]: class C(ABC):0

[ins] In [42]: issubclass(dict, C)
Out[42]: False

[ins] In [43]: issubclass('', C) # TypeError: issubclass() arg 1 must be a class

[ins] In [44]: issubclass(typing.Dict, C) # same error as above

@akulakov akulakov added type-bug An unexpected behavior, bug, or error labels Feb 28, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@sillydan1
Copy link

sillydan1 commented Nov 2, 2023

I encountered a slight variation of this issue. I am using python 3.10.12:

import inspect
from abc import ABCMeta

class C1(ABCMeta):
    pass

class C2(metaclass=C1):
    pass

DictAlias = dict[str,str]

print(inspect.isclass(DictAlias)) # True
print(issubclass(DictAlias, C1)) # False
print(issubclass(DictAlias, C2)) # TypeError: issubclass() arg 1 must be a class

Note that the original example by AMDmi3 also fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants