微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lxml-stubs 参数“nsmap”到“Element”的类型不兼容

如何解决lxml-stubs 参数“nsmap”到“Element”的类型不兼容

我在使用 mypy 和 lxml 存根时遇到问题。这个简化的代码重现了我在我的项目上运行 mypy 时看到的错误


from lxml import etree

def fcn() -> None:
    namespaces = {"a": "http://www.w3.org/2001/XMLSchema"}
    etree.Element(
        "{{{scl_namespaces['xs']}}}pattern",attrib={"value": "0"},nsmap=namespaces
    )

if __name__ == "__main__":
    fcn()

产生以下错误

c:\>mypy --strict -m lxml_mypy
lxml_mypy.py:7: error: Argument "nsmap" to "Element" has incompatible type "Dict[str,str]"; expected "Union[Dict[Optional[bytes],bytes],Dict[Optional[str],str],None]"
Found 1 error in 1 file (checked 1 source file)

我正在使用:

  • python 3.9.5
  • lxml 4.6.3
  • lxml 存根 0.2.0

我进行了一些搜索并找到了 TypeVar 和 TypedDict,但它们似乎都无法解决问题。有人对如何解决此输入错误有任何建议吗?

我还向 lxml-stubs repo 提交了一个问题,他们建议询问 mypy 人员。我想在我在那个 repo 中提出问题之前我会在这里问。

如果您需要更多信息,请告诉我,并提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。