Title: Bug: `issubclass()` doesn't narrow TypeVar type with Protocol · Issue #8556 · python/mypy · GitHub
Open Graph Title: Bug: `issubclass()` doesn't narrow TypeVar type with Protocol · Issue #8556 · python/mypy
X Title: Bug: `issubclass()` doesn't narrow TypeVar type with Protocol · Issue #8556 · python/mypy
Description: Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with mypy --python-version=3.8 reproduce-issubclass-bug.py. The issue seems similar to #7920. ...
Open Graph Description: Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with mypy --python-version=3.8 reproduce-issubclass-bu...
X Description: Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with mypy --python-version=3.8 reproduce-issubclass-bu...
Opengraph URL: https://github.com/python/mypy/issues/8556
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: `issubclass()` doesn't narrow TypeVar type with Protocol","articleBody":"Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with `mypy --python-version=3.8 reproduce-issubclass-bug.py`. The issue seems similar to https://github.com/python/mypy/issues/7920. Tested on both 0.770 and master (0.770+dev.afe0667bbe85e048b6bb4e3fc1b7d5a375b7d01f).\r\n\r\n```python\r\nfrom typing import Any\r\nfrom typing import Protocol\r\nfrom typing import runtime_checkable\r\nfrom typing import Type\r\nfrom typing import TypeVar\r\nfrom typing import Union\r\n\r\nT = TypeVar(\"T\", bound=Any)\r\n\r\n\r\n@runtime_checkable\r\nclass Parsable(Protocol):\r\n @classmethod\r\n def parse(cls: Type[T], data: Any) -> T:\r\n ...\r\n\r\n\r\nR = TypeVar(\"R\", bound=Union[Parsable, str])\r\n\r\n\r\ndef parse_object(response_type: Type[R], item: Any) -> R:\r\n if issubclass(response_type, Parsable):\r\n # note: Revealed type is 'Type[reproduce-issubclass-bug.Parsable]'\r\n reveal_type(response_type)\r\n # error: Incompatible return value type (got \"Parsable\", expected \"R\")\r\n return response_type.parse(item)\r\n # note: Revealed type is 'Type[R`-1]'\r\n reveal_type(response_type)\r\n # error: Too many arguments for \"Parsable\"\r\n # error: Incompatible return value type (got \"Union[Parsable, str]\", expected \"R\")\r\n return response_type(item)\r\n```\r\n\r\nOutput:\r\n\r\n```\r\nreproduce-issubclass-bug.py:25: note: Revealed type is 'Type[reproduce-issubclass-bug.Parsable]'\r\nreproduce-issubclass-bug.py:27: error: Incompatible return value type (got \"Parsable\", expected \"R\")\r\nreproduce-issubclass-bug.py:29: note: Revealed type is 'Type[R`-1]'\r\nreproduce-issubclass-bug.py:32: error: Too many arguments for \"Parsable\"\r\nreproduce-issubclass-bug.py:32: error: Incompatible return value type (got \"Union[Parsable, str]\", expected \"R\")\r\nFound 3 errors in 1 file (checked 1 source file)\r\n```\r\n","author":{"url":"https://github.com/antonagestam","@type":"Person","name":"antonagestam"},"datePublished":"2020-03-18T14:22:50Z","url":"https://github.com/python/mypy/issues/8556"}
| 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:d444b79d-3c37-3caf-02f4-77d1ca5bb975 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C34C:3E7030:B7C5B4:F66B60:6B004E85 |
| html-safe-nonce | f953a450fda41461195907f49852c517d8c31354ed5939250633ce26de74fd93 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzRDOjNFNzAzMDpCN0M1QjQ6RjY2QjYwOjZCMDA0RTg1IiwidmlzaXRvcl9pZCI6IjEzMjk0MDc1NjY2MzUwOTM2MzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f1bd1b99a112c75562cbe63f3da2640d3b363accad6165e27885300ab8d2d007 |
| hovercard-subject-tag | issue:583764772 |
| 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/8556/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c42a037d0b167a90e6062f665895c5b9f49e0ebba9ef9f1ef40edae08532c674/python/mypy/issues/8556 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c42a037d0b167a90e6062f665895c5b9f49e0ebba9ef9f1ef40edae08532c674/python/mypy/issues/8556 |
| og:image:alt | Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with mypy --python-version=3.8 reproduce-issubclass-bu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | antonagestam |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width