Title: Poor interactions with staticmethod and overload/union · Issue #15015 · python/mypy · GitHub
Open Graph Title: Poor interactions with staticmethod and overload/union · Issue #15015 · python/mypy
X Title: Poor interactions with staticmethod and overload/union · Issue #15015 · python/mypy
Description: Bug Report Behavior changed in v1.2.0 regarding how staticmethods are handled when invoked by doing foo = staticmethod(bar) (i.e. not as the @decorator syntax) A realworld example of this appeared in matplotlib, which is wrapping a third...
Open Graph Description: Bug Report Behavior changed in v1.2.0 regarding how staticmethods are handled when invoked by doing foo = staticmethod(bar) (i.e. not as the @decorator syntax) A realworld example of this appeared ...
X Description: Bug Report Behavior changed in v1.2.0 regarding how staticmethods are handled when invoked by doing foo = staticmethod(bar) (i.e. not as the @decorator syntax) A realworld example of this appeared ...
Opengraph URL: https://github.com/python/mypy/issues/15015
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Poor interactions with staticmethod and overload/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-->\r\n\r\n**Bug Report**\r\n\r\nBehavior changed in v1.2.0 regarding how staticmethods are handled when invoked by doing `foo = staticmethod(bar)` (i.e. not as the `@decorator` syntax)\r\n\r\nA realworld example of this appeared in [matplotlib](https://github.com/matplotlib/matplotlib/actions/runs/4632987811/jobs/8197705664), which is [wrapping a third party method as a staticmethod of one of our classes](https://github.com/matplotlib/matplotlib/blob/61ed3f40057a48821ccad758fd5f04f0df1b8aab/lib/matplotlib/_mathtext.py#L2027) (pyparsing)\r\n\r\nPossibly related:\r\n\r\n#14953 (most recent change to staticmethod handling)\r\n#14902 (typeshed sync, mentions staticmethod\r\n#7781 (original report of staticmethod behavior\r\n\r\nA pure python example (and extension) is provided below.\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-->\r\n\r\n\r\n**To Reproduce**\r\n\r\n```python\r\n\r\nfrom typing import overload, Callable, Any\r\n@overload\r\ndef foo(a: int) -> None:\r\n ...\r\n \r\n@overload\r\ndef foo(a: str) -> None: ...\r\n\r\ndef foo(a) -> None:\r\n ...\r\n\r\ndef func_factory(spam) -> Callable[[], Any]| Callable[[int], Any]:\r\n return lambda: None\r\n \r\ndef qux(i: int) -> int:\r\n return i\r\n\r\nclass Foo():\r\n foo = staticmethod(foo)\r\n bar = staticmethod(func_factory(None))\r\n \r\n @overload\r\n @staticmethod\r\n def baz(a: str) -> str: ...\r\n @overload\r\n @staticmethod\r\n def baz(a: int) -> int: ...\r\n \r\n @staticmethod\r\n def baz(a):\r\n return a\r\n \r\n qux = staticmethod(qux)\r\n\r\nreveal_type(foo) # Correctly 'Overload(def (a: builtins.int), def (a: builtins.str))'\r\nreveal_type(Foo.foo) # Should match above, actually 'def (*Any, **Any) -> Any' on v1.1.1 and \"def (a: builtins.int)\" on v1.2.0\r\n\r\nreveal_type(func_factory(None)) # Correctly 'Union[def () -> Any, def (builtins.int) -> Any]'\r\nreveal_type(Foo.bar) # Should match above, actually 'def (*Any, **Any) -> Any' on v1.1.1 and \"def (*\u003cnothing>, **\u003cnothing>) -> Any\" on v1.2.0\r\n\r\nreveal_type(Foo.baz) # Correctly \"Overload(def (a: builtins.str) -> builtins.str, def (a: builtins.int) -> builtins.int)\"\r\n\r\nreveal_type(qux) # Correctly \"def (i: builtins.int) -> builtins.int\"\r\nreveal_type(Foo.qux) # Should match above, actually 'def (*Any, **Any) -> builtins.int' on v1.1.1 and correct on v1.2.0\r\n\r\nFoo.foo(\"a\") # Errors on v1.2.0\r\n```\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.10&gist=c50c0fb3a6c10fbea317a31453b40e29\r\n\r\n**Expected Behavior**\r\n\r\nI would expect that the overload (or union) would carry forward to the decorated static method.\r\n\r\n**Actual Behavior**\r\n\r\nPrior to v1.2.0 (tested explicitly with v1.1.1:\r\nAll staticmethods constructed without `@decorator` syntax would simply get `def (*Any, **Any) -> Any`, which was at least not an error, though was throwing away type information.\r\n\r\nIn the case of a single non-overloaded function defined outside of the class, it gets the return type but not the parameter type.\r\n\r\nIn v1.2.0:\r\n\r\nIn the case of an overloaded method defined outside of the class, only the first overloaded definition is kept, resulting in incorrect errors for alternate signatures.\r\n\r\nIn the case of a Union type (such as from a factory function), an error is thrown on the call to `staticmethod`, even if all branches of the union satisfy the type `staticmethod` expects.\r\nThis was the actual case I saw with matplotlib.\r\n\r\nIn the case of a single non-overloaded function or an overload defined in the class (with `@staticmethod` on all overload branches) v1.2.0 behaves as expected.\r\n\r\n```\r\nmain.py:21: error: Argument 1 to \"staticmethod\" has incompatible type \"Union[Callable[[], Any], Callable[[int], Any]]\"; expected \"Callable[[VarArg(\u003cnothing>), KwArg(\u003cnothing>)], Any]\" [arg-type]\r\nmain.py:36: note: Revealed type is \"Overload(def (a: builtins.int), def (a: builtins.str))\"\r\nmain.py:37: note: Revealed type is \"def (a: builtins.int)\"\r\nmain.py:39: note: Revealed type is \"Union[def () -> Any, def (builtins.int) -> Any]\"\r\nmain.py:40: note: Revealed type is \"def (*\u003cnothing>, **\u003cnothing>) -> Any\"\r\nmain.py:42: note: Revealed type is \"Overload(def (a: builtins.str) -> builtins.str, def (a: builtins.int) -> builtins.int)\"\r\nmain.py:44: note: Revealed type is \"def (i: builtins.int) -> builtins.int\"\r\nmain.py:45: note: Revealed type is \"def (i: builtins.int) -> builtins.int\"\r\nmain.py:47: error: Argument 1 has incompatible type \"str\"; expected \"int\" [arg-type]\r\nFound 2 errors in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: v1.2.0 (compared to v1.1.1)\r\n- Mypy command-line flags: N/A\r\n- Mypy configuration options from `mypy.ini` (and other config files):N/A\r\n- Python version used: 3.11\r\n\r\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\r\n","author":{"url":"https://github.com/ksunden","@type":"Person","name":"ksunden"},"datePublished":"2023-04-07T01:05:15Z","url":"https://github.com/python/mypy/issues/15015"}
| 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:d677867f-baa7-5f1d-3464-5bfd7e7ee7e7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D50C:2B41BC:C4457:107E64:6B001C81 |
| html-safe-nonce | 9dd9882e5aa618e64b74d129e5948d029a2d9a634269a33afe2c328c1e787623 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTBDOjJCNDFCQzpDNDQ1NzoxMDdFNjQ6NkIwMDFDODEiLCJ2aXNpdG9yX2lkIjoiOTA0MjQ1NjAwNzQxMzg2NTYwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6b362ad879238c98465f4c6f8970b8a3ef36df39b982a80837b9af9d3dfeb291 |
| hovercard-subject-tag | issue:1658220513 |
| 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/15015/issue_layout |
| twitter:image | https://opengraph.githubassets.com/df8da2b5651799bec8c01cb2d9992d3e23c8980a935d2d8a3140f9a2a36e82a9/python/mypy/issues/15015 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/df8da2b5651799bec8c01cb2d9992d3e23c8980a935d2d8a3140f9a2a36e82a9/python/mypy/issues/15015 |
| og:image:alt | Bug Report Behavior changed in v1.2.0 regarding how staticmethods are handled when invoked by doing foo = staticmethod(bar) (i.e. not as the @decorator syntax) A realworld example of this appeared ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ksunden |
| 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