Title: No warning for uninitialised instance variable when using instance variable annotation · Issue #4206 · python/mypy · GitHub
Open Graph Title: No warning for uninitialised instance variable when using instance variable annotation · Issue #4206 · python/mypy
X Title: No warning for uninitialised instance variable when using instance variable annotation · Issue #4206 · python/mypy
Description: If you intend to initialise an instance variable in __init__ but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runtime warning but no warning from mypy. Te...
Open Graph Description: If you intend to initialise an instance variable in __init__ but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runt...
X Description: If you intend to initialise an instance variable in __init__ but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a ...
Opengraph URL: https://github.com/python/mypy/issues/4206
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"No warning for uninitialised instance variable when using instance variable annotation","articleBody":"If you intend to initialise an instance variable in `__init__` but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runtime warning but no warning from mypy. Tested on mypy version 0.540.\r\n\r\n```\r\nclass MyClass:\r\n my_attr: str\r\n def __init__(self, my_attr: str) -> None:\r\n pass\r\n # missing line: self.my_attr = my_attr\r\n\r\nx = MyClass(\"grapefruit\")\r\ns: str = x.my_attr\r\n# ^ AttributeError at runtime\r\n```\r\n\r\nHowever, if the instance variable annotation is removed, mypy does warn.\r\n\r\n```\r\nclass MyClass:\r\n def __init__(self, my_attr: str) -> None:\r\n pass\r\n # missing line: self.my_attr = my_attr\r\n\r\nx = MyClass(\"grapefruit\")\r\ns: str = x.my_attr\r\n```\r\n\r\nFrom PEP 526 (emphasis mine):\r\n\r\n> In particular, the value-less notation `a: int` allows one to annotate instance variables that **should be initialized** in `__init__` or `__new__`.","author":{"url":"https://github.com/ned","@type":"Person","name":"ned"},"datePublished":"2017-11-04T03:17:57Z","url":"https://github.com/python/mypy/issues/4206"}
| 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:8504ba36-b1e9-b796-36fa-463d5de3af42 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 80FC:1A12A9:A42B37:DA7250:6B004753 |
| html-safe-nonce | 87b308a28f890cbc879069e07f26c72a6235865bb628bcb31468e1387781cebf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MEZDOjFBMTJBOTpBNDJCMzc6REE3MjUwOjZCMDA0NzUzIiwidmlzaXRvcl9pZCI6IjMxMDMwOTYyMTY1NDQ2MjY1MTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 936373bf17e99b7280e6ed9eaec004f0ef0b4f4d0e8f6daf33ac70ef3b61f66c |
| hovercard-subject-tag | issue:271162562 |
| 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/4206/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a4b79b113f4f6255cc1b210b3caf3426ae525fa4eb2782d5b7589c4be217876b/python/mypy/issues/4206 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a4b79b113f4f6255cc1b210b3caf3426ae525fa4eb2782d5b7589c4be217876b/python/mypy/issues/4206 |
| og:image:alt | If you intend to initialise an instance variable in __init__ but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ned |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width