Title: Incorrect value for `Py_TPFLAGS_MANAGED_DICT` in `Lib/test/test_class.py` · Issue #136535 · python/cpython · GitHub
Open Graph Title: Incorrect value for `Py_TPFLAGS_MANAGED_DICT` in `Lib/test/test_class.py` · Issue #136535 · python/cpython
X Title: Incorrect value for `Py_TPFLAGS_MANAGED_DICT` in `Lib/test/test_class.py` · Issue #136535 · python/cpython
Description: I've noticed a discrepancy in the value of the Py_TPFLAGS_MANAGED_DICT flag constant used in Lib/test/test_class.py. Summary The test file Lib/test/test_class.py defines a local constant Py_TPFLAGS_MANAGED_DICT = (1 << 2). However, the c...
Open Graph Description: I've noticed a discrepancy in the value of the Py_TPFLAGS_MANAGED_DICT flag constant used in Lib/test/test_class.py. Summary The test file Lib/test/test_class.py defines a local constant Py_TPFLAGS...
X Description: I've noticed a discrepancy in the value of the Py_TPFLAGS_MANAGED_DICT flag constant used in Lib/test/test_class.py. Summary The test file Lib/test/test_class.py defines a local constant Py_TPF...
Opengraph URL: https://github.com/python/cpython/issues/136535
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect value for `Py_TPFLAGS_MANAGED_DICT` in `Lib/test/test_class.py`","articleBody":"I've noticed a discrepancy in the value of the `Py_TPFLAGS_MANAGED_DICT` flag constant used in `Lib/test/test_class.py`.\n\n### Summary\n\nThe test file `Lib/test/test_class.py` defines a local constant `Py_TPFLAGS_MANAGED_DICT = (1 \u003c\u003c 2)`. However, the canonical definition in the C header `Include/object.h` is `Py_TPFLAGS_MANAGED_DICT = (1 \u003c\u003c 4)`.\n\nThe existing tests that use this constant pass only coincidentally. They test plain classes which happen to have a flag set at the `(1 \u003c\u003c 2)` position, but this is not the actual managed dictionary flag.\n\nThis can be proven by a test case that uses a `__slots__` class. Such a class should not have a managed dictionary, and therefore, a test checking for the `MANAGED_DICT` flag should result in `0`. However, the test written to check for the incorrect `(1 \u003c\u003c 2)` flag also results in `0`, which causes an assertion to fail, proving the constant is wrong.\n\n### Proof of Incorrect Constant\n\nThe following test case demonstrates that the `(1 \u003c\u003c 2)` constant is incorrect. It uses a `__slots__` class, which is known not to have a managed dictionary.\n\nThe test attempts to assert that this class *has* the flag specified by the incorrect `(1 \u003c\u003c 2)` constant. This assertion fails, providing evidence that the constant is wrong.\n\n```python\n# In Lib/test/test_class.py\nimport unittest\n\nclass TestMisleadingConstant(unittest.TestCase):\n def test_slots_class_reveals_flaw_in_constant(self):\n # The incorrect constant as defined in test_class.py\n Py_TPFLAGS_MANAGED_DICT_WRONG = (1 \u003c\u003c 2)\n # The correct constant from object.h\n Py_TPFLAGS_MANAGED_DICT_CORRECT = (1 \u003c\u003c 4)\n\n class NoManagedDict:\n __slots__ = ('a',)\n\n # First, confirm that a __slots__ class correctly does NOT have\n # the true managed dict flag. This assertion passes.\n self.assertEqual(\n NoManagedDict.__flags__ \u0026 Py_TPFLAGS_MANAGED_DICT_CORRECT,\n 0,\n \"A __slots__ class should not have the correct MANAGED_DICT flag (1\u003c\u003c4)\"\n )\n\n # Next, try to assert that this class HAS the flag at the (1\u003c\u003c2) position.\n # This assertion FAILS with \"AssertionError: 0 != 4\", because a __slots__ class\n # does not have this flag either.\n # This failure is the proof that the (1\u003c\u003c2) constant is wrong for this purpose.\n self.assertEqual(\n NoManagedDict.__flags__ \u0026 Py_TPFLAGS_MANAGED_DICT_WRONG,\n Py_TPFLAGS_MANAGED_DICT_WRONG,\n \"This fails, proving the constant is wrong.\"\n )\n```\n\n### Reference Links\n\n1. **Incorrect constant in `test_class.py`**:\n * https://github.com/python/cpython/blob/49365bd110a254a6a304d2f880482bfeb2e4490d/Lib/test/test_class.py#L862\n\n2. **Correct constant definition in `object.h`**:\n * https://github.com/python/cpython/blob/49365bd110a254a6a304d2f880482bfeb2e4490d/Include/object.h#L532-L535\n\n### Proposed Fix\n\nThe constant `Py_TPFLAGS_MANAGED_DICT` in `Lib/test/test_class.py` should be updated to `(1 \u003c\u003c 4)` to match the C header definition.\nAdditionally, we could add a test case for a class that does not have `Py_TPFLAGS_INLINE_VALUES`.\n\nIf this analysis seems correct, I would be happy to open a Pull Request to update the constant and add a corresponding test case. \n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-136538\n* gh-140532\n* gh-140533\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ever0de","@type":"Person","name":"ever0de"},"datePublished":"2025-07-11T10:10:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/136535/cpython/issues/136535"}
| 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:ff0789f2-7cfd-35cd-57a9-45ac1a0acf3c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D196:2C09F1:717436:911E31:696B1536 |
| html-safe-nonce | aaafb21addefc4e23d754cf57bfffe20a9795c36b0a1a263921b7d7618c7b09b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTk2OjJDMDlGMTo3MTc0MzY6OTExRTMxOjY5NkIxNTM2IiwidmlzaXRvcl9pZCI6IjgwMTg0NjExMDU5NDk3MDk2MjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bdbf89badf476c8fc5ac70b44aa9c2edb2b65f02c5621cffa935aa583315f6dc |
| hovercard-subject-tag | issue:3222352400 |
| 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/136535/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7b3aecb67e2e81d29c18bdc3f985fba8c95cdc63d075fe7a951247050ac76aa2/python/cpython/issues/136535 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7b3aecb67e2e81d29c18bdc3f985fba8c95cdc63d075fe7a951247050ac76aa2/python/cpython/issues/136535 |
| og:image:alt | I've noticed a discrepancy in the value of the Py_TPFLAGS_MANAGED_DICT flag constant used in Lib/test/test_class.py. Summary The test file Lib/test/test_class.py defines a local constant Py_TPFLAGS... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ever0de |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width