Title: basic units example's subclassing is broken · Issue #19841 · matplotlib/matplotlib · GitHub
Open Graph Title: basic units example's subclassing is broken · Issue #19841 · matplotlib/matplotlib
X Title: basic units example's subclassing is broken · Issue #19841 · matplotlib/matplotlib
Description: Bug report Bug summary In basic_units.py, the TaggedValue has a __new__ method that does the following: matplotlib/examples/units/basic_units.py Lines 120 to 128 in f710ad3 def __new__(cls, value, unit): # generate a new subclass for val...
Open Graph Description: Bug report Bug summary In basic_units.py, the TaggedValue has a __new__ method that does the following: matplotlib/examples/units/basic_units.py Lines 120 to 128 in f710ad3 def __new__(cls, value, ...
X Description: Bug report Bug summary In basic_units.py, the TaggedValue has a __new__ method that does the following: matplotlib/examples/units/basic_units.py Lines 120 to 128 in f710ad3 def __new__(cls, value, ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/19841
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"basic units example's subclassing is broken","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n\r\nIn `basic_units.py`, the `TaggedValue` has a `__new__` method that does the following:\r\nhttps://github.com/matplotlib/matplotlib/blob/f710ad321b06b4b26137a51fd282436db8033f36/examples/units/basic_units.py#L120-L128\r\n\r\nHowever, when `value_class` is not `object`, then `object.__new__(subcls)` raises:\r\n```ipython\r\nIn [1]: class A: \r\n ...: def __new__(cls, value): \r\n ...: value_class = type(value) \r\n ...: subcls = type(f'A_{value_class.__name__}', (cls, value_class), {}) \r\n ...: return object.__new__(subcls) \r\n ...: \r\n\r\nIn [2]: A(object()) \r\nOut[2]: \u003c__main__.A_object at 0x7f9ce8a684c0\u003e\r\n\r\nIn [3]: A(1.0) \r\n---------------------------------------------------------------------------\r\nTypeError Traceback (most recent call last)\r\n\u003cipython-input-6-04f0da727658\u003e in \u003cmodule\u003e\r\n----\u003e 1 A(1.0)\r\n\r\n\u003cipython-input-5-3b6ef5f2aee2\u003e in __new__(cls, value)\r\n 3 value_class = type(value)\r\n 4 subcls = type(f'A_{value_class.__name__}', (cls, value_class), {})\r\n----\u003e 5 return object.__new__(subcls)\r\n 6 \r\n\r\nTypeError: object.__new__(A_float) is not safe, use float.__new__()\r\n```\r\n\r\nThis means that the intended subclassing there never happens, as it falls back to the generic `object.__new__` in the exception handler. I came across this while trying to fix #19535 with a conditional method, but of course, that never worked because the code always ended up using the generic non-subclass.\r\n\r\nI tried to fix this by doing `super().__new__` or `value_class.__new__` (and then some `object.\u003cmethod\u003e` needed to be `super().\u003cmethod\u003e` in other methods), but this runs into issues with other classes that also override `__new__`, namely `MaskedArray`.\r\n\r\nI think it makes sense to create subclasses (and then fix it so it actually leaves out methods that shouldn't be there), but I'm not sure that multiple inheritance is the right way to do it.\r\n\r\n**Matplotlib version**\r\n * Matplotlib version (`import matplotlib; print(matplotlib.__version__)`): `master` at the moment\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): n/a\r\n * Python version: 3.7.6","author":{"url":"https://github.com/QuLogic","@type":"Person","name":"QuLogic"},"datePublished":"2021-04-01T02:59:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/19841/matplotlib/issues/19841"}
| 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:f4c0536f-0f72-fe85-25fa-d4d9a5c4f1fe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D2D6:1D8CC3:E8D5AE:13BD2FA:6A5200E2 |
| html-safe-nonce | 622f7eb10e3e8911e92b45ec3cc1e7af6193fcbbc09fcb158b47062aefa61d78 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkQ2OjFEOENDMzpFOEQ1QUU6MTNCRDJGQTo2QTUyMDBFMiIsInZpc2l0b3JfaWQiOiIzOTA0Njk3NzcxNTEzNDc5Mzk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f1879f43cbe4aa07cd51711a7848a0c5aed3f3c9e666022cfa818ebbbce84b11 |
| hovercard-subject-tag | issue:847784460 |
| 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/matplotlib/matplotlib/19841/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e93a157a775768e120c71ef50352e69b5bdd64ce827e4a3173fabd60d375ed57/matplotlib/matplotlib/issues/19841 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e93a157a775768e120c71ef50352e69b5bdd64ce827e4a3173fabd60d375ed57/matplotlib/matplotlib/issues/19841 |
| og:image:alt | Bug report Bug summary In basic_units.py, the TaggedValue has a __new__ method that does the following: matplotlib/examples/units/basic_units.py Lines 120 to 128 in f710ad3 def __new__(cls, value, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | QuLogic |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width