Title: Misleading warning for generic argument type on wrong return type · Issue #12156 · python/mypy · GitHub
Open Graph Title: Misleading warning for generic argument type on wrong return type · Issue #12156 · python/mypy
X Title: Misleading warning for generic argument type on wrong return type · Issue #12156 · python/mypy
Description: Bug Report It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error. To Reproduce Run mypy a.py with import typing as t T = t.TypeVar('T') U =...
Open Graph Description: Bug Report It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error. To Reproduce Run mypy a.py with i...
X Description: Bug Report It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error. To Reproduce Run mypy a.py with i...
Opengraph URL: https://github.com/python/mypy/issues/12156
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Misleading warning for generic argument type on wrong return type","articleBody":"**Bug Report**\r\n\r\nIt appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error.\r\n\r\n\r\n\r\n\r\n\r\n**To Reproduce**\r\n\r\nRun `mypy a.py` with\r\n\r\n```py\r\nimport typing as t\r\n\r\nT = t.TypeVar('T')\r\nU = t.TypeVar('U')\r\n\r\ndef coalesce(a: T | None, b: U) -\u003e t.Union[T, U]:\r\n if a is None:\r\n return b\r\n return a\r\n\r\ndef foo() -\u003e int:\r\n return coalesce(42, None)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nLooking at this, I would expect Mypy to complain that the declared return value of `foo()` (being `int`) does not match what is actually returned (`int | None`). \r\n\r\n**Actual Behavior**\r\n\r\nIt appears that Mypy propagates the return type into the `coalesce()` type arguments instead and expects them both to be `int`, thus complaining about the argument type instead.\r\n\r\n```\r\na.py: note: In function \"foo\":\r\na.py:15: error: Argument 2 to \"coalesce\" has incompatible type \"None\"; expected \"int\" [arg-type]\r\n return coalesce(42, None)\r\n ^\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nI find this rather unintuitive. My suggestion would be to make Mypy complain about the type of the returned value instead of the argument type.\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: 0.931\r\n- Mypy command-line flags: /\r\n- Mypy configuration options from `pyproject.toml` (and other config files): \r\n\r\n ```toml\r\n [tool.mypy]\r\n pretty = true\r\n warn_redundant_casts = true\r\n #warn_unused_ignores = true\r\n warn_no_return = true\r\n warn_unreachable = true\r\n show_error_context = true\r\n show_error_codes = true\r\n ```\r\n\r\n- Python version used: 3.10.2\r\n- Operating system and version: WSL 2, Ubuntu 20\r\n","author":{"url":"https://github.com/NiklasRosenstein","@type":"Person","name":"NiklasRosenstein"},"datePublished":"2022-02-09T12:21:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/12156/mypy/issues/12156"}
| 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:1745100f-944e-8c3b-3c21-90c73adc2d37 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1F2:2566D5:629DECA:828CEB7:6A5DD4AA |
| html-safe-nonce | 1991841f3985c0acd64da045e94f4213010fa41916782a077155722ebd8e5b70 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUYyOjI1NjZENTo2MjlERUNBOjgyOENFQjc6NkE1REQ0QUEiLCJ2aXNpdG9yX2lkIjoiNjkyMzIxNDExNDQyNjU3Mzk5NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b445a6cf1da22f95928b3dd94a9d1935636feb884d2f6fb02eb51d5a214ed4db |
| hovercard-subject-tag | issue:1128471737 |
| 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/12156/issue_layout |
| twitter:image | https://opengraph.githubassets.com/34077d2a2606f0e0b63baf8d32aa5405f040ddc440ae4ecabde04dad0e43478e/python/mypy/issues/12156 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/34077d2a2606f0e0b63baf8d32aa5405f040ddc440ae4ecabde04dad0e43478e/python/mypy/issues/12156 |
| og:image:alt | Bug Report It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error. To Reproduce Run mypy a.py with i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | NiklasRosenstein |
| 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