Skip to content

[3.15] gh-153772: Make abc isinstance() tolerate instances without __class__ (GH-154149) - #156541

Merged
hugovk merged 1 commit into
python:3.15from
miss-islington:backport-3e245fa-3.15
Aug 29, 2026
Merged

[3.15] gh-153772: Make abc isinstance() tolerate instances without __class__ (GH-154149)#156541
hugovk merged 1 commit into
python:3.15from
miss-islington:backport-3e245fa-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

The built-in isinstance() reads an instance's class with a lookup that
suppresses AttributeError and falls back to the object's type, so
isinstance(obj, int) returns False for an object whose class access
raises. ABCMeta.instancecheck read class directly instead, so
isinstance(obj, Mapping) leaked that AttributeError.

Fall back to type(instance) when class is unavailable, in both the C
and the pure-Python implementations, so the abstract base classes behave
like the built-in isinstance(). Such objects are unusual, but they do turn
up in the wild (for example some Qt widgets).
(cherry picked from commit 3e245fa)

Co-authored-by: Vyron Vasileiadis hi@fedonman.com

…lass__ (pythonGH-154149)

The built-in isinstance() reads an instance's __class__ with a lookup that
suppresses AttributeError and falls back to the object's type, so
isinstance(obj, int) returns False for an object whose __class__ access
raises. ABCMeta.__instancecheck__ read __class__ directly instead, so
isinstance(obj, Mapping) leaked that AttributeError.

Fall back to type(instance) when __class__ is unavailable, in both the C
and the pure-Python implementations, so the abstract base classes behave
like the built-in isinstance(). Such objects are unusual, but they do turn
up in the wild (for example some Qt widgets).
(cherry picked from commit 3e245faf34efc41c39bbe76071e6edb7a62692a8)

Co-authored-by: Vyron Vasileiadis <hi@fedonman.com>
@hugovk
hugovk merged commit d122f3e into python:3.15 Aug 29, 2026
60 checks passed
@miss-islington
miss-islington deleted the backport-3e245fa-3.15 branch August 29, 2026 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants