Title: Type narrowing behavior for data descriptor assignment is incorrect in some cases · Issue #10399 · python/mypy · GitHub
Open Graph Title: Type narrowing behavior for data descriptor assignment is incorrect in some cases · Issue #10399 · python/mypy
X Title: Type narrowing behavior for data descriptor assignment is incorrect in some cases · Issue #10399 · python/mypy
Description: Mypy normally performs type narrowing for assignment statements when the LHS is a member access (e.g. a.b = x). However, it appears to avoid type narrowing in certain circumstances when b is a descriptor. This special casing leads to inc...
Open Graph Description: Mypy normally performs type narrowing for assignment statements when the LHS is a member access (e.g. a.b = x). However, it appears to avoid type narrowing in certain circumstances when b is a desc...
X Description: Mypy normally performs type narrowing for assignment statements when the LHS is a member access (e.g. a.b = x). However, it appears to avoid type narrowing in certain circumstances when b is a desc...
Opengraph URL: https://github.com/python/mypy/issues/10399
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing behavior for data descriptor assignment is incorrect in some cases","articleBody":"Mypy normally performs type narrowing for assignment statements when the LHS is a member access (e.g. `a.b = x`). However, it appears to avoid type narrowing in certain circumstances when `b` is a descriptor. This special casing leads to incorrect behaviors in some cases. (Look for \"!!!\" in the sample below.)\r\n\r\n```python\r\nfrom typing import Any, Protocol, Union\r\n\r\nclass HasName(Protocol):\r\n def __name__(self) -> str:\r\n ...\r\n\r\nclass Animal:\r\n def __name__(self) -> str:\r\n ...\r\n\r\nclass Dog(Animal):\r\n def __name__(self) -> str:\r\n ...\r\n\r\n def run(self) -> None:\r\n print(\"Run Spot Run!\")\r\n\r\n def bark(self) -> None:\r\n print(\"Ruff!\")\r\n\r\nclass Cat:\r\n def __name__(self) -> str:\r\n ...\r\n\r\n def run(self) -> None:\r\n print(\"Run Kitty Run!\")\r\n\r\n def purr(self) -> None:\r\n print(\"Purr!\")\r\n\r\nclass AnimalMember:\r\n def __get__(self, instance: Any, owner: Any) -> Animal:\r\n ...\r\n\r\n def __set__(self, instance: Any, value: HasName) -> None:\r\n ...\r\n\r\nclass Struct:\r\n member = AnimalMember()\r\n\r\ndef test_descriptor(cat: Cat, dog: Dog, cat_or_dog: Union[Cat, Dog]):\r\n inst = Struct()\r\n\r\n inst.member = cat_or_dog\r\n reveal_type(inst.member) # mypy: Revealed type is 'Animal'\r\n inst.member.run() # mypy: \"Animal\" has no attribute \"run\"\r\n inst.member.bark() # mypy: \"Animal\" has no attribute \"bark\"\r\n inst.member.purr() # mypy: \"Animal\" has no attribute \"purr\"\r\n\r\n inst.member = cat\r\n reveal_type(inst.member) # mypy: Revealed type is 'Animal'\r\n inst.member.run() # mypy: \"Animal\" has no attribute \"run\"\r\n inst.member.bark() # mypy: \"Animal\" has no attribute \"bark\"\r\n inst.member.purr() # mypy: \"Animal\" has no attribute \"purr\"\r\n\r\n inst.member = dog\r\n reveal_type(inst.member) # mypy: Revealed type is 'Dog'\r\n inst.member.run() # mypy: no error\r\n inst.member.bark() # mypy: no error\r\n inst.member.purr() # mypy: \"Dog\" has no attribute \"purr\"\r\n\r\n inst.member = cat_or_dog\r\n reveal_type(inst.member) # mypy: Revealed type is 'Dog' !!!!\r\n inst.member.run() # mypy: no error\r\n inst.member.bark() # mypy: no error\r\n inst.member.purr() # mypy: \"Dog\" has no attribute \"purr\"\r\n\r\n inst.member = cat\r\n reveal_type(inst.member) # mypy: Revealed type is 'Dog' !!!!\r\n inst.member.run() # mypy: no error\r\n inst.member.bark() # mypy: no error !!!!\r\n inst.member.purr() # mypy: \"Dog\" has no attribute \"purr\" !!!!\r\n```\r\n\r\nRefer to [this bug report in the pyright repo](https://github.com/microsoft/pyright/issues/1814) for details and a discussion of opitons. I'm interested in thoughts on a more robust heuristic that we can use across type checkers to handle this case properly.","author":{"url":"https://github.com/erictraut","@type":"Person","name":"erictraut"},"datePublished":"2021-05-02T02:17:48Z","url":"https://github.com/python/mypy/issues/10399"}
| 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:9c9e62b6-7723-6371-5ad3-5f605ee59256 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5B2:1756E4:E023CA:123B0A8:6AFFFABF |
| html-safe-nonce | 583b5e58c8a40dcd113fe4766d0deb78e0b24028cbf8268cb009fde05b3c8ae7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUIyOjE3NTZFNDpFMDIzQ0E6MTIzQjBBODo2QUZGRkFCRiIsInZpc2l0b3JfaWQiOiIyNzA1MTgwNTE0NjE2MDgxMDg3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1d357a7287f8990b3c91d0d1aad4c9b353ed97a2fb6f4188ae06fe6f1450e0ca |
| hovercard-subject-tag | issue:873818337 |
| 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/10399/issue_layout |
| twitter:image | https://opengraph.githubassets.com/81feb9422eec627bf581220badedb6dafb35f5b267ac135e18dc5d5a65be98be/python/mypy/issues/10399 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/81feb9422eec627bf581220badedb6dafb35f5b267ac135e18dc5d5a65be98be/python/mypy/issues/10399 |
| og:image:alt | Mypy normally performs type narrowing for assignment statements when the LHS is a member access (e.g. a.b = x). However, it appears to avoid type narrowing in certain circumstances when b is a desc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erictraut |
| 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