lxml 类型提示与 mypy

问题描述 投票:0回答:1

您好,我正在尝试使用

types-lxml
库输入提示 lxml 返回类型。

def parse_xml(path: str) -> etree._ElementTree:
    return etree.parse(path)


def components(path: str) -> List[etree._Element]:
    return parse_xml(path).xpath("/node1/node2")

但是 mypy 对第二个函数抱怨说:

error: Returning Any from function declared to return "List[_Element]"  [no-any-return]

知道如何解决这个问题吗?谢谢。

python lxml mypy
1个回答
0
投票

xpath 返回一个列表:

import lxml.etree
import mypy

# filecontent ="""<root><child>name</child>><child>name1</child></root>"""

def parse_xml(path: str) -> lxml.etree._ElementTree:
    return etree.parse(path)


def components(path: str) -> list():
    return parse_xml(path).xpath("/root/child")

r = parse_xml("./temp/example.xml")
print(type(r))

c = components("./temp/example.xml")
print(type(c))
© www.soinside.com 2019 - 2024. All rights reserved.