Title: Decide how to deal with str/unicode · Issue #1141 · python/mypy · GitHub
Open Graph Title: Decide how to deal with str/unicode · Issue #1141 · python/mypy
X Title: Decide how to deal with str/unicode · Issue #1141 · python/mypy
Description: We should agree on how we expect the string types to be used in Python 2 code. There are at least four ways we can approach this: Make str usually valid when unicode is expected. This is how mypy currently works, and this is similar to h...
Open Graph Description: We should agree on how we expect the string types to be used in Python 2 code. There are at least four ways we can approach this: Make str usually valid when unicode is expected. This is how mypy c...
X Description: We should agree on how we expect the string types to be used in Python 2 code. There are at least four ways we can approach this: Make str usually valid when unicode is expected. This is how mypy c...
Opengraph URL: https://github.com/python/mypy/issues/1141
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Decide how to deal with str/unicode","articleBody":"We should agree on how we expect the string types to be used in Python 2 code.\n\nThere are at least four ways we can approach this:\n1. Make `str` usually valid when `unicode` is expected. This is how mypy currently works, and this is similar to how PEP 484 defines `bytearray` / `bytes` compatibility. This will correspond to runtime semantics, but it's not safe as non-ascii characters in `str` objects will result in programs sometimes blowing up. A 7-bit `str` instance is almost always valid at runtime when `unicode` is expected.\n2. Get rid of the `str -> unicode` promotion and use `Union[str, unicode]` everywhere (or create an alias for it). This is almost like approach 1, except that we have a different name for `unicode` and more complex error messages and a complex programming model due to the proliferation of union types. There is potential for some additional type safety by using just `unicode` in user code.\n3. Enforce explicit `str` / `unicode` distinction in Python 2 code, similar to Python 3 (`str` would behave more or less like Python 3 `bytes`), and discourage union types. This will make it harder to annotate existing Python 2 programs which often use the two types almost interchangeably, but it will make programs safer.\n4. Have three different string types: `bytes` (distinct from from `str`) means 8-bit `str` instances -- these aren't compatible with `unicode`. `str` means ascii `str` instances. These are compatible with `bytes` and `unicode`, but not the other way around. `unicode` means `unicode` instances and isn't special. A string literal will have implicit type `str` or `bytes` depending on whether it only has ascii characters. This approach should be pretty safe and potentially also makes it fairly easy to adapt existing code, but harder than with approach 1.\n\nThese also affect how stubs should be written and thus it would be best if every tool using typeshed could use the same approach:\n- For approach 1, stubs should usually use `str`, `unicode` or `AnyStr`. This is how many stubs are written already.\n- For approach 2, stubs should use `str`, `Uniont[str, unicode]` or `AnyStr` for attributes and function arguments, and return types could additionally use plain `unicode`. Return types would in general be hard to specify precisely, as it may be difficult to predict whether a function called with `str` or combination of `str` and `unicode` returns `str`, `unicode` or `Union[str, unicode]`. In approach 1 we can safely fall back to `unicode` if unsure. `AnyStr` would be less useful as we could have mixed function arguments like `(str, unicode)` easily (see the typeshed issues mentioned below for more about this).\n- For approach 3, stubs would usually use either `str`, `unicode` or `AnyStr`, but `unicode` wouldn't accept plain `str` objects.\n- For approach 4, stubs could use three different types (`bytes`, `str`, `unicode`) in addition to `AnyStr`, and these would all behave differently. Unlike the first three approaches, `AnyStr` would range over `str`, `unicode` and `bytes` in Python 2 mode.\n\nNote that mypy currently assumes approach 1 and I don't know how well the other approaches would work in practice.\n\n[This was adapted from a comment on #1135; see the original issue for more discussion. Also, https://github.com/python/typeshed/issues/50 is relevant.]\n","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2016-01-21T16:30:51Z","url":"https://github.com/python/mypy/issues/1141"}
| 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:eb933ecf-3034-30f6-f92b-d30a0fb31d4d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B18:2FF516:50080B:6C13F0:6B002F64 |
| html-safe-nonce | 7ea8a676d368c8a3e19ce6ca8c92e74ed3fb11d5842c4b1691cfa5b6614bb12a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjE4OjJGRjUxNjo1MDA4MEI6NkMxM0YwOjZCMDAyRjY0IiwidmlzaXRvcl9pZCI6IjY5NDY1NTk4ODU5MTU5OTE5MDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9f701523ba4fb8fca43164f21756c23bece16cacab5b46dd399bf8b52e5fe5b8 |
| hovercard-subject-tag | issue:127963713 |
| 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/1141/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf008bbcfc587a1a0d6047628d57b21bf7e73180bf6fb6f15daa66ff72e29d4f/python/mypy/issues/1141 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf008bbcfc587a1a0d6047628d57b21bf7e73180bf6fb6f15daa66ff72e29d4f/python/mypy/issues/1141 |
| og:image:alt | We should agree on how we expect the string types to be used in Python 2 code. There are at least four ways we can approach this: Make str usually valid when unicode is expected. This is how mypy c... |
| 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 | 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