Title: Argument Clinic: DSLParser.parse_converter() can return an incorrect kwargs dict · Issue #106359 · python/cpython · GitHub
Open Graph Title: Argument Clinic: DSLParser.parse_converter() can return an incorrect kwargs dict · Issue #106359 · python/cpython
X Title: Argument Clinic: DSLParser.parse_converter() can return an incorrect kwargs dict · Issue #106359 · python/cpython
Description: Originally posted by @AlexWaygood in #106354 (comment): Tools/clinic/clinic.py:5039: error: Incompatible return value type (got "tuple[str, bool, dict[str | None, Any]]", expected "tuple[str, bool, dict[str, Any]]") [return-value] return...
Open Graph Description: Originally posted by @AlexWaygood in #106354 (comment): Tools/clinic/clinic.py:5039: error: Incompatible return value type (got "tuple[str, bool, dict[str | None, Any]]", expected "tuple[str, bool,...
X Description: Originally posted by @AlexWaygood in #106354 (comment): Tools/clinic/clinic.py:5039: error: Incompatible return value type (got "tuple[str, bool, dict[str | None, Any]]", expected "t...
Opengraph URL: https://github.com/python/cpython/issues/106359
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argument Clinic: DSLParser.parse_converter() can return an incorrect kwargs dict","articleBody":"_Originally posted by @AlexWaygood in https://github.com/python/cpython/issues/106354#issuecomment-1617797726_:\r\n\r\n```\r\nTools/clinic/clinic.py:5039: error: Incompatible return value type (got\r\n\"tuple[str, bool, dict[str | None, Any]]\", expected\r\n\"tuple[str, bool, dict[str, Any]]\") [return-value]\r\n return name, False, kwargs\r\n ^~~~~~~~~~~~~~~~~~~\r\n```\r\n\r\nI think mypy is flagging a real bug in the code here. The issue is this block of code here:\r\n\r\nhttps://github.com/python/cpython/blob/d694f043ae25308dda607fad4ea1d8d85d79982f/Tools/clinic/clinic.py#L5033-L5039\r\n\r\nThe function is annotated as return `tuple[str, bool, KwargDict]`, and `KwargDict` is a type alias for `dict[str, Any]`. It's important that the dictionary that's the third element of the tuple only has strings as keys. If it doesn't, then this will fail:\r\n\r\nhttps://github.com/python/cpython/blob/d694f043ae25308dda607fad4ea1d8d85d79982f/Tools/clinic/clinic.py#L4617\r\n\r\nThe code as written, however, doesn't guarantee that all the keys in the `kwargs` dictionary will be strings. In the dictionary comprehension, we can see that `annotation` is an `ast.Call` instance. That means that `annotation.keywords` is of type `list[ast.keyword]` -- we can see this from typeshed's stubs for the `ast` module (which are an invaluable reference if you're working with ASTs in Python!): https://github.com/python/typeshed/blob/18d45d62aabe68fce78965c4920cbdeddb4b54db/stdlib/_ast.pyi#L324-L329. If `annotation.keywords` is of type `list[ast.keyword]`, that means that the `node` variable in the dictionary comprehension is of type `keyword`, which means (again using typeshed's `ast` stubs), that `node.arg` is of type `str | None`: https://github.com/python/typeshed/blob/18d45d62aabe68fce78965c4920cbdeddb4b54db/stdlib/_ast.pyi#L516-L520. AKA, the keys in this dictionary are not always guaranteed to be strings -- there's a latent bug in this code!\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-106361\n* gh-106364\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/erlend-aasland","@type":"Person","name":"erlend-aasland"},"datePublished":"2023-07-03T11:11:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/106359/cpython/issues/106359"}
| 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:0cb3c2bb-7dd2-733e-f0ef-4a9ef4f53b4d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B60:23348E:1044F1A:15CD9A1:696ACF7C |
| html-safe-nonce | bc0961c55381186515193c602db3c82c1aa3baba3177317d3bffc0ee29a5e17f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjYwOjIzMzQ4RToxMDQ0RjFBOjE1Q0Q5QTE6Njk2QUNGN0MiLCJ2aXNpdG9yX2lkIjoiODkyODIyODg5NTQyODg5MDQ5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5bfcff54c60c4fd383225da3b13b011f0998b2b25f296b9c07dcf4223d23a2ee |
| hovercard-subject-tag | issue:1785955132 |
| 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/106359/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1b03adfc088d3e932c6186df50db6d2b15d2a49d30e8ec50a3cb5249bf4e829c/python/cpython/issues/106359 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1b03adfc088d3e932c6186df50db6d2b15d2a49d30e8ec50a3cb5249bf4e829c/python/cpython/issues/106359 |
| og:image:alt | Originally posted by @AlexWaygood in #106354 (comment): Tools/clinic/clinic.py:5039: error: Incompatible return value type (got "tuple[str, bool, dict[str | None, Any]]", expected "tuple[str, bool,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erlend-aasland |
| 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