Title: Incorrect definition of collection builtins causes type checking error related to false MRO · Issue #11543 · python/typeshed · GitHub
Open Graph Title: Incorrect definition of collection builtins causes type checking error related to false MRO · Issue #11543 · python/typeshed
X Title: Incorrect definition of collection builtins causes type checking error related to false MRO · Issue #11543 · python/typeshed
Description: The way some collection builtins (e.g. list, dict, ...) are defined in typeshed causes false positive with type checking, due to the incorrect MRO exposed by typeshed. For example, typeshed/stdlib/builtins.pyi Line 971 in af84d2f class l...
Open Graph Description: The way some collection builtins (e.g. list, dict, ...) are defined in typeshed causes false positive with type checking, due to the incorrect MRO exposed by typeshed. For example, typeshed/stdlib/...
X Description: The way some collection builtins (e.g. list, dict, ...) are defined in typeshed causes false positive with type checking, due to the incorrect MRO exposed by typeshed. For example, typeshed/stdlib/...
Opengraph URL: https://github.com/python/typeshed/issues/11543
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect definition of collection builtins causes type checking error related to false MRO","articleBody":"The way some collection builtins (e.g. `list`, `dict`, ...) are defined in typeshed causes false positive with type checking, due to the incorrect MRO exposed by typeshed.\r\n\r\nFor example, https://github.com/python/typeshed/blob/af84d2fbfe8640c9f5049bfd0e74ea3270c8ea2c/stdlib/builtins.pyi#L971 defines list as \"inheriting from `MutableSequence`\", however that is not true and causes the following false negative error:\r\n\r\n```python\r\nfrom typing import Optional\r\nfrom collections.abc import MutableSequence\r\n\r\n\r\nclass Item:\r\n def __init__(self, comment: Optional[str] = None):\r\n self._comment = comment\r\n\r\n\r\nclass Array(Item, MutableSequence, list):\r\n def __init__(self, values: list, comment: Optional[str] = None):\r\n Item.__init__(self, comment)\r\n list.__init__(self, values)\r\n\r\n\r\nif __name__ == '__main__':\r\n a = Array([0, 1, 2])\r\n print([p.__name__ for p in a.__class__.__mro__])\r\n```\r\n\r\n```console\r\nmain.py:10: error: Cannot determine consistent method resolution order (MRO) for \"Array\" [misc]\r\nmain.py:12: error: Argument 1 to \"__init__\" of \"Item\" has incompatible type \"Array\"; expected \"Item\" [arg-type]\r\nmain.py:13: error: No overload variant of \"__init__\" of \"list\" matches argument types \"Array\", \"list[Any]\" [call-overload]\r\nmain.py:13: note: Possible overload variants:\r\nmain.py:13: note: def [_T] __init__(self: list[_T]) -> None\r\nmain.py:13: note: def [_T] __init__(self: list[_T], Iterable[_T], /) -> None\r\nFound 3 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=dc0042ce469e49dc22de0c91384d4588\r\n\r\nNote that this does not correspond to the real implementation. When I run the script above, no runtime error is found. Instead, this is the output:\r\n\r\n```console\r\n$ python3.12 main.py\r\n['Array', 'Item', 'MutableSequence', 'Sequence', 'Reversible', 'Collection', 'Sized', 'Iterable', 'Container', 'list', 'object']\r\n```\r\n\r\nSo my guess is that `typeshed` should not tell `mypy` that \"`list` and co\" are subclasses of \"`MutableSequence` and co\".\r\n\r\n---\r\n\r\nNote:\r\n\r\nwhile this particular choice of inheritance is debatable, it is fundamental for the way some libraries work, e.g. `tomlkit` work. It uses `MutableSequence` as a mixin, so that I gets `MutableSequence` methods for free, but it also want the custom objects to be considered lists themselves, so the users can use the API transparently as if they were dealing with built-in objects (this is an important aspect of the design).\r\n\r\n---\r\n\r\nRelated: https://github.com/python/mypy/issues/11427","author":{"url":"https://github.com/abravalheri","@type":"Person","name":"abravalheri"},"datePublished":"2024-03-08T15:00:57Z","url":"https://github.com/python/typeshed/issues/11543"}
| 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:dca1a4f9-210a-257c-4e0a-8410331e5200 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B58A:13A091:2E742F:3DBCB2:6B00C252 |
| html-safe-nonce | cc18bcfeed5d48bac57e764f9703afcd48e2509f0a4a0b41a360534d85d54f19 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNThBOjEzQTA5MToyRTc0MkY6M0RCQ0IyOjZCMDBDMjUyIiwidmlzaXRvcl9pZCI6Ijk3NDY2MjAyODQwMjY3MjIxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2109b5e6578476c6f8917f1095b0abe832291c71c2d67feb9c5850dcd44a58e5 |
| hovercard-subject-tag | issue:2176211114 |
| 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/typeshed/11543/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ca270fc9a183e82bd02416a18af9fd50a420237c7041385f0be980f1390e0e0d/python/typeshed/issues/11543 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ca270fc9a183e82bd02416a18af9fd50a420237c7041385f0be980f1390e0e0d/python/typeshed/issues/11543 |
| og:image:alt | The way some collection builtins (e.g. list, dict, ...) are defined in typeshed causes false positive with type checking, due to the incorrect MRO exposed by typeshed. For example, typeshed/stdlib/... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abravalheri |
| hostname | github.com |
| expected-hostname | github.com |
| None | 93edd3bebc4afb7ce711ee74cb5d2a95927850b082a4d43e212222389cb1c415 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | 1fee81a280ea86312b69448e261b418189bfd5b5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width