Idea: add Char type to represent Text with size = 1
还没有人认领这个 Issue。
评估
调研方向
该 issue 未指定文件、测试或入口点;请先审阅 issue 中提出的 Char 和 Text 语义。完成的标准是形成一份达成共识的设计,涵盖可迭代行为、索引和切片、兼容性以及拼接规则。
由索引模型根据 Issue 内容生成。
描述
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.abc
In [2]: isinstance("foo", collections.abc.Sequence)
Out[2]: True
In [3]: isinstance("foo", collections.abc.Iterable)
Out[3]: True
The 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.
How about having a typing.Char to represent Text with length=1, and dedicate Text to a type with at least 2 characters?
Some examples to illustrate the usage:
def foo(xs: Iterable[Text]):
return '|'.join(xs)
# list(Text) should return List[Char]
foo("abcd") # error
foo(list("abcd")) # correct, List[Char] is a special case of List[Text]
def bar(xs: Iterable[Char]):
return '|'.join(xs)
bar("abcd") # correct, Text is a Iterable[Char]
bar(list("abcd")) # correct, List[Char] is a Iterable[Char]
bar(["ab", "cd"]) # error, List[Text] is not a Iterable[Char]
a: Char = 'a'
a[1] # error, Char cannot be indexed beyond index 0
a[0] # should be an error from type checking's PoV, Char should never be indexed
a[:] # should be an error from type checking's PoV, Char should never be copied as a slice
b: Text = 'b'
a + b # should be an error from type checking's PoV, Char and Text should be considered incompatible types
typing.cast(Text, a) + b # correct
c = a + a # correct, concatenate Char gets Text (but I'm not sure if this is reasonable though, what if we do (a + a) + a ?)
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/typing 的其他 Issue
-
topic: typing spec
难度 2/5 1-3 小时 新手友好度 72/100
-
topic: typing spec
难度 2/5 1-3 小时 新手友好度 75/100
-
topic: documentation
难度 2/5 1-3 小时 新手友好度 76/100
-
topic: documentation
难度 2/5 1-3 小时 新手友好度 65/100
-
topic: conformance tests topic: typing spec
难度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
sublimehq/package_control#1780 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·