Title: [MNT]: Type hints for the data keyword argument · Issue #25634 · matplotlib/matplotlib · GitHub
Open Graph Title: [MNT]: Type hints for the data keyword argument · Issue #25634 · matplotlib/matplotlib
X Title: [MNT]: Type hints for the data keyword argument · Issue #25634 · matplotlib/matplotlib
Description: Summary The data keyword argument presents a tricky edge case for typing that is currently not ideal. #25632 Provides the first step, untyped but present in the type stubs (so stubtest will check them at least. Proposed fix The OO api ty...
Open Graph Description: Summary The data keyword argument presents a tricky edge case for typing that is currently not ideal. #25632 Provides the first step, untyped but present in the type stubs (so stubtest will check t...
X Description: Summary The data keyword argument presents a tricky edge case for typing that is currently not ideal. #25632 Provides the first step, untyped but present in the type stubs (so stubtest will check t...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/25634
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[MNT]: Type hints for the data keyword argument","articleBody":"### Summary\n\nThe `data` keyword argument presents a tricky edge case for typing that is currently not ideal.\r\n\r\n#25632 Provides the first step, untyped but present in the type stubs (so `stubtest` will check them at least.\n\n### Proposed fix\n\n## The OO api type hints: overloads\r\n\r\nThe most properly the type hint would look something like:\r\n\r\n```python\r\n @overload\r\n def hlines(\r\n self,\r\n y: float | ArrayLike,\r\n xmin: float | ArrayLike,\r\n xmax: float | ArrayLike,\r\n colors: Sequence[ColorType] | None = ...,\r\n linestyles: LineStyleType = ...,\r\n label: str = ...,\r\n *,\r\n data: None =...,\r\n **kwargs\r\n ) -\u003e LineCollection: ...\r\n @overload\r\n def hlines(\r\n self,\r\n y: float | ArrayLike | str,\r\n xmin: float | ArrayLike | str,\r\n xmax: float | ArrayLike | str,\r\n colors: Sequence[ColorType] | None | str = ...,\r\n linestyles: LineStyleType = ...,\r\n label: str = ...,\r\n *,\r\n data: Mapping[str, ArrayLike],\r\n **kwargs\r\n ) -\u003e LineCollection: ...\r\n```\r\n\r\nThat is to say:\r\n\r\nAn overloaded call where one call has `data` of type `None`, and the other has `data` with type `Mapping[str, ArrayLike]` (and also appends `str` to the valid types for affected parameters)\r\n\r\nNote that technically `str` does pass as `ArrayLike`, as that is a broad union which includes scalars (and `str` specifically), but communicating explicitly that `str` is expected for the case where data is not None is a bit better.\r\n\r\nIf `**kwargs` is not included, the first overload could simply omit `data` all together, making it a required keyword arg.\r\n\r\n## The problem: pyplot\r\n\r\nCurrent pyplot generation code (tools/boilerplate.py) _only_ looks at the _first_ signature of overloads.\r\nThis currently only affects a small number of methods that are in the autogenerated portion of pyplot and have overloads in their definitions (e.g. Axes.legend), which simply don't get the full type hints in pyplot. \r\n\r\nCurrently, none of the affected methods result in a type hint being added which is not correct, just type hints being omitted (and thus to mypy are `Any`) and so not deriving the value of having type hints for that small number of methods\r\n\r\nThe above overload for `data` would not be the same, as it would either get `None` OR `Mapping[...]`, not the union. In the latter case, this would be a problem as the default would not match the hint. In the former, it would fail if you tried to use the arg. If no `**kwargs` and data is simply omitted, it would simply be as it already was where pyplot gets no type hint for `data`, but it does appear in the signature.\r\n\r\nThe pyplot generation code may need to be be more comprehensive and explicitly handle the case of overloads. This is certainly possible, but not easy.\r\n\r\n## Considerations for inline type hints\r\n\r\nSince `data` does not appear in the actual signature this is mildly complicated for if we ever do want to move towards inline type hints. Such a move is not in the current plans, but worth writing down why this is a complicating factor.\r\n\r\nIf we use overloads, I think it may actually just wash away, so maybe not as complicated as otherwise.\r\n\r\n[PEP 612](https://peps.python.org/pep-0612/) addresses allowing static typing of decorators which modify signatures by adding _positional_ arguments, but [explicitly excludes](https://peps.python.org/pep-0612/#concatenating-keyword-parameters) the case of adding a _keyword-only_ arg, as is the case for the `_preprocess_data`. So statically using the decorator to transparently modify the signature for the type checker is not possible.","author":{"url":"https://github.com/ksunden","@type":"Person","name":"ksunden"},"datePublished":"2023-04-05T22:07:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/25634/matplotlib/issues/25634"}
| 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:7c695b35-a172-c0ee-687e-a2d06cc7e8d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B87E:34EF3:AF6DFB:EADCA2:6A531967 |
| html-safe-nonce | 1782783ea1b3a5617871b9101f1352d05eb0d02dbb91c4e6a8565d89eac5b25f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODdFOjM0RUYzOkFGNkRGQjpFQURDQTI6NkE1MzE5NjciLCJ2aXNpdG9yX2lkIjoiNDgzMDg1MzEyNDY3NDc1NDkxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 05047b251e223329732aca59871768b4d8c1b80eb65381e0231f6ec402a66571 |
| hovercard-subject-tag | issue:1656364007 |
| 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/25634/issue_layout |
| twitter:image | https://opengraph.githubassets.com/55c246a7019863cbf9fbf00ff3cc7c14cd15a96c9b38e32a5dae5f3517028283/matplotlib/matplotlib/issues/25634 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/55c246a7019863cbf9fbf00ff3cc7c14cd15a96c9b38e32a5dae5f3517028283/matplotlib/matplotlib/issues/25634 |
| og:image:alt | Summary The data keyword argument presents a tricky edge case for typing that is currently not ideal. #25632 Provides the first step, untyped but present in the type stubs (so stubtest will check t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ksunden |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width