Title: Inconsistent errors reported for protocol matching · Issue #13850 · python/mypy · GitHub
Open Graph Title: Inconsistent errors reported for protocol matching · Issue #13850 · python/mypy
X Title: Inconsistent errors reported for protocol matching · Issue #13850 · python/mypy
Description: I ran into some strange behavior in mypy when investigating a bug reported against pyright. Here is a sample that demonstrates the issue: from typing import Generic, Protocol, TypeVar, overload _T_co = TypeVar("_T_co", covariant=True) cl...
Open Graph Description: I ran into some strange behavior in mypy when investigating a bug reported against pyright. Here is a sample that demonstrates the issue: from typing import Generic, Protocol, TypeVar, overload _T_...
X Description: I ran into some strange behavior in mypy when investigating a bug reported against pyright. Here is a sample that demonstrates the issue: from typing import Generic, Protocol, TypeVar, overload _T_...
Opengraph URL: https://github.com/python/mypy/issues/13850
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent errors reported for protocol matching","articleBody":"I ran into some strange behavior in mypy when investigating a [bug reported against pyright](https://github.com/microsoft/pyright/issues/4000).\r\n\r\nHere is a sample that demonstrates the issue:\r\n\r\n```python\r\nfrom typing import Generic, Protocol, TypeVar, overload\r\n\r\n_T_co = TypeVar(\"_T_co\", covariant=True)\r\n\r\nclass _NestedSequence(Protocol[_T_co]):\r\n @overload\r\n def __getitem__(self, index: int) -> _T_co | \"_NestedSequence[_T_co]\":\r\n ...\r\n\r\n @overload\r\n def __getitem__(self, index: slice) -> \"_NestedSequence[_T_co]\":\r\n ...\r\n\r\n # def __contains__(self, __s: object, /) -> bool:\r\n # ...\r\n\r\nclass MySequence(Generic[_T_co]):\r\n @overload\r\n def __getitem__(self, index: int) -> _T_co:\r\n ...\r\n\r\n @overload\r\n def __getitem__(self, index: slice) -> \"MySequence[_T_co]\":\r\n ...\r\n\r\n def __getitem__(self, index: slice | int) -> \"MySequence[_T_co]\" | _T_co:\r\n ...\r\n\r\n def __contains__(self, __s: object, /) -> bool:\r\n ...\r\n\r\nclass MySequenceCopy(Generic[_T_co]):\r\n @overload\r\n def __getitem__(self, index: int) -> _T_co:\r\n ...\r\n\r\n @overload\r\n def __getitem__(self, index: slice) -> \"MySequenceCopy[_T_co]\":\r\n ...\r\n\r\n def __getitem__(self, index: slice | int) -> \"MySequenceCopy[_T_co]\" | _T_co:\r\n ...\r\n\r\n def __contains__(self, __s: object, /) -> bool:\r\n ...\r\n\r\ndef func1_1(x: MySequence[int]):\r\n _: _NestedSequence[int] = x # (no error)\r\n\r\ndef func1_1_copy(x: MySequence[int]):\r\n _: _NestedSequence[int] = x # error: Incompatible types in assignment\r\n\r\ndef func1_2(x: MySequence[MySequence[int]]):\r\n _: _NestedSequence[int] = x # (no error)\r\n\r\ndef func2_1(x: MySequenceCopy[int]):\r\n _: _NestedSequence[int] = x # error: Incompatible types in assignment\r\n\r\ndef func2_1_copy(x: MySequenceCopy[int]):\r\n _: _NestedSequence[int] = x # error: Incompatible types in assignment\r\n\r\ndef func2_2(x: MySequenceCopy[MySequenceCopy[int]]):\r\n _: _NestedSequence[int] = x # error: Incompatible types in assignment\r\n\r\na: _NestedSequence[int] = [[1, 2, 3]] # error: List item 0/1/2 has incompatible type \"int\"; expected \"_NestedSequence[int]\"\r\n```\r\n\r\nStrange behavior 1: Function `func1_1_copy` is an exact copy of `func1_1`, yet mypy reports an error in `func1_1_copy` but not in `func1_1`.\r\n\r\nStrange behavior 2: The protocol class `MySequenceCopy` is an exact copy of `MySequence`, so I would expect them to behave the same from a type checking perspective. `func2_2` is a copy of `func2_1` with `MySequence` replaced with `MySequenceCopy`, so I would expect the two to produce the same type checking results. Mypy emits no error for `func1_2` but emits an error for `func2_2`.\r\n\r\nStrange behavior 3: The protocol class `_NestedSequence` contains a commented-out `__contains__` method. If this method is added back, I would expect there to be no change to the type checking results. Yet, when `__contains__` is added here, all of the errors previously reported by mypy disappear. This includes the errors in the final line of the sample. I would not expect the addition of `__contains__` to have any impact on the results.\r\n\r\nI'm not sure if these three issues are related, but I suspect they are.\r\n\r\nI'm using the latest published version of mypy (0.982).","author":{"url":"https://github.com/erictraut","@type":"Person","name":"erictraut"},"datePublished":"2022-10-09T16:26:36Z","url":"https://github.com/python/mypy/issues/13850"}
| 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:f2277058-f9f9-3372-60c0-0c2e883956d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9DB2:2EB177:71B95:9E86F:6B004EAC |
| html-safe-nonce | e68cc4d3f586d9384432112f3e6b99a0e65b9e2bc181acd557b7102076b9c2e9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5REIyOjJFQjE3Nzo3MUI5NTo5RTg2Rjo2QjAwNEVBQyIsInZpc2l0b3JfaWQiOiIyNzU2MzUyNjE4OTk2NDUzMDM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 007bb616fab867d44656af8eecc6225eb5982b671609eb8909753acaaeb9e0f0 |
| hovercard-subject-tag | issue:1402325311 |
| 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/13850/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a691adefdfc1bd242f6308a272e053a0b6760b124267a2fafa7c1e7a3e47f175/python/mypy/issues/13850 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a691adefdfc1bd242f6308a272e053a0b6760b124267a2fafa7c1e7a3e47f175/python/mypy/issues/13850 |
| og:image:alt | I ran into some strange behavior in mypy when investigating a bug reported against pyright. Here is a sample that demonstrates the issue: from typing import Generic, Protocol, TypeVar, overload _T_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erictraut |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width