Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Strange interaction betweentyping.Protocol and unrelatedisinstance() checks #105144

Closed
Assignees
AlexWaygood
Labels
3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-typingtype-bugAn unexpected behavior, bug, or error
@AlexWaygood

Description

@AlexWaygood

Onmain, anisinstance() check againstSized works just the same as it does on Python 3.11:

>>>from typingimport Sized>>>isinstance(1, Sized)False

However! If you first subclassSized like this,TypeError is raised on thatisinstance() check!

>>>from typingimport Sized, Protocol>>>classFoo(Sized,Protocol):pass...>>>isinstance(1, Sized)Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1153, in __instancecheck__    return self.__subclasscheck__(type(obj))           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1428, in __subclasscheck__    return issubclass(cls, self.__origin__)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "<frozen abc>", line 123, in __subclasscheck__  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1793, in __subclasscheck__    return super().__subclasscheck__(other)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "<frozen abc>", line 123, in __subclasscheck__  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1876, in _proto_hook    raise TypeError("Instance and class checks can only be used with"TypeError: Instance and class checks can only be used with @runtime_checkable protocols

This was originally reported by@vnmabus inpython/typing_extensions#207.

Note that (because of theabc-module cache), thisdoesn't reproduce if you do anisinstance() checkbefore subclassingtyping.Sized:

>>>import typing>>>isinstance(1, typing.Sized)False>>>classFoo(typing.Sized,typing.Protocol):pass...>>>isinstance(1, typing.Sized)False

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp