Title: What to do about inferring types from multiple scopes? · Issue #4492 · python/mypy · GitHub
Open Graph Title: What to do about inferring types from multiple scopes? · Issue #4492 · python/mypy
X Title: What to do about inferring types from multiple scopes? · Issue #4492 · python/mypy
Description: Typically mypy infers the type of the variable from the first assignment. In some cases mypy infers the type from two assignments. The first assignment creates internally a partial type, which is missing some information. The second assi...
Open Graph Description: Typically mypy infers the type of the variable from the first assignment. In some cases mypy infers the type from two assignments. The first assignment creates internally a partial type, which is m...
X Description: Typically mypy infers the type of the variable from the first assignment. In some cases mypy infers the type from two assignments. The first assignment creates internally a partial type, which is m...
Opengraph URL: https://github.com/python/mypy/issues/4492
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"What to do about inferring types from multiple scopes?","articleBody":"Typically mypy infers the type of the variable from the first assignment. In some cases mypy infers the type from two assignments. The first assignment creates internally a *partial type*, which is missing some information. The second assignment completes the type. Example:\r\n\r\n```py\r\nx = [] # x gets a partial List type\r\nx.append(1) # x gets complete type List[int]\r\n```\r\n\r\nIn some cases the assignments can be in two different scopes. Example:\r\n\r\n```py\r\nx = []\r\n\r\ndef f() -> None:\r\n x.append(1)\r\n\r\n# type of x is List[int]\r\n```\r\n\r\nThis feature has apparently been supported a while, but it has some issues:\r\n\r\n* This behavior is somewhat messy to support in fine-grained incremental mode (#4464). \r\n* It can make it hard to figure out the inferred type of a variable, since the two assignments can be separated from each other by hundreds of lines and many functions.\r\n* Moving functions around can affect the type of a module/class variable in cases where there are two potential functions that can complete a partial type.\r\n* If the second assignment is missing or removed, and the first assignment initializes the variable with `None`, the inferred type will become `None`, which is usually not what the user wanted.\r\n* It seems a bit fragile with some issues that took a long time to notice (#4484, #4455, #4416).\r\n\r\nThere are a few alternatives which might be reasonable, instead of the current behavior:\r\n\r\n1. Require an annotation for the variable in cases such as the above, where the assignment that completes a partial type is in a different scope. This would break compatibility with previous mypy releases. Based on a quick experiment, the S internal codebase at Dropbox would require about 40 additional type annotations if we make this change.\r\n2. Ignore assignments in nested scopes and assume `Any` for any missing partial type information. For example, if the initial assignment is `x = []` and the only additional assignments are in nested scopes, infer `List[Any]` as the type of `x`. This way the type of `x` is defined entirely by the contents of a single scope, which solves the above problems. This also introduces an additional problem -- implicit `Any` types. We might want to have a strictness flag that falls back to requiring a type annotation (as in option 1 above).\r\n\r\nCurrently my biggest concern is fine-grained incremental mode. There it may be possible to support the current behavior by introducing a new concept: \"bound targets\". I'll add a writeup about this to #4464.\r\n\r\n@gvanrossum @msullivan @ilevkivskyi Thoughts?\r\n","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2018-01-19T14:26:07Z","url":"https://github.com/python/mypy/issues/4492"}
| 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:643101d2-ee20-f9e8-23c7-bd5871dd5a25 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ECEA:3CE430:156019:1DFF49:6B005513 |
| html-safe-nonce | 74a39d08ee0c8e2a8d4076b813fca6301b36348a47e18d07bde96c974ecfaecb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQ0VBOjNDRTQzMDoxNTYwMTk6MURGRjQ5OjZCMDA1NTEzIiwidmlzaXRvcl9pZCI6IjQzMTkyNTM0NjQyMjUxMDg2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | df783c6771ca3134ed76d903dcd90a521ab9443b01f8073a2d3efe9be10bed3e |
| hovercard-subject-tag | issue:289992205 |
| 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/4492/issue_layout |
| twitter:image | https://opengraph.githubassets.com/87945493970d1e3e352737ff3833ce30bb81ef98f7777c12184eac77c8879de9/python/mypy/issues/4492 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/87945493970d1e3e352737ff3833ce30bb81ef98f7777c12184eac77c8879de9/python/mypy/issues/4492 |
| og:image:alt | Typically mypy infers the type of the variable from the first assignment. In some cases mypy infers the type from two assignments. The first assignment creates internally a partial type, which is m... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| 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