Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
Prior work in PEP-646 on Map was dropped to simplify it, with a promise it would appear in a future PEP. Here is a reference to the dropped Map feature: https://github.com/python/typing/issues/193#issuecomment-782689251
Here is an discussion of a potential usage of this Map feature for matching argument types for a length-n Tuple of unary Callables with a length-n Tuple parameterized by a TypeVarTuple: https://groups.google.com/g/dev-python/c/SbPOxIEvI60?pli=1
Here is a draft spec of PEP-646 that included discussion of Map: https://github.com/python/peps/blob/bf897f8c839d1b4d4534ab1fa223a210e2cacf06/pep-0646.rst
I would like to propose an extension of Map with a "bind" argument to allow disambiguation in case the parameterized type has multiple generic parameters. To give an example:
###########
## Converting from multiple domains to a common codomain
############
Domain = TypeVar('Domain')
Codomain = TypeVar('Codomain')
class Detector(Generic(Domain)):
def __init__(self, detector_fn: Callable[[Any], Optional[Domain]):
self.detector_fn = detector_fn
def detect(arg: Any) -> Optional[Domain]
# returns `arg` if `arg` belongs to Domain, otherwise returns None
return self.detector_fn(arg)
class Converter(Generic[Domain, Codomain]):
def __init__(self, converter_fn: Callable[[Domain], Codomain]):
self.converter_fn = converter_fn
def convert(arg: Domain) -> Codomain:
return self.converter_fn(arg)
Domains = TypeVarTuple('Domains')
class ConverterCollection(Generic[*Domains, Codomain]):
def __init__(self, detectors: Tuple[*Map[Detector, Domains]], converters: Tuple[*Map[Converter, Domains, bind=Domain]]):
self.detectors = detectors
self.converters = converters
def convert(self, obj_to_convert: Union[*Domains]) -> Codomain:
for detector, converter in zip(self.detectors, self.converters):
detected_object = detector.detect(obj_to_convert)
if not (detected_object is None):
return converter.convert(object_to_convert)
raise ValueError('No converter for object.')
### Usage
int_detector = Detector[int](lambda x: x if isinstance(x, int) else None)
int_converter = Converter[int, str](lambda x: json.dumps(x))
float_detector = Detector[float](lambda x: x if isinstance(x, float) else None)
float_converter = Converter[float, str](lambda x: json.dumps(x))
cc = ConverterCollection[int, float, str](
(int_detector, float_detector),
(int_converter, float_converter)
)
cc.convert(2) # works
cc.convert(2.0) # also works
cc.convert('hi') # type error
The bind=Domain argument here would be because Converter class is generic in both Domain and Codomain, so there is ambiguity about which generic parameter to Map. This is not necessary for Detector because, because it is generic only over Domain, so there is no ambiguity what to map over.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 PEP-646 中被放弃的 Map 讨论和所引用的 typing issue #193 评论开始,然后阅读关于匹配 Tuple 和 TypeVarTuple 参数的链接讨论。确定所提议的 bind argument 是否能够被无歧义地规范,以及它要求什么样的类型检查行为。完成的标准是产出一份达成共识的规范或一份范围明确的实现计划。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100