`ElementTree.parse` type hinted to return unusable generic types
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 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 thanET.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 ofET.parse(), it returns the root-node as a type that can be assigned toET.Elementwithout 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 suggestingET.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:
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず typeshed PR 13349 と ElementTree.parse のアノテーションを読み、次にそれらを ElementTree().parse() の回避策および Python ElementTree のドキュメントと比較します。意図されたジェネリックな挙動と ET.parse() に期待される代入可能性が確立され、関連する型付けまたはドキュメントの問題に対処できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100