lxml-stubs
lxml-stubs copied to clipboard
Inconsistency regarding namespace mappings
by this comment i felt strongly encouraged refactor code in order to use an empty string as key for the default namespace in a namespace mapping, but soon found inconsistencies with the current annotations and implementation.
first, mypy complains about this:
an_element = etree.fromstring("<element/>")
a_new_element = an_element.makeelement("new", nsmap=an_element.nsmap)
with error: Argument "nsmap" to "makeelement" of "_Element" has incompatible type "Dict[Optional[str], str]"; expected "Optional[Mapping[str, str]]".
then, the empty string as key isn't even the default:
In [1]: from lxml import etree
In [2]: t = etree.fromstring("<element xmlns='test'/>")
In[3]: t.nsmap.get(None)
Out[3]: 'test'
In[4]: t.nsmap.get("")
Out[[4]:
i have no proposal how to solve these issues, but i consider the first demo a bug and the latter at least confusing.