Title: [arg-type] wrong argument type in generic function with union · Issue #17654 · python/mypy · GitHub
Open Graph Title: [arg-type] wrong argument type in generic function with union · Issue #17654 · python/mypy
X Title: [arg-type] wrong argument type in generic function with union · Issue #17654 · python/mypy
Description: Bug Report I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a object. But if i save the result in a variabl...
Open Graph Description: Bug Report I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a objec...
X Description: Bug Report I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a objec...
Opengraph URL: https://github.com/python/mypy/issues/17654
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[arg-type] wrong argument type in generic function with union","articleBody":"\u003c!--\r\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\r\nPlease consider:\r\n\r\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\r\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\r\n- asking on gitter chat: https://gitter.im/python/typing\r\n--\u003e\r\n\r\n**Bug Report**\r\n\r\n\u003c!--\r\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\r\n\r\nIf the project you encountered the issue in is open source, please provide a link to the project.\r\n--\u003e\r\n\r\nI have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a object. But if i save the result in a variable and then pass it in function, mypy won't show error. It's very counterintuitive\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import *\r\n\r\nT = TypeVar(\"T\")\r\nD = TypeVar(\"D\")\r\n\r\n\r\nclass Key: ...\r\n\r\n\r\nclass FancyKey(Generic[T]): ...\r\n\r\n\r\ndef get(key: FancyKey[T], default: D) -\u003e D | T:\r\n ...\r\n return default\r\n\r\n\r\ndef foo(value: object) -\u003e Any: ...\r\n\r\n\r\nkey = FancyKey[Key]()\r\nfoo(\r\n get(key, default=None)\r\n) # mypy: Argument 1 to \"bar\" of \"Bar\" has incompatible type \"Foo[Lab]\"; expected \"Foo[object]\" [arg-type]\r\n\r\ntreasure = get(key, default=None) # no error\r\nfoo(treasure) # no error\r\n```\r\n[Playground](https://mypy-play.net/?mypy=latest\u0026python=3.12\u0026gist=ed6b7f7a1d748e1b5091ba3c3e972a76)\r\n\r\n**Actual Behavior**\r\n\r\nmypy: Argument 1 to \"bar\" of \"Bar\" has incompatible type \"Foo[Lab]\"; expected \"Foo[object]\" [arg-type]\r\n\r\nIf a return-type function won't be generic or function will return not a Union - no error will be received\r\nIf type of `value` change to Any - no error will be received\r\n\r\nMaybe this issue related with [another](https://github.com/python/mypy/issues/17025)\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in --\u003e\r\n\r\n- Mypy version used: 1.11.1\r\n- Mypy command-line flags: no flags\r\n- Mypy configuration options from `mypy.ini` (and other config files): no config\r\n- Python version used: 3.12\r\n\r\n","author":{"url":"https://github.com/RoketFlame","@type":"Person","name":"RoketFlame"},"datePublished":"2024-08-09T09:29:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/17654/mypy/issues/17654"}
| 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:2f85844c-0d72-daf0-49d8-9e5198984b98 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB36:28C0E5:24B32B3:2FE728A:6A5CB621 |
| html-safe-nonce | 703882d9e3d330a5c9afa4973dfd1d206cf1cd5d72308e6a6ff2a69787873f45 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjM2OjI4QzBFNToyNEIzMkIzOjJGRTcyOEE6NkE1Q0I2MjEiLCJ2aXNpdG9yX2lkIjoiMTg5NjgwMzIxODA5OTIxNTY5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3d909964cb6564c61552fb0fd54417b47484d1d24ca6b6c1bd89277ca2e7fd14 |
| hovercard-subject-tag | issue:2457510276 |
| 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/17654/issue_layout |
| twitter:image | https://opengraph.githubassets.com/374419164c8b2feccd9d8f86734f6ac8314dc3aad013902468b7d37a7fde7715/python/mypy/issues/17654 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/374419164c8b2feccd9d8f86734f6ac8314dc3aad013902468b7d37a7fde7715/python/mypy/issues/17654 |
| og:image:alt | Bug Report I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a objec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RoketFlame |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width