Title: Type-narrowing from parent to child class produces `Never` if inheriting from a concrete version of a generic parent class. · Issue #16494 · python/mypy · GitHub
Open Graph Title: Type-narrowing from parent to child class produces `Never` if inheriting from a concrete version of a generic parent class. · Issue #16494 · python/mypy
X Title: Type-narrowing from parent to child class produces `Never` if inheriting from a concrete version of a generic parent class. · Issue #16494 · python/mypy
Description: Bug Report If I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child class. Trying to do the narrowing unexpect...
Open Graph Description: Bug Report If I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child cl...
X Description: Bug Report If I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child cl...
Opengraph URL: https://github.com/python/mypy/issues/16494
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type-narrowing from parent to child class produces `Never` if inheriting from a concrete version of a generic parent class.","articleBody":"**Bug Report**\r\n\r\nIf I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child class. Trying to do the narrowing unexpectedly produces `Never`.\r\n\r\nIn my original code (not included here), this type-narrowing is after business logic that ensures that the object is an instance of the child class, and therefore the generic type is of the correct type. Additionally, this narrowing was done on `self` in a method in the parent class, so using `Parent[Any]` or `Parent[int]` isn't an option, since that requires changing the type of `self`.\r\n\r\nPossibly related to #14785 \r\n\r\n**To Reproduce**\r\n\r\nRun mypy on the following code. Or use this [playground link](https://mypy-play.net/?mypy=latest&python=3.11&gist=20dc444401116fbaed1f07be7fd8b767).\r\n\r\n```python\r\nfrom typing import Generic, TypeVar, reveal_type\r\nT = TypeVar(\"T\")\r\n\r\nclass Parent(Generic[T]):\r\n pass\r\nclass Child(Parent[int]):\r\n pass\r\n\r\ndef foo(thing: Parent[T]) -> None:\r\n assert isinstance(thing, Child)\r\n reveal_type(thing)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nThe revealed type should be `Child`.\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:11: note: Revealed type is \"Never\"\r\nSuccess: no issues found in 1 source file\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Bug is reproducible in the mypy Playground.\r\n- Mypy version used: \"latest (1.7.0)\" and \"master branch\" both reproduce the bug\r\n- Python version used: 3.11\r\n- Adding the `--warn-unreachable` option produces the same output.","author":{"url":"https://github.com/theemathas","@type":"Person","name":"theemathas"},"datePublished":"2023-11-15T01:06:13Z","url":"https://github.com/python/mypy/issues/16494"}
| 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:627f0f27-a25f-cb9b-2fbd-73562d8ec594 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC26:3DAD59:19CD3FA:2217A0A:6B0026A7 |
| html-safe-nonce | 409f52ecd7d2850137b45a5402fd50cc0e6657756fbd777955e2886a2ce628e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzI2OjNEQUQ1OToxOUNEM0ZBOjIyMTdBMEE6NkIwMDI2QTciLCJ2aXNpdG9yX2lkIjoiNzQ0NDE2MTUwODIwMTczNTg0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3d5799d5d53b45b30060e1ced02a868f10b1db9addbd7bce9286fb4762343927 |
| hovercard-subject-tag | issue:1993837262 |
| 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/16494/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e1866a49f9becb4ede371eaf76fce6b250b9319cd72df683d25bc6ff711ba571/python/mypy/issues/16494 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e1866a49f9becb4ede371eaf76fce6b250b9319cd72df683d25bc6ff711ba571/python/mypy/issues/16494 |
| og:image:alt | Bug Report If I have a generic parent class, and I have a child class inheriting from a concrete version of the parent class, then I cannot type-narrow from the generic parent class to the child cl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | theemathas |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width