Title: `Callable` not deduced correctly · Issue #16868 · python/mypy · GitHub
Open Graph Title: `Callable` not deduced correctly · Issue #16868 · python/mypy
X Title: `Callable` not deduced correctly · Issue #16868 · python/mypy
Description: I have a function add(x,y) that adds two floats, and that that the user can specify. If no custom function add is given, default_add is used. If it is given, I'm adding a little wrapper code to assert stuff: from typing import Callable d...
Open Graph Description: I have a function add(x,y) that adds two floats, and that that the user can specify. If no custom function add is given, default_add is used. If it is given, I'm adding a little wrapper code to ass...
X Description: I have a function add(x,y) that adds two floats, and that that the user can specify. If no custom function add is given, default_add is used. If it is given, I'm adding a little wrapper code to...
Opengraph URL: https://github.com/python/mypy/issues/16868
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`Callable` not deduced correctly","articleBody":"I have a function `add(x,y)` that adds two floats, and that that the user can specify. If no custom function `add` is given, `default_add` is used. If it _is_ given, I'm adding a little wrapper code to assert stuff:\r\n\r\n```python\r\nfrom typing import Callable\r\n\r\n\r\ndef default_add(x: float, y: float, extra: float = 0) -> float:\r\n return x + y + extra\r\n\r\n\r\ndef wrap_add(\r\n add: Callable[[float, float], float],\r\n) -> Callable[[float, float], float]:\r\n def _add(x: float, y: float) -> float:\r\n assert isinstance(x, (float, int))\r\n assert isinstance(y, (float, int))\r\n\r\n z = add(x, y)\r\n\r\n assert isinstance(z, (float, int))\r\n return z\r\n\r\n return _add\r\n\r\n\r\nfun: Callable[[float, float], float] | None = None\r\n\r\nfun2 = default_add if fun is None else wrap_add(fun)\r\n\r\nfun2(3, 5)\r\n```\r\n\r\nRunning mypy on the above gives\r\n\r\n```\r\na.py:27: error: Cannot call function of unknown type [operator]\r\n```\r\n\r\nWhen adding explicit typing to `fun2`,\r\n```python\r\nfun2: Callable[[float, float], float] = default_add if fun is None else wrap_add(fun)\r\n```\r\n\r\none incorrectly gets\r\n\r\n```\r\na.py:25: error: Incompatible types in assignment (expression has type \"function\", variable has type \"Callable[[float, float], float]\") \r\n```\r\n\r\n---\r\n\r\n- Mypy version used: 1.8.0\r\n- Mypy command-line flags: -\r\n- Mypy configuration options from `mypy.ini` (and other config files): -\r\n- Python version used: 3.11.6","author":{"url":"https://github.com/nschloe","@type":"Person","name":"nschloe"},"datePublished":"2024-02-04T17:40:55Z","url":"https://github.com/python/mypy/issues/16868"}
| 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:ac483043-3a77-d417-5a0a-114150c7ad79 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB34:2A087:EE4E5:140973:6B001D88 |
| html-safe-nonce | 52c3a4e52aa749b029acb1a9a624850ecda14d13065c1cdb2dac4a2306282270 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjM0OjJBMDg3OkVFNEU1OjE0MDk3Mzo2QjAwMUQ4OCIsInZpc2l0b3JfaWQiOiI0NjgyNjkzODU5ODI4ODMzNjcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 26927030bb0f5ed66d5af7f1bf57945377cc19b8ca0b3588b82501876cf66202 |
| hovercard-subject-tag | issue:2117226963 |
| 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/16868/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba01e27f6a0df72cbbf71de70be9c2cdd7bb9946dfd8b07f43e90201df72dd85/python/mypy/issues/16868 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba01e27f6a0df72cbbf71de70be9c2cdd7bb9946dfd8b07f43e90201df72dd85/python/mypy/issues/16868 |
| og:image:alt | I have a function add(x,y) that adds two floats, and that that the user can specify. If no custom function add is given, default_add is used. If it is given, I'm adding a little wrapper code to ass... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nschloe |
| 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