Title: Changing order of isinstance checks breaks inferred type · Issue #4422 · python/mypy · GitHub
Open Graph Title: Changing order of isinstance checks breaks inferred type · Issue #4422 · python/mypy
X Title: Changing order of isinstance checks breaks inferred type · Issue #4422 · python/mypy
Description: This works fine: def __add__(self, other: object) -> 'Currency': if isinstance(other, self.__class__) and isinstance(other, Currency): return self.__class__(other.amt + self.amt) return NotImplemented but this does not: def __add__(self,...
Open Graph Description: This works fine: def __add__(self, other: object) -> 'Currency': if isinstance(other, self.__class__) and isinstance(other, Currency): return self.__class__(other.amt + self.amt) return NotImplemen...
X Description: This works fine: def __add__(self, other: object) -> 'Currency': if isinstance(other, self.__class__) and isinstance(other, Currency): return self.__class__(other.amt + self.amt) return ...
Opengraph URL: https://github.com/python/mypy/issues/4422
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Changing order of isinstance checks breaks inferred type","articleBody":"This works fine:\r\n```python\r\ndef __add__(self, other: object) -> 'Currency':\r\n if isinstance(other, self.__class__) and isinstance(other, Currency):\r\n return self.__class__(other.amt + self.amt)\r\n return NotImplemented\r\n```\r\n\r\nbut this does not:\r\n```python\r\ndef __add__(self, other: object) -> 'Currency':\r\n if isinstance(other, Currency) and isinstance(other, self.__class__): # only expression order is flipped\r\n return self.__class__(other.amt + self.amt)\r\n return NotImplemented\r\n```\r\nthrows: `error: \"object\" has no attribute \"amt\"`\r\n\r\nFull code to reproduce it locally: https://gist.github.com/pirate/b3ef5a25449285dadc366dd926cebcea\r\n\r\nThe intention is to check that `other` is the same class as `self` (to prevent adding `BTC` to `USD` by accident). That check alone should imply that `other` is a subclass of `Currency` (since the `__add__` method is defined on `Currency`), but it looks like I have to explicitly make both assertions, and make the `Currency` assertion **after** the `self.__class__` one in order for mypy to narrow the type correctly.\r\n\r\nCorrect me if I'm mistaken, but I think there may be two bugs here:\r\n - The order of `isinstance` checks shouldn't matter (especially not in an `and` expression)\r\n - The `isinstance(other, Currency)` should not even be necessary, as it's implied that other is a `Currency` if `other.__class__ == self.__class__`\r\n\r\nThis issue discusses a vaguely similar type-narrowing problem, but I'm not sure if it's actually related: https://github.com/python/mypy/issues/2776\r\n ","author":{"url":"https://github.com/pirate","@type":"Person","name":"pirate"},"datePublished":"2018-01-03T06:01:21Z","url":"https://github.com/python/mypy/issues/4422"}
| 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:ca5315c1-5ebd-9e08-e66f-ccc9acbbc660 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF32:1F3799:298DF1A:3684C08:6B000C50 |
| html-safe-nonce | 9a1b078b0c6248204bb203e521c82fc31ab5e506f5f6c4c64eaebf9bfa46f5b5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjMyOjFGMzc5OToyOThERjFBOjM2ODRDMDg6NkIwMDBDNTAiLCJ2aXNpdG9yX2lkIjoiNjkzMjI1MDc1MTM5Nzc5Mjg0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fd0c00a6c45da1919b1a58072ea59a872e33af08c81fcadaef4eb4c17f4e7a70 |
| hovercard-subject-tag | issue:285605017 |
| 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/4422/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e3e30b28310f9de6cfe513e80901bb1588ea6674d3b334a725bc86a4be67485/python/mypy/issues/4422 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e3e30b28310f9de6cfe513e80901bb1588ea6674d3b334a725bc86a4be67485/python/mypy/issues/4422 |
| og:image:alt | This works fine: def __add__(self, other: object) -> 'Currency': if isinstance(other, self.__class__) and isinstance(other, Currency): return self.__class__(other.amt + self.amt) return NotImplemen... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pirate |
| 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