Title: [MNT]: Improve return type of `ioff` and `ion` to improve Pyright analysis of bound variables · Issue #27659 · matplotlib/matplotlib · GitHub
Open Graph Title: [MNT]: Improve return type of `ioff` and `ion` to improve Pyright analysis of bound variables · Issue #27659 · matplotlib/matplotlib
X Title: [MNT]: Improve return type of `ioff` and `ion` to improve Pyright analysis of bound variables · Issue #27659 · matplotlib/matplotlib
Description: Summary The following code causes Pyright (the type checker used by Pylance, default type checker for Python in VSCode) to emit an error: import matplotlib.pyplot as plt with plt.ioff(): fig, ax = plt.subplots() ax.plot([...], [...]) # <...
Open Graph Description: Summary The following code causes Pyright (the type checker used by Pylance, default type checker for Python in VSCode) to emit an error: import matplotlib.pyplot as plt with plt.ioff(): fig, ax = ...
X Description: Summary The following code causes Pyright (the type checker used by Pylance, default type checker for Python in VSCode) to emit an error: import matplotlib.pyplot as plt with plt.ioff(): fig, ax = ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/27659
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[MNT]: Improve return type of `ioff` and `ion` to improve Pyright analysis of bound variables","articleBody":"### Summary\r\n\r\nThe following code causes Pyright (the type checker used by Pylance, default type checker for Python in VSCode) to emit an error:\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\n\r\nwith plt.ioff():\r\n fig, ax = plt.subplots()\r\n\r\nax.plot([...], [...]) # \u003c-- Pyright error: \"ax\" is possibly unbound\r\n```\r\n\r\nThe root cause of the issue seems to be that `ioff` and `ion` return `contextlib.ExitStack`, which can in theory supress excpetions, as indicated by the return type of `ErrorStack.__exit__` being `bool` rather than `Literal[False]` or `None`. (See https://github.com/microsoft/pyright/issues/7009)\r\n\r\nIn practice though, the way matplotlib uses `ExitStack`, it can't swallow exceptions. It would be convenient for typed codebases if the return type of `ion` and `ioff` reflected that.\r\n\r\n### Proposed fix\r\n\r\nA simple context manager like this could be used instead to make it clear to type checkers that exceptions will never be supressed (note that the return type of `__exit__` is `None`):\r\n\r\n```python\r\nclass _InteractiveContextManager(contextlib.AbstractContextManager):\r\n def __init__(self, callback: Callable):\r\n self._callback = callback\r\n\r\n def __enter__(self) -> None:\r\n return None\r\n \r\n def __exit__(self, __exc_type, __exc_value, __traceback) -> None:\r\n self._callback()\r\n```\r\nThis also has the added benefit that by returning `None` from `__enter__` we can better enforce that the context manager is not stored or accessed by the user. ","author":{"url":"https://github.com/FeldrinH","@type":"Person","name":"FeldrinH"},"datePublished":"2024-01-16T18:10:44Z","url":"https://github.com/matplotlib/matplotlib/issues/27659"}
| 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:914b19c2-d826-28cd-1fda-488d8ec43259 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8300:22E601:59CD02:78BA48:6B003282 |
| html-safe-nonce | da3e393bea4b6023d7ab45591d089d64c76a100792f83431a69d0661ac4b9654 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzAwOjIyRTYwMTo1OUNEMDI6NzhCQTQ4OjZCMDAzMjgyIiwidmlzaXRvcl9pZCI6IjE2MzAyNzg3OTE3NDgxMzc2MDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2ceaecb4c6533b23a6f563fa8a057827ff09f0ac876cd0f70cc743a0c55cb455 |
| hovercard-subject-tag | issue:2084547673 |
| 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/27659/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6a70a8151d7fb1889360720798277f470da61fe2b5ed85b77854d645dfc6fe3/matplotlib/matplotlib/issues/27659 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6a70a8151d7fb1889360720798277f470da61fe2b5ed85b77854d645dfc6fe3/matplotlib/matplotlib/issues/27659 |
| og:image:alt | Summary The following code causes Pyright (the type checker used by Pylance, default type checker for Python in VSCode) to emit an error: import matplotlib.pyplot as plt with plt.ioff(): fig, ax = ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FeldrinH |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width