Title: Idea: add Char type to represent Text with size = 1 · Issue #510 · python/typing · GitHub
Open Graph Title: Idea: add Char type to represent Text with size = 1 · Issue #510 · python/typing
X Title: Idea: add Char type to represent Text with size = 1 · Issue #510 · python/typing
Description: Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly doesn't work: In [1]: import collections.a...
Open Graph Description: Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly do...
X Description: Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly do...
Opengraph URL: https://github.com/python/typing/issues/510
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Idea: add Char type to represent Text with size = 1","articleBody":"Due to the lack of character type in Python, a function that accept `List[Text]` will need to be extra careful when generalize the accepted argument type. `Iterable[Text]` and `Sequence[Text]` clearly doesn't work:\r\n\r\n```python\r\nIn [1]: import collections.abc\r\n\r\nIn [2]: isinstance(\"foo\", collections.abc.Sequence)\r\nOut[2]: True\r\n\r\nIn [3]: isinstance(\"foo\", collections.abc.Iterable)\r\nOut[3]: True\r\n```\r\n\r\nThe closest thing you can do is to do something like `Union[List[Text], Set[Text], Tuple[Text]]` but it will not work with things like `KeysView`.\r\n\r\nHow about having a `typing.Char` to represent Text with length=1, and dedicate `Text` to a type with at least 2 characters?\r\n\r\nSome examples to illustrate the usage:\r\n\r\n```python\r\ndef foo(xs: Iterable[Text]):\r\n return '|'.join(xs)\r\n\r\n# list(Text) should return List[Char]\r\nfoo(\"abcd\") # error\r\nfoo(list(\"abcd\")) # correct, List[Char] is a special case of List[Text]\r\n\r\ndef bar(xs: Iterable[Char]):\r\n return '|'.join(xs)\r\n\r\nbar(\"abcd\") # correct, Text is a Iterable[Char]\r\nbar(list(\"abcd\")) # correct, List[Char] is a Iterable[Char]\r\nbar([\"ab\", \"cd\"]) # error, List[Text] is not a Iterable[Char]\r\n\r\na: Char = 'a'\r\n\r\na[1] # error, Char cannot be indexed beyond index 0\r\na[0] # should be an error from type checking's PoV, Char should never be indexed\r\na[:] # should be an error from type checking's PoV, Char should never be copied as a slice\r\n\r\nb: Text = 'b'\r\na + b # should be an error from type checking's PoV, Char and Text should be considered incompatible types\r\ntyping.cast(Text, a) + b # correct\r\n\r\nc = a + a # correct, concatenate Char gets Text (but I'm not sure if this is reasonable though, what if we do (a + a) + a ?)\r\n```","author":{"url":"https://github.com/yegle","@type":"Person","name":"yegle"},"datePublished":"2017-12-06T22:48:33Z","url":"https://github.com/python/typing/issues/510"}
| 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:2dce20f1-2f8c-dfee-badd-30286f1cd93b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9E8:182C25:B03BBE:EC16C6:6B004B2D |
| html-safe-nonce | a01913bd7dd6183eaa9bd7a51be3ef1f8a7bcc1812621a3b0340e255f50aea1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUU4OjE4MkMyNTpCMDNCQkU6RUMxNkM2OjZCMDA0QjJEIiwidmlzaXRvcl9pZCI6IjY1Mzc4NTY1MjA1NTQzNzI1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4dc2facd811963e2994f3b653164fd84be8a5c50194f4d9f0361ec29d7626c65 |
| hovercard-subject-tag | issue:279935282 |
| 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/510/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1bc7a86607d7ae217a92a1f5ec7f97dcb9296b78911ea232a35e588169c97985/python/typing/issues/510 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1bc7a86607d7ae217a92a1f5ec7f97dcb9296b78911ea232a35e588169c97985/python/typing/issues/510 |
| og:image:alt | Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly do... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yegle |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width