python / python/typeshed

`ElementTree.parse` type hinted to return unusable generic types

未关闭
#14,672 3 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
5.1k
派生
2.1k
平均合并
1 天 19 小时
30 天内合并 PR
82

描述

Bug Report

Since upgrading to mypy 1.15+, using ElementTree has become increasingly difficult. Likely due to https://github.com/python/typeshed/pull/13349. I'm not sure if this extra strictness is expected or if I'm just holding it wrong.

One example is the ElementTree.parse function that now returns a generic ET.ElementTree[ET.Element[str]], which is no longer compatible with basic type signatures like ET.ElementTree, which is interpreted as ElementTree[Element[str] | None].

This adds many challenges that make it difficult to work with.

  • The generic argument here is very difficult to understand what it means and is lacking documentation. From what i've been able to deduce it's possible that the root node is of a different Element type or that the root node is None. While perhaps a valid scenario, it adds a big uphill to learning and usability.
  • If one were to use these generic types, typing out ET.ElementTree[ET.Element[str]] is a lot more verbose than ET.ElementTree, and i'm questioning if the strictness here is worth it.
  • If one tries to type hint the code with these generics. Python throws errors that 'ElementTree.Element' is not subscriptable. One workaround i found for this is to surround the type hints with double-quotes.

To Reproduce

import xml.etree.ElementTree as ET

def process_document(doc: ET.ElementTree) -> None:
    pass

def process_document2(doc: ET.ElementTree[ET.Element[str]]) -> None:    # TypeError: type 'xml.etree.ElementTree.Element' is not subscriptable
    pass

process_document(ET.parse("foo.xml"))     # mypy error: Argument 1 to "process_document" has incompatible type "ElementTree[Element[str]]"; expected "ElementTree[Element[str] | None]"  [arg-type]
process_document2(ET.parse("foo.xml"))

Run with mypy --strict

Expected Behavior

ET.parse returns types assignable to ET.ElementTree

Actual Behavior

Mypy returns errror

# mypy error: Argument 1 to "process_document" has incompatible type "ElementTree[Element[str]]"; expected "ElementTree[Element[str] | None]"  [arg-type]

Workarounds

  • Use ET.ElementTree().parse() function instead of ET.parse(), it returns the root-node as a type that can be assigned to ET.Element without any generic arguments.
    This is actually a pretty decent option, if this is considered to be the primary way of using ElementTree from now on, the documentation should reflect that, right now it's suggesting ET.parse()
  • Type out the complete "ElementTree[Element[str] | None]" wrapped with double-quotes.

Your Environment

  • Mypy version used:
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读 typeshed PR 13349 以及 ElementTree.parse 的注解,然后将其与 ElementTree().parse() workaround 和 Python ElementTree 文档进行比较。完成的标准是明确预期的泛型行为和 ET.parse() 的预期可赋值性,并处理相关的 typing 或文档问题。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。