Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument "nsmap" to "Element" has incompatible type #31

Closed
keith-gray-powereng opened this issue Jul 3, 2021 · 5 comments
Closed

Argument "nsmap" to "Element" has incompatible type #31

keith-gray-powereng opened this issue Jul 3, 2021 · 5 comments

Comments

@keith-gray-powereng
Copy link

This simplified code reproduces an error I am seeing when running mypy over my project.

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()

The following error is produced.

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)

I am using:

  • python 3.9.5
  • lxml 4.6.3
  • lxml-stubs 0.2.0

I did some searching and found TypeVar and TypedDict but neither of those seemed to be able to address the issue. Does anyone have suggestions on how to resolve this typing error?

Please let me know if you need more info and thanks in advance.

@scoder
Copy link
Member

scoder commented Jul 3, 2021

Hmm, the error looks wrong to me. I would expect
Union[Dict[Optional[bytes], bytes], Dict[Optional[str], str], None]
to include
Dict[str, str].
Seems more something to ask on the mypy side, maybe?

@JelleZijlstra
Copy link
Collaborator

This is a variance issue; Dict is invariant in its key type. See https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance.

@scoder
Copy link
Member

scoder commented Feb 4, 2022

Mostly a duplicate of the more general #29

@abelcheung
Copy link
Contributor

Just noticed this part:

https://github.com/lxml/lxml/blob/182e0c92f7fd32701f85cad532f29c2e559757b5/src/lxml/_elementpath.py#L260-L267

In this code path which seems to be used by ET.iterfind(), _Element.iterfind() and their callers, lxml can support None as default namespace key, like {None: 'http://my.ns/'}. XPath and friends pose no problem as they don't support default namespace.

If it is decided lxml shouldn't support it in annotation, this issue can be treated as the same as #29, otherwise namespace mappings would need extra consideration of its own.

@scoder
Copy link
Member

scoder commented Feb 19, 2022

The type hints are incomplete anyway. Anything that helps users will probably be fine.

Let's mark this as duplicate of #29 and wait for someone to actually complain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants