Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing · GitHub
Open Graph Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing
X Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing
Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed accurately models this using @typing.overl...
Open Graph Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed ac...
X Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter....
Opengraph URL: https://github.com/python/typing/issues/1096
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Introduce `typing.STRICTER_STUBS`","articleBody":"Some functions can return different types depending on passed arguments. For example:\r\n* `open(name, 'rb')` returns `io.BufferedReader`, whereas\r\n* `open(name, 'wb')` returns `io.BufferedWriter`.\r\n\r\nThe typeshed accurately models this using `@typing.overload` and `typing.Literal`. However there is the case that the argument value deciding the return type cannot be determined statically, for example:\r\n\r\n```python\r\ndef my_open(name: str, write: bool):\r\n with open(name, 'wb' if write else 'rb') as f:\r\n content = f.read()\r\n```\r\n\r\nIn this case typeshed currently just claims that `open` returns `typing.IO[Any]`, so `content` ends up having the type `Any`, resulting in a loss of type safety (e.g. `content.startswith('hello')` will lead to a runtime error if the file was opened in binary mode, but type checkers won't be able to warn you about this because of `Any`).\r\n\r\nWhile typeshed could theoretically just change the return type to `typing.IO[Union[str, bytes]]`, that would force all existing code bases that currently rely on `Any` to type check to update their code, which is of course unacceptable.\r\n\r\nWhen starting a new project I however want the strictest type stubs possible. I explicitly do not want standard library functions to return unsafe values like `Any` (or the a bit less unsafe `AnyOf` suggested in #566), when the return types can be modeled by a `Union`.\r\n\r\nI therefore propose the introduction of a new variable `typing.STRICTER_STUBS: bool`, that's only available during type checking.\r\n\r\nWhich would allow typeshed to do the following:\r\n\r\n```python\r\nif typing.STRICTER_STUBS:\r\n AnyOrUnion = typing.Union\r\nelse:\r\n AnyOrUnion = typing.Any\r\n```\r\n\r\nAmbiguous return types could then be annotated as e.g. `-> typing.IO[AnyOrUnion[str, bytes]]`.\r\n\r\nThis would allow users to opt into stricter type stubs, if they so desire, without forcing changes on existing code bases.\r\n\r\nCC: @AlexWaygood, @JelleZijlstra, @srittau, @hauntsaninja, @rchen152, @erictraut\r\n\r\nP.S. Since I have seen `Union` return types being dismissed because \"the caller needs to use `isinstance()`\", I want to note that this is not true, if the caller wants to trade type safety for performance, they can always just add an explicit `Any` annotation to circumvent the runtime overhead of `isinstance`. Union return types force you to either handle potential type errors or explicitly opt out of type safety, which I find strongly preferable to lack of type safety by default.","author":{"url":"https://github.com/not-my-profile","@type":"Person","name":"not-my-profile"},"datePublished":"2022-03-01T16:28:06Z","url":"https://github.com/python/typing/issues/1096"}
| 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:ce259f84-da8f-dfec-6d21-9e9582e01d0c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8CAE:197995:21AE64B:2DB2871:6B00239D |
| html-safe-nonce | 4186e8c9cc3d9b8bdafdaef09445230bf512a127296ddba8307714ac881c705f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Q0FFOjE5Nzk5NToyMUFFNjRCOjJEQjI4NzE6NkIwMDIzOUQiLCJ2aXNpdG9yX2lkIjoiNzEwNzI5Mjg2NTEwMDc4NDU0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d81b3817b38c88a77418dbbf4603384c3c88dcc3e7d8d2a8855b91cd2e7f2065 |
| hovercard-subject-tag | issue:1155547089 |
| 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/typing/1096/issue_layout |
| twitter:image | https://opengraph.githubassets.com/878420e58e1d774c2820c373316358578eabae0d97401028a05024b1fc2e1634/python/typing/issues/1096 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/878420e58e1d774c2820c373316358578eabae0d97401028a05024b1fc2e1634/python/typing/issues/1096 |
| og:image:alt | Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed ac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | not-my-profile |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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