Title: Returning T instead of UserDefinedClass[T] is not reported as an error · Issue #2905 · python/mypy · GitHub
Open Graph Title: Returning T instead of UserDefinedClass[T] is not reported as an error · Issue #2905 · python/mypy
X Title: Returning T instead of UserDefinedClass[T] is not reported as an error · Issue #2905 · python/mypy
Description: At first glance, the following code should not pass type verification because f and g return or yield T instead of Node[T]: from typing import TypeVar, Generic, Iterator T = TypeVar('T') class Node(Generic[T]): def __init__(self, value: ...
Open Graph Description: At first glance, the following code should not pass type verification because f and g return or yield T instead of Node[T]: from typing import TypeVar, Generic, Iterator T = TypeVar('T') class Node...
X Description: At first glance, the following code should not pass type verification because f and g return or yield T instead of Node[T]: from typing import TypeVar, Generic, Iterator T = TypeVar('T') cl...
Opengraph URL: https://github.com/python/mypy/issues/2905
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Returning T instead of UserDefinedClass[T] is not reported as an error","articleBody":"At first glance, the following code should not pass type verification because `f` and `g` return or yield `T` instead of `Node[T]`:\r\n\r\n```\r\nfrom typing import TypeVar, Generic, Iterator\r\n\r\nT = TypeVar('T')\r\nclass Node(Generic[T]):\r\n def __init__(self, value: T) -> None:\r\n self.value = value\r\n\r\ndef f(v: Node) -> Node:\r\n return v.value\r\n\r\ndef g(v: Node) -> Iterator[Node]:\r\n yield v.value\r\n```\r\n\r\nHowever, `mypy --strict --python-version 3.6` doesn't report any errors. It does produce a warning:\r\n\r\n> Returning `Any` from function with declared return type `node.Node[Any]` \r\n\r\nbut even the warning is only reported for `f()`, not for `g()`.\r\n\r\nTo be fair, there are ways to call these functions that doesn't violate type expectations, for example `f(Node(Node(1)))` would return `Node(1)` which fits its promised return type. But I was hoping `mypy` might notice that the only way this code would be correct is if the argument to `f()` is a `Node[Node]` rather than just arbitrary `Node`. And so I was expecting `mypy` to force me to write:\r\n\r\n```\r\ndef f(v: Node[Node]) -> Node:\r\n return v.value\r\n\r\ndef g(v: Node[Node]) -> Node:\r\n yield v.value\r\n```\r\n\r\nif that's what I meant of course. (Although it's much more likely that it's actually a bug, and that I meant to `return v` rather than `return v.value`; in that case, `mypy` would help me catch that bug.)\r\n\r\nFurthermore, if I add the following code at the end of the original code snippet:\r\n\r\n```\r\nv = Node(1)\r\nf(v)\r\nfor x in g(v):\r\n pass\r\n```\r\n\r\n`mypy` should be able to see that something is definitely wrong; at this point it's completely clear that `f(v)` won't return the promised `Node`, and `g(v)` won't yield one. But adding those lines does not add any new error or warning messages.","author":{"url":"https://github.com/pkch","@type":"Person","name":"pkch"},"datePublished":"2017-02-24T19:20:34Z","url":"https://github.com/python/mypy/issues/2905"}
| 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:6d08be37-9194-892b-96be-23748ffb3a14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C36E:157BA1:4DD551:68D4A2:6B003002 |
| html-safe-nonce | ea0902db508ab6c247c52e78a15dd1f4fc49d16cf95793511b64fde06942fd77 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzZFOjE1N0JBMTo0REQ1NTE6NjhENEEyOjZCMDAzMDAyIiwidmlzaXRvcl9pZCI6Ijg5MTk4MTUzNTA4NzUzMzY3MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 44f71636debb5bd078cbe746d237fef5c684f7e4627301b183950460f113802e |
| hovercard-subject-tag | issue:210134938 |
| 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/2905/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f9030c74ae01e91b0e074634793cab44580b1ebe9c527918327ed86b8efcd49b/python/mypy/issues/2905 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f9030c74ae01e91b0e074634793cab44580b1ebe9c527918327ed86b8efcd49b/python/mypy/issues/2905 |
| og:image:alt | At first glance, the following code should not pass type verification because f and g return or yield T instead of Node[T]: from typing import TypeVar, Generic, Iterator T = TypeVar('T') class Node... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pkch |
| 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