Title: `isinstance` check against `collection.abc` classes fails if value has no `__class__` · Issue #153772 · python/cpython · GitHub
Open Graph Title: `isinstance` check against `collection.abc` classes fails if value has no `__class__` · Issue #153772 · python/cpython
X Title: `isinstance` check against `collection.abc` classes fails if value has no `__class__` · Issue #153772 · python/cpython
Description: Bug report Bug description: The problem can be reproduced with the following class. It succeeds when tested with isinstance() against base types like int, but fails when tested against collections.abc classes like Mapping. >>> from colle...
Open Graph Description: Bug report Bug description: The problem can be reproduced with the following class. It succeeds when tested with isinstance() against base types like int, but fails when tested against collections....
X Description: Bug report Bug description: The problem can be reproduced with the following class. It succeeds when tested with isinstance() against base types like int, but fails when tested against collections....
Opengraph URL: https://github.com/python/cpython/issues/153772
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`isinstance` check against `collection.abc` classes fails if value has no `__class__`","articleBody":"# Bug report\n\n### Bug description:\n\nThe problem can be reproduced with the following class. It succeeds when tested with `isinstance()` against base types like `int`, but fails when tested against `collections.abc` classes like `Mapping`.\n\n```python\n>>> from collections.abc import Mapping\n>>> \n>>> class NoClass:\n... def __getattribute__(self, attr):\n... if attr == \"__class__\":\n... raise AttributeError(attr)\n... return super().__getattribute__(attr)\n... \n>>> \n>>> isinstance(NoClass(), int)\nFalse\n>>> isinstance(NoClass(), Mapping)\nTraceback (most recent call last):\n File \"\u003cpython-input-4>\", line 1, in \u003cmodule>\n isinstance(NoClass(), Mapping)\n ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"\u003cfrozen abc>\", line 119, in __instancecheck__\n File \"\u003cpython-input-0>\", line 4, in __getattribute__\n raise AttributeError(attr)\nAttributeError: __class__\n```\nThe example class is arguably broken, but it seems objects without `__class__` actually exist. For a real life example see https://github.com/robotframework/robotframework/issues/5699 where the problematic object apparently is a Qt widget.\n\nThis is a very annoying case, because handling these problematic objects requires changing all `isinstance` usages where the type can be `collections.abc` class from something like\n```\nif isinstance(value, Mapping):\n ...\n```\nto\n```\ntry:\n is_mapping = isinstance(value, Mapping)\nexcept AttributeError:\n is_mapping = False\nif is_mapping:\n ...\n```\nI'll do that in our code in cases where there problematic objects are likely to occur, but wrapping all `isinstance` usage isn't practical so I'm worried there still can be crashes.\n\nI hope `collections.abc` types would be fixed so that they return `False` with objects without `__class__` instead of failing with `AttributeError`. Alternatively, if objects without `__class__` are considered to be totally broken, the interpreter should fail/warn when they are instantiated and not only with `isinstance()`.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154149\n* gh-156541\n* gh-156542\n* gh-156543\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pekkaklarck","@type":"Person","name":"pekkaklarck"},"datePublished":"2026-07-15T15:06:23Z","url":"https://github.com/python/cpython/issues/153772"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b90c7bd0-42b7-4344-24eb-84af17963bf7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A794:17543D:610168:7AEBAF:6B020981 |
| html-safe-nonce | 23a51c3c20f637353302d39e98c18a6e3de639329e26a6c0b20608e2886afcde |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzk0OjE3NTQzRDo2MTAxNjg6N0FFQkFGOjZCMDIwOTgxIiwidmlzaXRvcl9pZCI6Ijk0MDA2NjY5Mzg5ODcwMTE4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4cf62ed6353e77eaf9057e4d8c1110fc307bfdc90b468bd087706d761b160d17 |
| hovercard-subject-tag | issue:4893751133 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/153772/issue_layout |
| twitter:image | https://opengraph.githubassets.com/79c4699fdb3c9bef5e8dd164b73485af341c064bdbd48e694e94f78d31c879da/python/cpython/issues/153772 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/79c4699fdb3c9bef5e8dd164b73485af341c064bdbd48e694e94f78d31c879da/python/cpython/issues/153772 |
| og:image:alt | Bug report Bug description: The problem can be reproduced with the following class. It succeeds when tested with isinstance() against base types like int, but fails when tested against collections.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pekkaklarck |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width