René's URL Explorer Experiment


Title: AttributeErrors raised in `.keys()` or `.__getitem__()` during `{**mymapping}`are incorrectly masked · Issue #145876 · python/cpython · GitHub

Open Graph Title: AttributeErrors raised in `.keys()` or `.__getitem__()` during `{**mymapping}`are incorrectly masked · Issue #145876 · python/cpython

X Title: AttributeErrors raised in `.keys()` or `.__getitem__()` during `{**mymapping}`are incorrectly masked · Issue #145876 · python/cpython

Description: Bug description: I have a custom Mapping type. I am unpacking it with eg {**mymapping}. If, in either the keys() or the the __getitem__() method, I raise most kinds of errors, such as a ValueError, these are reported correctly. BUT, if I...

Open Graph Description: Bug description: I have a custom Mapping type. I am unpacking it with eg {**mymapping}. If, in either the keys() or the the __getitem__() method, I raise most kinds of errors, such as a ValueError,...

X Description: Bug description: I have a custom Mapping type. I am unpacking it with eg {**mymapping}. If, in either the keys() or the the __getitem__() method, I raise most kinds of errors, such as a ValueError,...

Opengraph URL: https://github.com/python/cpython/issues/145876

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"AttributeErrors raised in `.keys()` or `.__getitem__()` during `{**mymapping}`are incorrectly masked","articleBody":"### Bug description:\n\nI have a custom Mapping type. I am unpacking it with eg `{**mymapping}`. If, in either the `keys()` or the the `__getitem__()` method, I raise most kinds of errors, such as a ValueError, these are reported correctly. BUT, if I raise an AttributeError, then this error isn't reported properly, instead I get `TypeError: 'MyMapping' object is not a mapping`, masking the actual error:\n\n```python\nclass MyMapping:\n    def __init__(\n        self,\n        *,\n        raises_on_keys: type[Exception] | None = None,\n        raises_on_getitem: type[Exception] | None = None,\n    ):\n        self.raises_on_keys = raises_on_keys\n        self.raises_on_getitem = raises_on_getitem\n\n    def __getitem__(self, key):\n        if self.raises_on_getitem:\n            raise self.raises_on_getitem(\"error in __getitem__\")\n        return key * 2\n\n    def keys(self):\n        if self.raises_on_keys:\n            raise self.raises_on_keys(\"error in keys\")\n        return [1, 2, 3]\n\n\noptions = [\n    None,\n    ValueError,\n    AttributeError,\n]\noutcomes = []\nfor raises_on_keys in options:\n    for raises_on_getitem in options:\n        try:\n            d = {\n                **MyMapping(\n                    raises_on_keys=raises_on_keys, raises_on_getitem=raises_on_getitem\n                )\n            }\n            outcomes.append((raises_on_keys, raises_on_getitem, \"Success\", d))\n        except Exception as e:\n            outcomes.append((raises_on_keys, raises_on_getitem, \"Exception\", str(e)))\n\n# format to markdown table\nprint(\"| raises_on_keys | raises_on_getitem | outcome | result |\")\nprint(\"| --- | --- | --- | --- |\")\nfor raises_on_keys, raises_on_getitem, outcome, result in outcomes:\n    raises_on_keys_str = raises_on_keys.__name__ if raises_on_keys else \"None\"\n    raises_on_getitem_str = raises_on_getitem.__name__ if raises_on_getitem else \"None\"\n    print(\n        f\"| {raises_on_keys_str} | {raises_on_getitem_str} | {outcome} | `{result}` |\"\n    )\n```\n\nRan with `uv run --python 3.14 bug.py`, which resolves to python `3.14.2`. This gives:\n\n| raises_on_keys | raises_on_getitem | error |\n| --- | --- | --- |\n| None | None | `` |\n| None | ValueError | `ValueError: error in __getitem__` |\n| None | AttributeError | `TypeError: 'MyMapping' object is not a mapping` |\n| ValueError | None | `ValueError: error in keys` |\n| ValueError | ValueError | `ValueError: error in keys` |\n| ValueError | AttributeError | `ValueError: error in keys` |\n| AttributeError | None | `TypeError: 'MyMapping' object is not a mapping` |\n| AttributeError | ValueError | `TypeError: 'MyMapping' object is not a mapping` |\n| AttributeError | AttributeError | `TypeError: 'MyMapping' object is not a mapping` |\n\nWhat I would expect is for all of the `TypeError: 'MyMapping' object is not a mapping` errors to actually be `AttributeError: error in keys` or `AttributeError: error in __getitem__` errors.\n\nI assume this is because in the implementation, it does assumes ducktyping, and the raised attribute error is interpreted as \"the passed object doesn't even have a `keys()`/`__getitem__` method\"\n\neg guessing this is how this is currently implemented:\n```python\ntry:\n    for key in obj.keys():\n        yield key, obj.__getitem__(key)\nexcept AttributeError as e:\n    raise TypeError(f\"'{type(obj).__name__}' object is not a mapping\")\n```\n\nWhat I think SHOULD happen:\n\n```python\ntry:\n    keys = obj.keys\nexcept AttributeError as e:\n    raise TypeError(f\"'{type(obj).__name__}' object is not a mapping\")\nfor key in keys():\n    try:\n        getter = obj.__getitem__\n    except AttributeError as e:\n        raise TypeError(f\"'{type(obj).__name__}' object is not a mapping\")\n    yield key, getter(key)\n```\n\nEDIT: Actually this should be more performant, only 2 checks, instead of N checks, one per key. (Also, for the record, this includes suggestion to improve the error messages, but that should definitely be a separate PR)\n\n```python\ntry:\n    keys = obj.keys\nexcept AttributeError as e:\n    raise TypeError(f\"'{type(obj).__name__}' object requires a .keys() method to be used as a mapping\")\ntry:\n    getter = obj.__getitem__\nexcept AttributeError as e:\n    raise TypeError(f\"'{type(obj).__name__}' object requires a .__getitem__() method to be used as a mapping\")\nfor key in keys():\n    yield key, getter(key)\n```\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-145877\n* gh-145878\n* gh-145906\n* gh-146472\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/NickCrews","@type":"Person","name":"NickCrews"},"datePublished":"2026-03-12T18:03:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/145876/cpython/issues/145876"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:932aeba2-8cde-9088-2a7e-1c8bcb6b0819
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8146:3089B4:28931E:376282:6A513FF6
html-safe-nonce8a871d203d812c279e5cc0674742ca659362c6dd8c78872e472172bb91a5e1d2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTQ2OjMwODlCNDoyODkzMUU6Mzc2MjgyOjZBNTEzRkY2IiwidmlzaXRvcl9pZCI6IjIyODUxNzIwMjA1OTY1MjI5OTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmace6bad21a1d44f2cd3055d37449c352d6dcc64f6239a9bf0937f426634a50de52
hovercard-subject-tagissue:4066343151
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/145876/issue_layout
twitter:imagehttps://opengraph.githubassets.com/386c805a77653bfeb8480672354cb63314b22c7b731cd0182f53d27a6c2e51c8/python/cpython/issues/145876
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/386c805a77653bfeb8480672354cb63314b22c7b731cd0182f53d27a6c2e51c8/python/cpython/issues/145876
og:image:altBug description: I have a custom Mapping type. I am unpacking it with eg {**mymapping}. If, in either the keys() or the the __getitem__() method, I raise most kinds of errors, such as a ValueError,...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameNickCrews
hostnamegithub.com
expected-hostnamegithub.com
Nonee840f405b2718e8f2d55aafa9ff27dbce17e29d0c253011d05ea0fea3c78baff
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasee876f00c5723f6080b9d294e4958fd4d506e6faf
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/145876#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F145876
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F145876
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/145876
Reloadhttps://github.com/python/cpython/issues/145876
Reloadhttps://github.com/python/cpython/issues/145876
Please reload this pagehttps://github.com/python/cpython/issues/145876
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/145876
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
AttributeErrors raised in .keys() or .__getitem__() during {**mymapping}are incorrectly maskedhttps://github.com/python/cpython/issues/145876#top
https://github.com/serhiy-storchaka
3.15pre-release feature fixes, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/NickCrews
NickCrewshttps://github.com/NickCrews
on Mar 12, 2026https://github.com/python/cpython/issues/145876#issue-4066343151
gh-145876: Fix AttributeError masked during dict unpacking #145877https://github.com/python/cpython/pull/145877
gh-145876: preserve AttributeError in dict unpacking #145878https://github.com/python/cpython/pull/145878
gh-145876: Do not mask AttributeErrors raised during dictionary unpacking #145906https://github.com/python/cpython/pull/145906
gh-145876: Do not mask KeyErrors raised during dictionary unpacking in call #146472https://github.com/python/cpython/pull/146472
serhiy-storchakahttps://github.com/serhiy-storchaka
3.15pre-release feature fixes, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.