Title: Invalid type inferred for attribute initialized to `None` in class body · Issue #4416 · python/mypy · GitHub
Open Graph Title: Invalid type inferred for attribute initialized to `None` in class body · Issue #4416 · python/mypy
X Title: Invalid type inferred for attribute initialized to `None` in class body · Issue #4416 · python/mypy
Description: The type inferred for attribute x in the fragment below is unexpected (I was using --strict-optional): class A: x = None y = None def __init__(self): # Note: no annotation self.x = 1 def g(self) -> None: self.y = 1 a = A() reveal_type(a....
Open Graph Description: The type inferred for attribute x in the fragment below is unexpected (I was using --strict-optional): class A: x = None y = None def __init__(self): # Note: no annotation self.x = 1 def g(self) ->...
X Description: The type inferred for attribute x in the fragment below is unexpected (I was using --strict-optional): class A: x = None y = None def __init__(self): # Note: no annotation self.x = 1 def g(self) -&...
Opengraph URL: https://github.com/python/mypy/issues/4416
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid type inferred for attribute initialized to `None` in class body","articleBody":"The type inferred for attribute `x` in the fragment below is unexpected (I was using `--strict-optional`):\r\n\r\n```py\r\nclass A:\r\n x = None\r\n y = None\r\n\r\n def __init__(self): # Note: no annotation\r\n self.x = 1\r\n\r\n def g(self) -> None:\r\n self.y = 1\r\n\r\na = A()\r\nreveal_type(a.x) # None \u003c---- unexpected\r\nreveal_type(a.y) # Union[int, None]\r\n\r\nif a.x is not None:\r\n 1 + '' # No error, because mypy considers this unreachable\r\n```\r\n\r\n`Union[None, Any]` would be a better inferred type for `A.x`. It would work around the false negative. This would be consistent with how global variables work:\r\n\r\n```py\r\nx = None\r\n\r\ndef f():\r\n global x\r\n x = 1\r\n\r\nreveal_type(x) # Union[Any, None]\r\n```\r\n\r\nWhen not using strict optional checking, the type of `A.x` is currently also `None`. `Any` would be a better inferred type in that case. \r\n\r\nAnother alternative would be to require annotations for both `x` and `y`, since inferring a useful (non-`None`) type requires non-local context. This would be my preference if we didn't have existing code to worry about -- this could require a large number of additional annotations for code that currently type checks cleanly. A final option would be to only require an annotation for `y` and infer `Union[None, Any]` for `x`, since `x` is initialized in an unannotated method.\r\n\r\n[Note that use of non-local context (outside current scope) can make fine-grained incremental checking harder to implement.]\r\n\r\n","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2018-01-02T17:03:13Z","url":"https://github.com/python/mypy/issues/4416"}
| 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:441a8216-e75f-340d-5e97-dc14db42bd75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9868:283776:E9058B:137CE8A:6B00570F |
| html-safe-nonce | e386dfe0559cbac213b0b53015cb0d15a96c1aee05ad5261d5cefeeddbf435e6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODY4OjI4Mzc3NjpFOTA1OEI6MTM3Q0U4QTo2QjAwNTcwRiIsInZpc2l0b3JfaWQiOiI4OTcyMTM4MjgzMTUyNzI1Nzc1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b182dab272a0ba3358702167de0005db02934335d16356da0cf3a9d8018ab8b3 |
| hovercard-subject-tag | issue:285482836 |
| 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/4416/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4d8625ab8c8f97cec7c82e64dddc0877345d7cae68525e9c3c3ddbee03e1bf3a/python/mypy/issues/4416 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4d8625ab8c8f97cec7c82e64dddc0877345d7cae68525e9c3c3ddbee03e1bf3a/python/mypy/issues/4416 |
| og:image:alt | The type inferred for attribute x in the fragment below is unexpected (I was using --strict-optional): class A: x = None y = None def __init__(self): # Note: no annotation self.x = 1 def g(self) ->... |
| 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