Skip to content

Should typing be calling getattr on arbitrary classes?  #113320

@Gobot1234

Description

@Gobot1234

Bug report

Bug description:

I was updating my CI to support python3.12 and ran into the following error that isn't present in the 3.12 builds https://github.com/Gobot1234/steam.py/actions/runs/7276342755/job/19825996032

MRE:

from enum import IntEnum from typing import Protocol class classproperty: def __init__(self, func): self.__func__ = func def __get__(self, instance, type): return self.__func__(type) class _CommentThreadType(IntEnum): WorkshopAccountDeveloper = 2 class Commentable(Protocol): @classproperty def _COMMENTABLE_TYPE(cls): return _CommentThreadType[cls.__name__]

Traceback on main:

Running Debug|x64 interpreter... Traceback (most recent call last): File "C:\Users\alexw\coding\test_protocol.py", line 14, in <module> class Commentable(Protocol): ...<2 lines>... return _CommentThreadType[cls.__name__] File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1838, in __init__ cls.__callable_proto_members_only__ = all( ~~~^ callable(getattr(cls, attr, None)) for attr in cls.__protocol_attrs__ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1839, in <genexpr> callable(getattr(cls, attr, None)) for attr in cls.__protocol_attrs__ ~~~~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\test_protocol.py", line 9, in __get__ return self.__func__(type) ~~~~~~~~~~~~~^^^^^^ File "C:\Users\alexw\coding\test_protocol.py", line 17, in _COMMENTABLE_TYPE return _CommentThreadType[cls.__name__] ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "C:\Users\alexw\coding\cpython\Lib\enum.py", line 763, in __getitem__ return cls._member_map_[name] ~~~~~~~~~~~~~~~~^^^^^^ KeyError: 'Commentable'

Should typing be more careful about accessing random attributes on classes to prevent these kinds of errors in the future?

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions