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

Remove redundant type ignores and fix typing errors #1759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdflib/plugins/shared/jsonld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,4 @@ def _get_source_id(self, source, key):
"Term",
"id, name, type, container, index, language, reverse, context," "prefix, protected",
)
Term.__new__.__defaults__ = (UNDEF, UNDEF, UNDEF, UNDEF, False, UNDEF, False, False) # type: ignore[attr-defined]
Term.__new__.__defaults__ = (UNDEF, UNDEF, UNDEF, UNDEF, False, UNDEF, False, False)
12 changes: 5 additions & 7 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,14 +1906,12 @@ def recurse():
# to worry about that, which is a bonus...
n_keys = [
k
# type error: Item "Element" of "Union[Document, Element]" has no attribute "attributes"
for k in node.attributes.keysNS() # type: ignore[union-attr]
for k in node.attributes.keysNS()
if k[0] != "http://www.w3.org/2000/xmlns/"
]
o_keys = [
k
# type error: Item "Element" of "Union[Document, Element]" has no attribute "attributes"
for k in other.attributes.keysNS() # type: ignore[union-attr]
for k in other.attributes.keysNS()
if k[0] != "http://www.w3.org/2000/xmlns/"
]
if len(n_keys) != len(o_keys):
Expand Down Expand Up @@ -1968,9 +1966,9 @@ def recurse():

elif node.nodeType == XMLNode.DOCUMENT_TYPE_NODE:
if TYPE_CHECKING:
assert isinstance(node, xml.dom.minidom.Document)
assert isinstance(other, xml.dom.minidom.Document)
return node.publicId == other.publicId and node.systemId == other.system.Id
assert isinstance(node, xml.dom.minidom.DocumentType)
assert isinstance(other, xml.dom.minidom.DocumentType)
return node.publicId == other.publicId and node.systemId == other.systemId

else:
# should not happen, in fact
Expand Down
64 changes: 63 additions & 1 deletion test/test_xmlliterals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import logging
import textwrap
from typing import Callable, Optional, Sequence

import pytest
import rdflib
from rdflib import RDF, Graph, Literal
from rdflib import RDF, Literal

import xml.dom.minidom


def testPythonRoundtrip():
Expand Down Expand Up @@ -88,3 +95,58 @@ def testHTML():

assert l1 != l2
assert not l1.eq(l2)


@pytest.mark.parametrize(
("values", "equals"),
[
pytest.param(
[
lambda: Literal('<something />', datatype=RDF.XMLLiteral),
lambda: Literal('<something/>', datatype=RDF.XMLLiteral),
],
True,
),
pytest.param(
[
lambda: Literal(
xml.dom.minidom.parseString(
textwrap.dedent(
"""\
<!DOCTYPE example>
<something/>
"""
)
),
datatype=RDF.XMLLiteral,
),
lambda: Literal(
xml.dom.minidom.parseString(
textwrap.dedent(
"""\
<!DOCTYPE example>
<something />
"""
)
),
datatype=RDF.XMLLiteral,
),
],
True,
),
],
)
def test_eq(values: Sequence[Callable[[], Literal]], equals: bool) -> None:
first_value = values[0]()
logging.debug("first_value = \n%r", first_value)
for value in values[1:]:
current_value = value()
logging.debug("current_value = \n%r", current_value)
if equals:
assert first_value.eq(
current_value
), f"{current_value} must be equal to {first_value}"
else:
assert not first_value.eq(
current_value
), f"{current_value} must be not be equal to {first_value}"