Title: Incorrect inference with isinstance() · Issue #2993 · python/mypy · GitHub
Open Graph Title: Incorrect inference with isinstance() · Issue #2993 · python/mypy
X Title: Incorrect inference with isinstance() · Issue #2993 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect inference with isinstance()","articleBody":"I think there are a few problems with `isinstance` inference.\r\n\r\n1) This code type checks with `mypy --strcit --python-version 3.6`, but it should not:\r\n```\r\nfrom typing import *\r\ndef f(x: Union[int, str], typ: type) -> None:\r\n if isinstance(x, (typ, int)):\r\n print(x + 1)\r\n```\r\nThe semantic analyzer concludes that within the body of `if isinstance()`, the type of `x` must be `int`. It's not, however. For example, in this case `f('a', str)`, the `if` branch will be followed even though `x` is not an `int` (and this will cause runtime `TypeError` without any mypy warnings).\r\n\r\nThe reason this happens is that `type` annotation is represented as an `Instance` rather than as a `FunctionLike` inside `checker.py:get_isinstance_type()`. As a result, it's skipped entirely in the loop, and only `int` is added to `types`, so sem analyzer thinks `x` must absolutely be `int`.\r\n\r\nOne solution is to make `type` represented with an instance of `CallableType` (why isn't it btw?!). Another solution is to simply add a bit of logic to the loop to properly handle this case.\r\n\r\n2) This code:\r\n```\r\ndef f(x: Union[int, str, List, Tuple]) -> None:\r\n if isinstance(x, (str, (list, tuple))):\r\n print(x[1])\r\n```\r\nresults in `error: Argument 2 to \"isinstance\" has incompatible type \"Tuple[str, Tuple[List[_T], Tuple[_T_co, ...]]]\"; expected \"Union[type, Tuple[type, ...]]\"`. \r\n\r\nThis is because there's this weird thing in python that allows to put *nested* tuples as the second argument to `isinstance`, and `mypy` (along with most other people) doesn't know about it. I don't know if it's worth fixing, but it's super easy (just flatten the second argument if it's a tuple).\r\n\r\nIf my understanding is correct, I'll make a PR.","author":{"url":"https://github.com/pkch","@type":"Person","name":"pkch"},"datePublished":"2017-03-14T09:29:42Z","url":"https://github.com/python/mypy/issues/2993"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:cde82943-4d46-2167-4eb5-2b15af4564df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E5A:F5AA9:22B6104:2F3CBC9:6B0029C9 |
| html-safe-nonce | df408c7a97350ea12756f86b09321ba647f690e44e81c5add3c6ded0fb02adc3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTVBOkY1QUE5OjIyQjYxMDQ6MkYzQ0JDOTo2QjAwMjlDOSIsInZpc2l0b3JfaWQiOiI0MTg3NDYxOTI1NjE5MTE2NDg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0f705e16060b25965dddea83af088f3480be13f62aae89d0890f02609694489e |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/2993 |
| twitter:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-cache |
| 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