Title: Dict arguments that contain a subclass are not handled by Mypy correctly · Issue #9738 · python/mypy · GitHub
Open Graph Title: Dict arguments that contain a subclass are not handled by Mypy correctly · Issue #9738 · python/mypy
X Title: Dict arguments that contain a subclass are not handled by Mypy correctly · Issue #9738 · python/mypy
Description: I have the following example piece of code from typing import Any, Dict class P: pass class C(P): pass class A: def __init__(self, x: Dict[P, Any]): pass class B(A): def __init__(self, x: Dict[C, Any]): super().__init__(x) When running m...
Open Graph Description: I have the following example piece of code from typing import Any, Dict class P: pass class C(P): pass class A: def __init__(self, x: Dict[P, Any]): pass class B(A): def __init__(self, x: Dict[C, A...
X Description: I have the following example piece of code from typing import Any, Dict class P: pass class C(P): pass class A: def __init__(self, x: Dict[P, Any]): pass class B(A): def __init__(self, x: Dict[C, A...
Opengraph URL: https://github.com/python/mypy/issues/9738
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Dict arguments that contain a subclass are not handled by Mypy correctly","articleBody":"I have the following example piece of code\r\n\r\n```\r\nfrom typing import Any, Dict\r\n\r\n\r\nclass P:\r\n pass\r\n\r\nclass C(P):\r\n pass\r\n\r\nclass A:\r\n def __init__(self, x: Dict[P, Any]):\r\n pass\r\n\r\nclass B(A):\r\n def __init__(self, x: Dict[C, Any]):\r\n super().__init__(x)\r\n```\r\n\r\nWhen running mypy on it, this warning appears:\r\n\r\n\u003e**Argument 1 to \"__init__\" of \"A\" has incompatible type \"Dict[C, Any]\"; expected \"Dict[P, Any]\"**\r\n\r\nWhy is this happening? If I do the same, but without using `Dict[]`, ie:\r\n```\r\nclass P:\r\n pass\r\n\r\nclass C(P):\r\n pass\r\n\r\nclass A:\r\n def __init__(self, x: P):\r\n pass\r\n\r\nclass B(A):\r\n def __init__(self, x: C):\r\n super().__init__(x)\r\n```\r\n\r\n I get no complains. I tried using `Mapping[]` instead of `Dict[]` but the same warning occurs. \r\n\r\nThis happens a lot in the code, as I am subclassing from a Library class, I cannot use cast every time, it's counter productive\r\n\r\n**Environment**\r\n\r\n- Mypy version used: 0.790\r\n- Python version used: 3.8.5\r\n- Operating system and version: Ubuntu 20.04\r\n\r\n\r\n","author":{"url":"https://github.com/VasLem","@type":"Person","name":"VasLem"},"datePublished":"2020-11-19T20:08:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/9738/mypy/issues/9738"}
| 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:e723f5f0-003c-b544-3c5e-f54dcc20784e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC2E:1ABADA:2012B1:2B681E:6A625FE5 |
| html-safe-nonce | cfb5413f705abb68d5dcc18d415e553e42fe2b1b93311b9981b6abbbd2bef08d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzJFOjFBQkFEQToyMDEyQjE6MkI2ODFFOjZBNjI1RkU1IiwidmlzaXRvcl9pZCI6IjcwMDQ4MDQyNzI0MTQxNTQ3MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9d68846539c3b1b2f556124cadbd2118f5ce34848c1dcb58646a615383834f3b |
| hovercard-subject-tag | issue:746886099 |
| 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/9738/issue_layout |
| twitter:image | https://opengraph.githubassets.com/db7572b5887f216ead1e73f262b9798b37d387ec080645a890cbb384bec4b3d7/python/mypy/issues/9738 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/db7572b5887f216ead1e73f262b9798b37d387ec080645a890cbb384bec4b3d7/python/mypy/issues/9738 |
| og:image:alt | I have the following example piece of code from typing import Any, Dict class P: pass class C(P): pass class A: def __init__(self, x: Dict[P, Any]): pass class B(A): def __init__(self, x: Dict[C, A... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | VasLem |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6d5cd56dfa7e7ed6718ba879da3c0d884bd8b5fecd3883b5f00dcb66b8b8680f |
| 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 | d28abcf30a4025dfeef7673e1509edbc5d402c97 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width