Title: mypy fails to narrow Union[float,int] to float after isinstance(x, float) · Issue #6060 · python/mypy · GitHub
Open Graph Title: mypy fails to narrow Union[float,int] to float after isinstance(x, float) · Issue #6060 · python/mypy
X Title: mypy fails to narrow Union[float,int] to float after isinstance(x, float) · Issue #6060 · python/mypy
Description: Mypy fails to typecheck the following valid code: from typing import Union def maybe_convert_to_int(x: Union[float,int]) -> Union[float,int]: '''Convert x from float to int if it can be done losslessly.''' if isinstance(x, float): # x mu...
Open Graph Description: Mypy fails to typecheck the following valid code: from typing import Union def maybe_convert_to_int(x: Union[float,int]) -> Union[float,int]: '''Convert x from float to int if it can be done lossle...
X Description: Mypy fails to typecheck the following valid code: from typing import Union def maybe_convert_to_int(x: Union[float,int]) -> Union[float,int]: '''Convert x from float to int if it can...
Opengraph URL: https://github.com/python/mypy/issues/6060
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy fails to narrow Union[float,int] to float after isinstance(x, float)","articleBody":"Mypy fails to typecheck the following valid code:\r\n\r\n```python\r\nfrom typing import Union\r\ndef maybe_convert_to_int(x: Union[float,int]) -> Union[float,int]:\r\n '''Convert x from float to int if it can be done losslessly.'''\r\n if isinstance(x, float):\r\n # x must be a float here, so this is a legal operation, but\r\n # mypy still thinks its type is Union[float,int]\r\n assert isinstance(x, float)\r\n if x.is_integer():\r\n x = int(x)\r\n return x\r\n```\r\n\r\nWhen I run mypy on it, I get:\r\n\r\n```\r\n$ mypy ~/temp/test_mypy.py\r\ntemp/test_mypy.py:8: error: Item \"int\" of \"Union[float, int]\" has no attribute \"is_integer\"\r\n$ python --version\r\nPython 3.7.1\r\n$ mypy --version\r\nmypy 0.650\r\n```\r\n\r\nI would expect this program to pass the type checker, since I am using `isinstance` to narrow the type to float before performing the float-specific operation `float.is_integer`. If I replace the assertion with `x = float(x)`, that satisfies the type checker.","author":{"url":"https://github.com/DarwinAwardWinner","@type":"Person","name":"DarwinAwardWinner"},"datePublished":"2018-12-12T07:32:44Z","url":"https://github.com/python/mypy/issues/6060"}
| 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:531c8034-01a9-cb32-677b-3f19b6a0efc0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA08:1F5A:168212:246394:6B00302B |
| html-safe-nonce | 10cbfcb8d8ed381eb6dea69786f820c9c6e60ee9f4580c7f88e753cd9bdd7679 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTA4OjFGNUE6MTY4MjEyOjI0NjM5NDo2QjAwMzAyQiIsInZpc2l0b3JfaWQiOiIyNjk1MjAyMzE3NzQ1MDA4NjgzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6eefc13c9358c324865546a82b27bdcb60fc13255d051f6f3948b91651e35be5 |
| hovercard-subject-tag | issue:390098798 |
| 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/6060/issue_layout |
| twitter:image | https://opengraph.githubassets.com/afd5aa8d2a82548e2e322daafc1ff407d6c6b9a8705a15c28194b6588bb4714d/python/mypy/issues/6060 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/afd5aa8d2a82548e2e322daafc1ff407d6c6b9a8705a15c28194b6588bb4714d/python/mypy/issues/6060 |
| og:image:alt | Mypy fails to typecheck the following valid code: from typing import Union def maybe_convert_to_int(x: Union[float,int]) -> Union[float,int]: '''Convert x from float to int if it can be done lossle... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DarwinAwardWinner |
| 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