Title: Support file-level assertions as inline configuration · Issue #19013 · python/mypy · GitHub
Open Graph Title: Support file-level assertions as inline configuration · Issue #19013 · python/mypy
X Title: Support file-level assertions as inline configuration · Issue #19013 · python/mypy
Description: Feature One would be able to do: # mypy: assert=platform == "win32" and python_version >= "3.10" and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greater than or equal to (3, 10). This is simi...
Open Graph Description: Feature One would be able to do: # mypy: assert=platform == "win32" and python_version >= "3.10" and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greate...
X Description: Feature One would be able to do: # mypy: assert=platform == "win32" and python_version >= "3.10" and the file would only be checked if sys.platform is win32 and the tuple sys...
Opengraph URL: https://github.com/python/mypy/issues/19013
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support file-level assertions as inline configuration","articleBody":"**Feature**\n\nOne would be able to do:\n\n```python\n# mypy: assert=platform == \"win32\" and python_version >= \"3.10\"\n```\n\nand the file would only be checked if `sys.platform` is `win32` and the tuple `sys.version_info` is greater than or equal to `(3, 10)`. This is similar to the [environment markers](https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers) used in dependency definitions but with only support for the two static [platform variables](https://mypy.readthedocs.io/en/stable/config_file.html#platform-configuration) that are already used by Mypy.\n\nImplementation-wise, there would be a pre-processing step that transforms the Python version string (if present) into a tuple. This way you can pass those two variables directly to `eval`.\n\n**Pitch**\n\nThis would significantly help in situations such as https://github.com/python/mypy/issues/19009. Brief recap:\n\n> When you have the following provider module:\n> \n> ```python\n> if sys.platform == \"win32\":\n> from my_pkg._pty.windows import PtySession\n> else:\n> from my_pkg._pty.unix import PtySession\n> ```\n> \n> and attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.\n> \n> For example, if you call `pty.openpty()` inside `my_pkg._pty.unix` and run Mypy on Windows it will show:\n> \n> ```\n> Module has no attribute \"openpty\" [attr-defined]\n> ```\n\nThe best solution available is adding a [top-level assertion](https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks) as introduced [here](https://github.com/python/mypy/pull/5894). For example, on top of the module `my_pkg._pty.unix` you would put `assert sys.platform != \"win32\"`.\n\nThis is suboptimal because it requires modifying runtime behavior. The `sys` import is required, perhaps only for the assertion, and if there are imports that only exist conditionally the assertion has to be in the middle of the standard import block on top.\n\nAdding a file-level comment is the most pragmatic option at hand, in lieu of adding an option that automatically propagates conditional static assertions to enclosed imports.","author":{"url":"https://github.com/ofek","@type":"Person","name":"ofek"},"datePublished":"2025-05-01T21:09:27Z","url":"https://github.com/python/mypy/issues/19013"}
| 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:c5927c82-f2ed-e27d-bacc-da432933af82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91AE:3EE843:1190F29:1798FD7:6AFFCE52 |
| html-safe-nonce | 86d61ce6d4376629d7c84382cd2a024bfab9151e9bf363be9d60a2317aee1a36 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUFFOjNFRTg0MzoxMTkwRjI5OjE3OThGRDc6NkFGRkNFNTIiLCJ2aXNpdG9yX2lkIjoiMTU0ODMwODU2NzgwOTgzODY3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6e187197703c6f9e31f696a7d2d4191fd2e62634658d485b9a72153ff622e491 |
| hovercard-subject-tag | issue:3034722374 |
| 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/mypy/19013/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fd1b3808a63ae7f0aff1e269cb18befee91a734c9b9ae21a1930294237051f55/python/mypy/issues/19013 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fd1b3808a63ae7f0aff1e269cb18befee91a734c9b9ae21a1930294237051f55/python/mypy/issues/19013 |
| og:image:alt | Feature One would be able to do: # mypy: assert=platform == "win32" and python_version >= "3.10" and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greate... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ofek |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width