Title: Test stub files against real code · Issue #5028 · python/mypy · GitHub
Open Graph Title: Test stub files against real code · Issue #5028 · python/mypy
X Title: Test stub files against real code · Issue #5028 · python/mypy
Description: There are a few use cases when stubs may be compelling over type annotations or type comments: annotate code that needs to be Python 2 compatible, ideally by using type annotations (so no type comments), often times users prefer to have ...
Open Graph Description: There are a few use cases when stubs may be compelling over type annotations or type comments: annotate code that needs to be Python 2 compatible, ideally by using type annotations (so no type comm...
X Description: There are a few use cases when stubs may be compelling over type annotations or type comments: annotate code that needs to be Python 2 compatible, ideally by using type annotations (so no type comm...
Opengraph URL: https://github.com/python/mypy/issues/5028
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Test stub files against real code","articleBody":"There are a few use cases when stubs may be compelling over type annotations or type comments:\r\n\r\n- annotate code that needs to be Python 2 compatible, ideally by using type annotations (so no type comments), \r\n- often times users prefer to have type information outside of the source code (this is to avoid modifying legacy code change - and in practice lowers resistance from maintainers),\r\n- the maintainer of the library does not want to take on the in-line type annotations, someone else trying to provide it as a separate package.\r\n\r\nHowever at the moment this has some limitations: stub files are used only to check clients of the stub, not the code itself. \r\n\r\n```python\r\n# a.py\r\ndef add_int(a, b):\r\n return a + b\r\n\r\nadd_int(\"a\", \"b\") # mypy does not complain, actually ignores the entire file\r\n```\r\n\r\n```python\r\n# a.pyi\r\ndef add_int(a:int, b:int) -> int: ...\r\n```\r\n\r\n```python\r\n# client.py\r\nfrom a import add_int\r\nadd_int(1, 2)\r\nadd_int(1, \"2\") # here mypy is going to complain\r\n```\r\n\r\nIn case of the above files mypy will warn about bad usage of inside ``client.py``, but not in ``a.py`` (that is, mypy does not validate against the source files being annotated by the stub). This has serious drawbacks: \r\n- maintaining the stub files is hard, now users need to manually make sure whatever is in the stub file is an accurate representation of the matching source files (both interface - aka number of arguments, names - and type wise),\r\n- the stub creator does not know if the source code itself is type correct or not.\r\n\r\nHere I propose to implement a way to support testing stub files against the real code:\r\n\r\n- build the syntax tree for the source file,\r\n- build the syntax tree for the stub file, \r\n- merge stub tree into the source tree\r\n - source file only tree available: dynamically typed - noop\r\n - abstract syntax tree only: raise error - missing annotated sources\r\n - both stub and source AST exists:\r\n - for all source elements for what there is a matching stub element\r\n - copy inject over type annotations\r\n - complain if function interface missmatch\r\n- now just run the existing type checking mechanism on the enriched source code ast\r\n\r\nMerging the AST definitely is not trivial. The plan is to start with a POC implementation, that works for the most cases, and then see the viability of this approach. Note this would help a lot both stub maintainers I think (typeshed) and projects that need Python 2 support. ","author":{"url":"https://github.com/gaborbernat","@type":"Person","name":"gaborbernat"},"datePublished":"2018-05-13T18:18:48Z","url":"https://github.com/python/mypy/issues/5028"}
| 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:d095e848-8c00-4c2d-4784-90673c74a238 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A150:B7CF9:EFA2E9:14066E5:6B0057F8 |
| html-safe-nonce | 1c4fffce91b8c4e13acf9709641ae6992eff7a981cb67fc5123eda1880e4fd68 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTUwOkI3Q0Y5OkVGQTJFOToxNDA2NkU1OjZCMDA1N0Y4IiwidmlzaXRvcl9pZCI6Ijg5ODU0Nzk2NTQxNjQ5NDQ4ODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 305cbb073318db8b2460e1f4dbc4f5687ecf3c67aaf21d1178c6dc9c1fdfef3f |
| hovercard-subject-tag | issue:322615734 |
| 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/5028/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ee9ff349de1aa52588061416033cfea623ae91126b70318f59b6f7f74426dea5/python/mypy/issues/5028 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ee9ff349de1aa52588061416033cfea623ae91126b70318f59b6f7f74426dea5/python/mypy/issues/5028 |
| og:image:alt | There are a few use cases when stubs may be compelling over type annotations or type comments: annotate code that needs to be Python 2 compatible, ideally by using type annotations (so no type comm... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gaborbernat |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width