Title: Inconsistent error messages when returning the wrong type for the type-conversion magic methods · Issue #130821 · python/cpython · GitHub
Open Graph Title: Inconsistent error messages when returning the wrong type for the type-conversion magic methods · Issue #130821 · python/cpython
X Title: Inconsistent error messages when returning the wrong type for the type-conversion magic methods · Issue #130821 · python/cpython
Description: Bug report Bug description: I noticed the error messages between magic methods like __int__ and __float__ were inconsistent. This seems like slightly undesirable behavior to me. I used the following code to generate many of them. class F...
Open Graph Description: Bug report Bug description: I noticed the error messages between magic methods like __int__ and __float__ were inconsistent. This seems like slightly undesirable behavior to me. I used the followin...
X Description: Bug report Bug description: I noticed the error messages between magic methods like __int__ and __float__ were inconsistent. This seems like slightly undesirable behavior to me. I used the followin...
Opengraph URL: https://github.com/python/cpython/issues/130821
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent error messages when returning the wrong type for the type-conversion magic methods","articleBody":"# Bug report\n\n### Bug description:\n\nI noticed the error messages between magic methods like `__int__` and `__float__` were inconsistent. This seems like slightly undesirable behavior to me. I used the following code to generate many of them.\n\n```python\nclass Foo:\n def __int__(self):\n return None\n \n def __float__(self):\n return None\n \n def __bytes__(self):\n return None\n \n def __complex__(self):\n return None\n \n def __bool__(self):\n return None\n \n def __str__(self):\n return None\n\ntry:\n int(Foo())\nexcept Exception as e:\n print(e)\n\ntry:\n float(Foo())\nexcept Exception as e:\n print(e)\n\ntry:\n bytes(Foo())\nexcept Exception as e:\n print(e)\n\ntry:\n complex(Foo())\nexcept Exception as e:\n print(e)\n\ntry:\n bool(Foo())\nexcept Exception as e:\n print(e)\n\ntry:\n str(Foo())\nexcept Exception as e:\n print(e)\n```\n\nAnd the output is as follows:\n\n```python\n__int__ returned non-int (type NoneType)\nFoo.__float__ returned non-float (type NoneType)\n__bytes__ returned non-bytes (type NoneType)\n__complex__ returned non-complex (type NoneType)\n__bool__ should return bool, returned NoneType\n__str__ returned non-string (type NoneType)\n```\nThe first issue I've made, but seems like a reasonable bug. I'm not sure if there are other \"type-conversion\" magic methods out there that aren't consistent.\n\n### CPython versions tested on:\n\n3.13, 3.12\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-130835\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/mxgordon","@type":"Person","name":"mxgordon"},"datePublished":"2025-03-04T07:28:08.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/130821/cpython/issues/130821"}
| 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:d7d731a2-e677-27ba-d836-4bc03a326f5b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA7A:8D142:10F8462:16B9EE4:696ACECF |
| html-safe-nonce | 1eea731a451e9891ca32e4f776f6832db11e5c8ea651c0a2ff42ccbb9ef82b01 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTdBOjhEMTQyOjEwRjg0NjI6MTZCOUVFNDo2OTZBQ0VDRiIsInZpc2l0b3JfaWQiOiIyMjY5OTcxOTQ2NjM3MjgzMDIzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 272b5512f7ef12e53113b0b098ff6957717e7d57fbcb5b1d3eecb1b2e819a3c4 |
| hovercard-subject-tag | issue:2893235443 |
| 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/130821/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a1cc2c9896d9c9c7277d5116682d301ffde5f7b0fd54d7a887da7d158a53052/python/cpython/issues/130821 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a1cc2c9896d9c9c7277d5116682d301ffde5f7b0fd54d7a887da7d158a53052/python/cpython/issues/130821 |
| og:image:alt | Bug report Bug description: I noticed the error messages between magic methods like __int__ and __float__ were inconsistent. This seems like slightly undesirable behavior to me. I used the followin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mxgordon |
| hostname | github.com |
| expected-hostname | github.com |
| None | c785f4ce187e9e7331257791b36ddee01625bb8e292a9b4fe2c16d4c006abf5d |
| 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 | c718a376fcf780eb22089171adb84a543f660bf7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width