Title: Type[object] isn't hashable, and can't be asserted to be Hashable · Issue #12993 · python/mypy · GitHub
Open Graph Title: Type[object] isn't hashable, and can't be asserted to be Hashable · Issue #12993 · python/mypy
X Title: Type[object] isn't hashable, and can't be asserted to be Hashable · Issue #12993 · python/mypy
Description: Bug Report Context: mypy doesn't regard Type[object] as Hashable. (It is possible with metaclasses that this might be the case, LSP concerns aside.) It rightly complains when Type[object] is used where a Hashable is required. The issue i...
Open Graph Description: Bug Report Context: mypy doesn't regard Type[object] as Hashable. (It is possible with metaclasses that this might be the case, LSP concerns aside.) It rightly complains when Type[object] is used w...
X Description: Bug Report Context: mypy doesn't regard Type[object] as Hashable. (It is possible with metaclasses that this might be the case, LSP concerns aside.) It rightly complains when Type[object] is us...
Opengraph URL: https://github.com/python/mypy/issues/12993
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type[object] isn't hashable, and can't be asserted to be Hashable","articleBody":"\r\n**Bug Report**\r\n\r\nContext: mypy doesn't regard `Type[object]` as `Hashable`. (It is possible with metaclasses that this might be the case, LSP concerns aside.) It rightly complains when `Type[object]` is used where a `Hashable` is required.\r\n\r\nThe issue is that mypy assumes `Type[object]` _cannot_ be `Hashable`, even following an assertion, making the above difficult to work around.\r\n\r\n**To Reproduce**\r\n\r\nhttps://mypy-play.net/?mypy=master&python=3.10&gist=3a8ec03c4472430ecbaeaf397dbc0acd\r\n\r\n```python\r\n\r\nimport collections\r\nimport functools\r\nimport typing\r\nfrom typing import (\r\n Type,\r\n )\r\n\r\n\r\n@functools.lru_cache(maxsize=None)\r\ndef cached_function(type_: Type[object]) -> bool:\r\n # expensive computation here\r\n _ = type_\r\n return True\r\n\r\n\r\ndef function1(type_: Type[object]) -> None:\r\n\r\n # it's theoretically possible for a Type[...] object to be\r\n # unhashable (although this would seem to violate the Liskov\r\n # Substitution Principle, LSP)\r\n assert cached_function(type_)\r\n # actual, as expected:\r\n # error: Argument 1 to \"__call__\" of\r\n # \"_lru_cache_wrapper\" has incompatible type\r\n # \"Type[object]\"; expected \"Hashable\" [arg-type]\r\n\r\ndef function1a(type_: Type[object]) -> None:\r\n\r\n # we attempt to assure mypy that 'type_' is Hashable\r\n assert isinstance(type_, typing.Hashable)\r\n reveal_type(type_)\r\n # expected: revealed type is: ...\r\n # actual: prints nothing; mypy believes this is unreachable\r\n\r\n assert cached_function(type_)\r\n # expected: no warning\r\n # actual: mypy believes this is unreachable\r\n\r\ndef function1b(type_: Type[object]) -> None:\r\n\r\n # we attempt to assure mypy that 'type_' is Hashable\r\n assert isinstance(type_, collections.abc.Hashable)\r\n reveal_type(type_)\r\n # expected: revealed type is: ...\r\n # actual: prints nothing; mypy believes this is unreachable\r\n\r\n assert cached_function(type_)\r\n # expected: no warning\r\n # actual: mypy believes this is unreachable\r\n```\r\n\r\n**Expected Behavior**\r\n \r\nAdding `assert isinstance(..., Hashable)` should work around this problem.\r\n\r\n**Actual Behavior**\r\n\r\nAsserting that a variable having type `Type[object]` is `Hashable` causes mypy to conclude the code is unreachable.\r\n\r\n**Your Environment**\r\n- Mypy version used: 0.961 and master (on mypy-play.net)\r\n- Mypy command-line flags: (none required; `--warn-unreachable` adds insight)\r\n- Mypy configuration options from `mypy.ini` (and other config files): (none)\r\n- Python version used: 3.8, 3.10\r\n- Operating system and version: macOS 10.14, mypy-play.net\r\n","author":{"url":"https://github.com/finite-state-machine","@type":"Person","name":"finite-state-machine"},"datePublished":"2022-06-17T15:21:10Z","url":"https://github.com/python/mypy/issues/12993"}
| 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:30cca118-66c5-6dc7-c7a6-63d442b66a1f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D5AA:11BB24:245AEA9:316DD5A:6B002EAA |
| html-safe-nonce | f22fd72c24a338ccb893b89e7127aaaa368baf0f4435cbcfd90916eb5d27f783 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUFBOjExQkIyNDoyNDVBRUE5OjMxNkRENUE6NkIwMDJFQUEiLCJ2aXNpdG9yX2lkIjoiODc1ODE2MzEzNzAyMDA0NzAxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 53cce9a36bd6580e3589c6f2614464bf2131fea5440029eb0bc5ea6459324ae1 |
| hovercard-subject-tag | issue:1275159287 |
| 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/12993/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5506091966bf8faef69ce8060ad79dc4e3dd12c705539e6433f65e8f335bac81/python/mypy/issues/12993 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5506091966bf8faef69ce8060ad79dc4e3dd12c705539e6433f65e8f335bac81/python/mypy/issues/12993 |
| og:image:alt | Bug Report Context: mypy doesn't regard Type[object] as Hashable. (It is possible with metaclasses that this might be the case, LSP concerns aside.) It rightly complains when Type[object] is used w... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | finite-state-machine |
| 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