Title: Simple dependent types · Issue #3062 · python/mypy · GitHub
Open Graph Title: Simple dependent types · Issue #3062 · python/mypy
X Title: Simple dependent types · Issue #3062 · python/mypy
Description: [This is based on a discussion between me and @gvanrossum last week. I'm writing this down so that we won't forget about this.] Several stdlib functions have signatures where the return type depends on the value of an argument. Examples:...
Open Graph Description: [This is based on a discussion between me and @gvanrossum last week. I'm writing this down so that we won't forget about this.] Several stdlib functions have signatures where the return type depend...
X Description: [This is based on a discussion between me and @gvanrossum last week. I'm writing this down so that we won't forget about this.] Several stdlib functions have signatures where the return typ...
Opengraph URL: https://github.com/python/mypy/issues/3062
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Simple dependent types","articleBody":"[This is based on a discussion between me and @gvanrossum last week. I'm writing this down so that we won't forget about this.]\r\n\r\nSeveral stdlib functions have signatures where the return type depends on the value of an argument. Examples:\r\n\r\n* `open` returns an `IO[str]` or `IO[bytes]` depending on whether the mode argument has `'b'` as a substring.\r\n* `subprocess.check_output` returns a `str` or `bytes` depending on the value of the boolean `universal_newlines` argument.\r\n\r\nA simple way to support these would be to introduce a few additional types that work with `str` and `bool` literals:\r\n\r\n* A string pattern type that can describe the contents of a string literal, perhaps using a regular expression.\r\n* Types for `False` and `True`.\r\n\r\nFor example, if we had `FalseType` and `TrueType`, we could write a function whose return value depends on the value of a boolean argument:\r\n\r\n```py\r\n@overload\r\ndef f(x: FalseType) -> bytes: ...\r\n@overload\r\ndef f(x: TrueType) -> str: ...\r\n\r\ndef f(x: Any) -> Any:\r\n if x:\r\n return 'x'\r\n else:\r\n return b'x'\r\n\r\nreveal_type(f(False)) # bytes\r\nreveal_type(f(True) # str\r\n```\r\n\r\nNot sure about this one:\r\n\r\n```py\r\n...\r\nx = bool('')\r\nreveal_type(f(x)) # Union[str, bytes] since we don't know the boolean value statically?\r\n```\r\n\r\nWe could also have a generic class where a generic type argument depends on the value of an argument to `__init__`. Not sure how we should represent these -- here is one potential approach:\r\n\r\n```py\r\nclass C(Generic[AnyStr]):\r\n @overload\r\n @returntype('C[str]')\r\n def __init__(self, x: TrueType) -> None: ...\r\n @overload\r\n @returntype('C[bytes]')\r\n def __init__(self, x: FalseType) -> None: ...\r\n ...\r\n\r\nreveal_type(C(True)) # C[str]\r\n```\r\n\r\nAlternatively, we could use `__new__`, but this would be problematic if there is no corresponding method defined at runtime.","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2017-03-27T16:15:38Z","url":"https://github.com/python/mypy/issues/3062"}
| 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:c81687fe-c9b4-c590-8fb5-d0bd09236971 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF2A:3CBF1A:1E7E670:29B1D27:6B001DE5 |
| html-safe-nonce | 0f83ee63040707b38941fc3d915486096d5c6cfb731cc0ae1163dcd7bb89cd5f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjJBOjNDQkYxQToxRTdFNjcwOjI5QjFEMjc6NkIwMDFERTUiLCJ2aXNpdG9yX2lkIjoiODU1MDk5MTY2MTQ2NTg2OTc5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e34efa2d6bc642331194b6d5526d1368288fe2ccee2f8d8d2de8899ea8f93464 |
| hovercard-subject-tag | issue:217298527 |
| 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/3062/issue_layout |
| twitter:image | https://opengraph.githubassets.com/458592416ea87722b2a4723ccad8156cb6932c20204959dbaa0da3eeaf5e1e13/python/mypy/issues/3062 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/458592416ea87722b2a4723ccad8156cb6932c20204959dbaa0da3eeaf5e1e13/python/mypy/issues/3062 |
| og:image:alt | [This is based on a discussion between me and @gvanrossum last week. I'm writing this down so that we won't forget about this.] Several stdlib functions have signatures where the return type depend... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width