Title: overload with restrictions on type arguemnts of self · Issue #5320 · python/mypy · GitHub
Open Graph Title: overload with restrictions on type arguemnts of self · Issue #5320 · python/mypy
X Title: overload with restrictions on type arguemnts of self · Issue #5320 · python/mypy
Description: I'd like to be able for overload to handle restrictions on self to the type arguments of a Generic type, but I think mypy ignores restriction on self. Here's an example, where I want to have a different signature for the Lnk.__rshift__ m...
Open Graph Description: I'd like to be able for overload to handle restrictions on self to the type arguments of a Generic type, but I think mypy ignores restriction on self. Here's an example, where I want to have a diff...
X Description: I'd like to be able for overload to handle restrictions on self to the type arguments of a Generic type, but I think mypy ignores restriction on self. Here's an example, where I want to hav...
Opengraph URL: https://github.com/python/mypy/issues/5320
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"overload with restrictions on type arguemnts of self","articleBody":"I'd like to be able for `overload` to handle restrictions on self to the type arguments of a Generic type, but I think mypy ignores restriction on self. Here's an example, where I want to have a different signature for the `Lnk.__rshift__` method when the second type argument is a `Tuple`:\r\n```python\r\nIn = TypeVar('In')\r\nOut = TypeVar('Out')\r\nOther = TypeVar('Other')\r\n\r\n_1 = TypeVar('_1')\r\n_2 = TypeVar('_2')\r\n\r\n__1 = TypeVar('__1')\r\n__2 = TypeVar('__2')\r\n\r\nclass Lnk(Generic[In, Out]):\r\n # normal restriction on second argument\r\n @overload\r\n def __rshift__(self, other: 'Lnk[Out, Other]') -> 'Lnk[In,Other]':\r\n pass\r\n\r\n # restriction of type argument of self, as I want to have a special case for links to tuples\r\n @overload\r\n def __rshift__(self: 'Lnk[In, Tuple[_1, _2]]',\r\n other: 'Tuple[Lnk[_1, __1], Lnk[_2, __2]]'\r\n ) -> 'Lnk[In, Tuple[__1, __2]]':\r\n pass\r\n\r\n def __rshift__(self: Any, other: Any) -> Any:\r\n ...\r\n\r\n# create some links\r\na: Lnk[str, Tuple[str, int]] = Lnk()\r\nb: Lnk[str, int] = Lnk()\r\nc: Lnk[int, float] = Lnk()\r\n\r\n# valid calls to link things together\r\nd: Lnk[str, float] = b >> c\r\ne: Lnk[str, Tuple[int, float]] = a >> (b, c)\r\n\r\n# invalid call, but mypy does not check type of self\r\nf: Lnk[str, Tuple[float, int]] = a >> (c, b)\r\n```\r\nIf you create a top level function `link` with the exact signatures as `__rshift__`, mypy correctly complains about the invalid call, but I'd like to use the operators to create a DSL:\r\n```python\r\n@overload\r\ndef link(self: 'Lnk[In, Out]', other: 'Lnk[Out, Other]') -> 'Lnk[In,Other]':\r\n pass\r\n\r\n@overload\r\ndef link(self: 'Lnk[In, Tuple[_1, _2]]',\r\n other: 'Tuple[Lnk[_1, __1], Lnk[_2, __2]]'\r\n ) -> 'Lnk[In, Tuple[__1, __2]]':\r\n pass\r\n\r\ndef link(self: Any, other: Any) -> Any:\r\n ...\r\n\r\n# mypy correctly complains\r\nf: Lnk[str, Tuple[float, int]] = link(a, (c, b))\r\n```\r\n\r\n* Python 3.6.5\r\n* mypy 0.620+dev-f5058bcb269a55b33a02c3e3fd345541839bf06e\r\n\r\n","author":{"url":"https://github.com/wabu","@type":"Person","name":"wabu"},"datePublished":"2018-07-04T18:40:59Z","url":"https://github.com/python/mypy/issues/5320"}
| 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:c23c1894-6446-d79f-c798-3eb1ea5fc2d6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E09A:2261EB:ED1F3C:134553F:6AFFF176 |
| html-safe-nonce | c01d7dceb5360d5cce8a48debf9d30e366433e14845006607b109f7d4dfde15a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDlBOjIyNjFFQjpFRDFGM0M6MTM0NTUzRjo2QUZGRjE3NiIsInZpc2l0b3JfaWQiOiI2ODc5OTg3MTI0MDI4ODMwMDcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3e7270a79c4a1b54d23eec04b21b9ace1a3ecad2de726cd38fb2ef319eb87df7 |
| hovercard-subject-tag | issue:338355017 |
| 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/5320/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2ea43c337445655fa036e80f6117a469e7d4538aefe75494367eac4efc29c78b/python/mypy/issues/5320 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2ea43c337445655fa036e80f6117a469e7d4538aefe75494367eac4efc29c78b/python/mypy/issues/5320 |
| og:image:alt | I'd like to be able for overload to handle restrictions on self to the type arguments of a Generic type, but I think mypy ignores restriction on self. Here's an example, where I want to have a diff... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wabu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width