Title: Float multiplication of numpy.ndarray in lambda is incorrectly analysed · Issue #8001 · python/mypy · GitHub
Open Graph Title: Float multiplication of numpy.ndarray in lambda is incorrectly analysed · Issue #8001 · python/mypy
X Title: Float multiplication of numpy.ndarray in lambda is incorrectly analysed · Issue #8001 · python/mypy
Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues Please provide more information to help ...
Open Graph Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ...
X Description: Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ...
Opengraph URL: https://github.com/python/mypy/issues/8001
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Float multiplication of numpy.ndarray in lambda is incorrectly analysed","articleBody":"Note: if you are reporting a wrong signature of a function or a class in\r\nthe standard library, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n\r\nPlease provide more information to help us understand the issue:\r\n\r\n* Are you reporting a bug, or opening a feature request?\r\n\r\nBug.\r\n\r\n* Please insert below the code you are checking with mypy,\r\n or a mock-up repro if the source is private. We would appreciate\r\n if you try to simplify your case to a minimal repro.\r\n\r\n```python\r\nfrom typing import NewType, Tuple, Deque\r\n\r\nimport numpy as np\r\n\r\n\r\nclass Point(np.ndarray):\r\n def __new__(cls, x: float, y: float) -> np.ndarray:\r\n return np.array((x, y), dtype=np.float32)\r\n\r\n\r\nNose = NewType(\"Nose\", Tuple[Point, ...])\r\n\r\n\r\ndef average_position(positions: Deque) -> Nose:\r\n factor = 1.0 / len(positions)\r\n point_sum = [Point(0.0, 0.0) for _ in range(10)]\r\n for nose in positions:\r\n for i, point in enumerate(nose):\r\n point_sum[i] += point\r\n avg_pos = Nose(tuple(map(lambda p: factor * p, point_sum)))\r\n return avg_pos\r\n\r\n\r\ndef average_position_with_multiply(positions: Deque) -> Nose:\r\n factor = 1.0 / len(positions)\r\n point_sum = [Point(0.0, 0.0) for _ in range(10)]\r\n for nose in positions:\r\n for i, point in enumerate(nose):\r\n point_sum[i] += point\r\n avg_pos = Nose(tuple(map(lambda p: np.multiply(factor, p), point_sum)))\r\n return avg_pos\r\n\r\n```\r\n\r\n* What is the actual behavior/output?\r\n\r\nMypy gives the errors below:\r\n```python\r\nbug.py:20: error: Argument 1 to \"map\" has incompatible type \"Callable[[Point], float]\"; expected \"Callable[[Point], Point]\"\r\nbug.py:20: error: Incompatible return value type (got \"float\", expected \"Point\")\r\nFound 2 errors in 1 file (checked 1 source file)\r\n\r\n```\r\n\r\nIn the lambda in `average_position()`, mypy incorrectly (?) determines the type as `Callable[[Point], float]`, while using the NumPy function `np.multiply()` (which does the same thing) gives no error. Both functions output the same correct value.\r\n\r\n* What is the behavior/output you expect?\r\nThere should be no error.\r\n\r\n* What are the versions of mypy and Python you are using?\r\n Do you see the same issue after installing mypy from Git master?\r\n\r\n```plain\r\nmypy==0.740\r\nPython 3.8.0\r\n```\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\n\r\n```ini\r\n[mypy]\r\npython_version = 3.8\r\n\r\n[mypy-cv2,dlib,numpy]\r\nignore_missing_imports = True\r\n```\r\n\r\nI am unsure if this is actually an error, or if I've done something wrong somewhere, but both functions run and output correctly.\r\n","author":{"url":"https://github.com/jamesohortle","@type":"Person","name":"jamesohortle"},"datePublished":"2019-11-22T07:56:04Z","url":"https://github.com/python/mypy/issues/8001"}
| 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:70659a0e-987f-8413-a8e2-492df4056212 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBD0:36D598:3FB35B:557E31:6B002A91 |
| html-safe-nonce | 4a655173ff711918f7badabfbe77a245293f4624a4f3ae94117bc8e812286c2d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkQwOjM2RDU5ODozRkIzNUI6NTU3RTMxOjZCMDAyQTkxIiwidmlzaXRvcl9pZCI6IjUxOTg5OTI3Njc1MzkyMjcyODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 339786b666fd61685a7c73027cdbe478fe73f3a63664060bae65bf7e9ecb317d |
| hovercard-subject-tag | issue:527043946 |
| 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/8001/issue_layout |
| twitter:image | https://opengraph.githubassets.com/299b7ad63bfcb816a5a9a1c1c368c2b5cc242bfabc8c60491f1bca14a6615644/python/mypy/issues/8001 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/299b7ad63bfcb816a5a9a1c1c368c2b5cc242bfabc8c60491f1bca14a6615644/python/mypy/issues/8001 |
| og:image:alt | Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jamesohortle |
| 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