Title: Failing to recognize generic constraints in tuples · Issue #7702 · python/mypy · GitHub
Open Graph Title: Failing to recognize generic constraints in tuples · Issue #7702 · python/mypy
X Title: Failing to recognize generic constraints in tuples · Issue #7702 · python/mypy
Description: Are you reporting a bug, or opening a feature request? Bug The basic context here is I need to map data into different shapes using various Mappings, and then write those shapes to different targets using various Writers. I want to ensur...
Open Graph Description: Are you reporting a bug, or opening a feature request? Bug The basic context here is I need to map data into different shapes using various Mappings, and then write those shapes to different target...
X Description: Are you reporting a bug, or opening a feature request? Bug The basic context here is I need to map data into different shapes using various Mappings, and then write those shapes to different target...
Opengraph URL: https://github.com/python/mypy/issues/7702
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Failing to recognize generic constraints in tuples","articleBody":"* Are you reporting a bug, or opening a feature request? Bug\r\n\r\nThe basic context here is I need to map data into different shapes using various `Mapping`s, and then write those shapes to different targets using various `Writer`s. I want to ensure that when I define a job configuration, I get a `Mapping` and `Writer` that are compatible with one another (i.e. they map to and write the same shape).\r\n\r\nInitially I tried returning a `Tuple[Mapping[E], Writer[E]]`, but found that mypy *does not* report an error when I deliberately mismatch a `Mapping` and `Writer`. Fortunately, however, an error *is* raised when I define a (data)class to hold these two objects instead of just a tuple. I think using a dataclass here is probably a good idea regardless (for reader comprehension purposes), but I would nevertheless expect the tuple implementation to behave the same way. It seems like someone could try the tuple implementation, not see any errors, and incorrectly assume they have a typechecking safety net that isn't actually there.\r\n\r\n```python\r\nfrom dataclasses import dataclass\r\nfrom typing import Generic, Tuple, TypeVar\r\nfrom typing_extensions import Protocol\r\n\r\n\r\nM = TypeVar(\"M\", covariant=True)\r\nclass Mapping(Protocol[M]):\r\n def map(self) -> M:\r\n ...\r\n\r\n\r\nW = TypeVar(\"W\", contravariant=True)\r\nclass Writer(Protocol[W]):\r\n def write(self, shape: W):\r\n ...\r\n\r\n\r\nclass Circle:\r\n pass\r\n\r\n\r\nclass CircleMapping:\r\n def map(self) -> Circle:\r\n return Circle()\r\n\r\n\r\nclass CircleWriter:\r\n def write(self, shape: Circle):\r\n print(\"in CircleWriter.write\")\r\n\r\n\r\nclass Square:\r\n pass\r\n\r\n\r\nclass SquareMapping:\r\n def map(self) -> Square:\r\n return Square()\r\n\r\n\r\nclass SquareWriter:\r\n def write(self, shape: Square):\r\n print(\"in SquareWriter.write\")\r\n\r\n\r\nE = TypeVar(\"E\")\r\n\r\n\r\n## Tuple strategy\r\nTools = Tuple[Mapping[E], Writer[E]]\r\n\r\ndef get_tools(shape_name: str) -> Tools:\r\n if shape_name == \"square\":\r\n return (SquareMapping(), CircleWriter()) # mypy does not flag an error here!\r\n else:\r\n return (CircleMapping(), CircleWriter())\r\n\r\ndef run_1(tools: Tools):\r\n (mapping, writer) = tools\r\n shape = mapping.map()\r\n writer.write(shape)\r\n\r\n\r\n\r\n## Dataclass strategy\r\n@dataclass\r\nclass EtlTools(Generic[E]):\r\n mapping: Mapping[E]\r\n writer: Writer[E]\r\n\r\ndef get_etl_tools(shape_name: str) -> EtlTools:\r\n if shape_name == \"square\":\r\n return EtlTools(mapping=SquareMapping(), writer=CircleWriter()) # mypy error, cannot infer type argument 1 of EtlTools\r\n else:\r\n return EtlTools(mapping=CircleMapping(), writer=CircleWriter())\r\n\r\ndef run_2(etl_tools: EtlTools):\r\n shape = etl_tools.mapping.map()\r\n etl_tools.writer.write(shape)\r\n```\r\n\r\n* What is the actual behavior/output?\r\nMypy only errors on the dataclass implementation, not the tuple implementation\r\n\r\n* What is the behavior/output you expect?\r\nThe tuple implementation should report an error as well\r\n\r\n* What are the versions of mypy and Python you are using?\r\nmypy = \"==0.720\"\r\npython version 3.7.4\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\nNone","author":{"url":"https://github.com/mikeknep","@type":"Person","name":"mikeknep"},"datePublished":"2019-10-11T21:13:37Z","url":"https://github.com/python/mypy/issues/7702"}
| 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:5af2647d-edc3-773d-c446-57920f046149 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91E0:22E601:434CB8:5A9923:6B002C37 |
| html-safe-nonce | 66260e248edeedc7891eb4cb9555e4493610ee91434084db9adbd719d6bc4faf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUUwOjIyRTYwMTo0MzRDQjg6NUE5OTIzOjZCMDAyQzM3IiwidmlzaXRvcl9pZCI6IjgyNDQ0NzgzNTI1NjQ0MzE5MjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ffe47c4933c52074cba881ca0aa75d1c0bb0b203eb69e395c790a8f2ea3cb5a7 |
| hovercard-subject-tag | issue:506060563 |
| 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/7702/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c100b7dcb007dd4a0f163b64aeadb8f9098a88c6142dd87435feba65fda95d3b/python/mypy/issues/7702 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c100b7dcb007dd4a0f163b64aeadb8f9098a88c6142dd87435feba65fda95d3b/python/mypy/issues/7702 |
| og:image:alt | Are you reporting a bug, or opening a feature request? Bug The basic context here is I need to map data into different shapes using various Mappings, and then write those shapes to different target... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mikeknep |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width