Title: Context surprise: Value of type variable "AnyStr" of "f" cannot be "object" · Issue #3876 · python/mypy · GitHub
Open Graph Title: Context surprise: Value of type variable "AnyStr" of "f" cannot be "object" · Issue #3876 · python/mypy
X Title: Context surprise: Value of type variable "AnyStr" of "f" cannot be "object" · Issue #3876 · python/mypy
Description: In this example, good() and bad() are equivalent, yet only bad() produces an error: from typing import AnyStr, Iterable, List, Set, Text def f(a: Iterable[AnyStr]) -> List[AnyStr]: ... def good(a: List[Text]) -> Set[Text]: fa = f(a) retu...
Open Graph Description: In this example, good() and bad() are equivalent, yet only bad() produces an error: from typing import AnyStr, Iterable, List, Set, Text def f(a: Iterable[AnyStr]) -> List[AnyStr]: ... def good(a: ...
X Description: In this example, good() and bad() are equivalent, yet only bad() produces an error: from typing import AnyStr, Iterable, List, Set, Text def f(a: Iterable[AnyStr]) -> List[AnyStr]: ... def good(...
Opengraph URL: https://github.com/python/mypy/issues/3876
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Context surprise: Value of type variable \"AnyStr\" of \"f\" cannot be \"object\"","articleBody":"In this example, `good()` and `bad()` are equivalent, yet only `bad()` produces an error:\r\n```py\r\nfrom typing import AnyStr, Iterable, List, Set, Text\r\ndef f(a: Iterable[AnyStr]) -> List[AnyStr]: ...\r\ndef good(a: List[Text]) -> Set[Text]:\r\n fa = f(a)\r\n return set(a) - set(fa)\r\ndef bad(a: List[Text]) -> Set[Text]:\r\n return set(a) - set(f(a)) # E: Value of type variable \"AnyStr\" of \"f\" cannot be \"object\"\r\n```\r\nIt seems the set subtraction operator passes along some context and this gets misinterpreted. Note that because of the declared type of `a`, the generic calls to `f(a)` should always substitute `Text` for `AnyStr`, but somehow (maybe in the solver) `object` gets tried and fails.\r\n\r\nA simpler version shows this perhaps better:\r\n```py\r\nfrom typing import AnyStr, Iterable, List, Set\r\ndef f(a: Set[AnyStr]) -> Set[AnyStr]: ...\r\ndef good(a: Set[str]) -> Set[str]:\r\n fa = f(a)\r\n return set(a) - set(fa)\r\ndef bad(a: Set[str]) -> Set[str]:\r\n return a - f(a) # TWO errors here\r\n```\r\nThis gives two errors:\r\n```\r\n__tmp__.py:10: error: Value of type variable \"AnyStr\" of \"f\" cannot be \"object\"\r\n__tmp__.py:10: error: Argument 1 to \"f\" has incompatible type Set[str]; expected Set[object]\r\n```\r\nHow exactly it could end up trying `Set[object]` is a mystery to me (but then, I didn't look at the source yet).","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2017-08-25T21:15:56Z","url":"https://github.com/python/mypy/issues/3876"}
| 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:2bf73da5-139c-2488-afe2-49cfd71f25e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A18C:120A77:18134AA:206E98C:6AFFF15E |
| html-safe-nonce | ac64c9fbc3d8db601c64974100a443ead5d412afc7656dd2fdc8c341921b51f9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMThDOjEyMEE3NzoxODEzNEFBOjIwNkU5OEM6NkFGRkYxNUUiLCJ2aXNpdG9yX2lkIjoiMTg3MTQ4MzA4MTM0OTY1NjkyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0207ab2cf6a30449aa5e553764ff32a937039b24a4866da60eedf2fd0d8d82e5 |
| hovercard-subject-tag | issue:253019953 |
| 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/3876/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9ed42702a0dbc602feb70ad34c32f075d7cbac4a1de03b2fa0fa5aaac0854daa/python/mypy/issues/3876 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9ed42702a0dbc602feb70ad34c32f075d7cbac4a1de03b2fa0fa5aaac0854daa/python/mypy/issues/3876 |
| og:image:alt | In this example, good() and bad() are equivalent, yet only bad() produces an error: from typing import AnyStr, Iterable, List, Set, Text def f(a: Iterable[AnyStr]) -> List[AnyStr]: ... def good(a: ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| 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