René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:d677867f-baa7-5f1d-3464-5bfd7e7ee7e7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD50C:2B41BC:C4457:107E64:6B001C81
html-safe-nonce9dd9882e5aa618e64b74d129e5948d029a2d9a634269a33afe2c328c1e787623
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTBDOjJCNDFCQzpDNDQ1NzoxMDdFNjQ6NkIwMDFDODEiLCJ2aXNpdG9yX2lkIjoiOTA0MjQ1NjAwNzQxMzg2NTYwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac6b362ad879238c98465f4c6f8970b8a3ef36df39b982a80837b9af9d3dfeb291
hovercard-subject-tagissue:1658220513
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/15015/issue_layout
twitter:imagehttps://opengraph.githubassets.com/df8da2b5651799bec8c01cb2d9992d3e23c8980a935d2d8a3140f9a2a36e82a9/python/mypy/issues/15015
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/df8da2b5651799bec8c01cb2d9992d3e23c8980a935d2d8a3140f9a2a36e82a9/python/mypy/issues/15015
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameksunden
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/15015#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F15015
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F15015
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/15015
Reloadhttps://github.com/python/mypy/issues/15015
Reloadhttps://github.com/python/mypy/issues/15015
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests546 (546)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/15015
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/15015
Poor interactions with staticmethod and overload/unionhttps://github.com/python/mypy/issues/15015#top
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
topic-descriptorsProperties, class vs. instance attributeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-descriptors%22
https://github.com/ksunden
ksundenhttps://github.com/ksunden
on Apr 7, 2023https://github.com/python/mypy/issues/15015#issue-1658220513
matplotlibhttps://github.com/matplotlib/matplotlib/actions/runs/4632987811/jobs/8197705664
wrapping a third party method as a staticmethod of one of our classeshttps://github.com/matplotlib/matplotlib/blob/61ed3f40057a48821ccad758fd5f04f0df1b8aab/lib/matplotlib/_mathtext.py#L2027
#14953https://github.com/python/mypy/pull/14953
#14902https://github.com/python/mypy/pull/14902
#7781https://github.com/python/mypy/issues/7781
https://mypy-play.net/?mypy=latest&python=3.10&gist=c50c0fb3a6c10fbea317a31453b40e29https://mypy-play.net/?mypy=latest&python=3.10&gist=c50c0fb3a6c10fbea317a31453b40e29
ksundenhttps://github.com/ksunden
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
on Apr 7, 2023https://github.com/python/mypy/issues/15015#event-8950707037
https://github.com/hoodmane
hoodmanehttps://github.com/hoodmane
on Apr 7, 2023https://github.com/python/mypy/issues/15015#issuecomment-1500588396
https://github.com/ksunden
ksundenhttps://github.com/ksunden
on Apr 7, 2023https://github.com/python/mypy/issues/15015#issuecomment-1500597895
@hoodmanehttps://github.com/hoodmane
https://github.com/hoodmane
hoodmanehttps://github.com/hoodmane
on Apr 7, 2023https://github.com/python/mypy/issues/15015#issuecomment-1500608115
https://github.com/hoodmane
hoodmanehttps://github.com/hoodmane
on Apr 7, 2023https://github.com/python/mypy/issues/15015#issuecomment-1500609340
AlexWaygoodhttps://github.com/AlexWaygood
topic-descriptorsProperties, class vs. instance attributeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-descriptors%22
on Apr 11, 2023https://github.com/python/mypy/issues/15015#event-8976162490
https://github.com/ksunden
ksundenhttps://github.com/ksunden
on Apr 21, 2023https://github.com/python/mypy/issues/15015#issuecomment-1517320787
https://github.com/b-kamphorst
b-kamphorsthttps://github.com/b-kamphorst
on Apr 26, 2023https://github.com/python/mypy/issues/15015#issuecomment-1523432434
https://github.com/b-kamphorst
b-kamphorsthttps://github.com/b-kamphorst
on Jul 31, 2023https://github.com/python/mypy/issues/15015#issuecomment-1657827685
#15737https://github.com/python/mypy/issues/15737
annotatedhttps://github.com/python/mypy/blob/v1.4.1/mypy/typeshed/stdlib/builtins.pyi#L118
https://github.com/ksunden
ksundenhttps://github.com/ksunden
on Apr 1, 2024https://github.com/python/mypy/issues/15015#issuecomment-2030704852
https://github.com/Jamim
Jamimhttps://github.com/Jamim
on May 9, 2024https://github.com/python/mypy/issues/15015#issuecomment-2101901694
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/15015
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/15015
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
topic-descriptorsProperties, class vs. instance attributeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-descriptors%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.