Skip to content

Commit

Permalink
Adapts deprecations of delb 0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyfuture committed Jan 14, 2025
1 parent cc0ccf4 commit 27fb08a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 50 deletions.
13 changes: 4 additions & 9 deletions _delb/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ def __resolve_accessor(self, item: AttributeAccessor) -> QualifiedName:
raise TypeError(ATTRIBUTE_ACCESSOR_MSG)

if namespace is None:
namespace = self.__node._namespace
namespace = self._node._namespace

return namespace, name

Expand Down Expand Up @@ -2146,7 +2146,7 @@ def fetch_or_create_by_xpath(

return self._create_by_xpath(
ast=ast,
namespaces=Namespaces(namespaces or Namespaces({"": self.namespace or ""})),
namespaces=Namespaces(namespaces or Namespaces({"": self._namespace})),
)

def _create_by_xpath(
Expand Down Expand Up @@ -3212,8 +3212,8 @@ def __init__(
self.writer = writer

def _collect_prefixes(self, root: TagNode):
if root.namespace not in self._namespaces.values():
self._prefixes[root.namespace] = ""
if root._namespace not in self._namespaces.values():
self._prefixes[root._namespace] = ""

for node in traverse_bf_ltr_ttb(root, is_tag_node):
assert isinstance(node, TagNode)
Expand Down Expand Up @@ -3251,11 +3251,6 @@ def _collect_prefixes(self, root: TagNode):
assert "" not in self._prefixes.values()
self._prefixes[namespace] = ""

if "" in self._prefixes:
# for quick resolution the empty namespace is represented as `None` like
# `TagNode.namespace` and `Attribute.namespace`
self._prefixes[None] = self._prefixes.pop("")

def __redeclare_empty_prefix(self):
# a possibly collected declaration of an empty namespace needs to be mapped to
# a prefix because an empty namespace needs to be serialized and such cannot be
Expand Down
2 changes: 1 addition & 1 deletion _delb/xpath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def evaluate(
# global namespaces are guaranteed by the Namespaces implementation
if namespaces is None:
if _is_node_of_type(node, "TagNode"):
_namespaces = Namespaces({"": cast("TagNode", node).namespace or ""})
_namespaces = Namespaces({"": cast("TagNode", node)._namespace})
else:
_namespaces = Namespaces({})
elif isinstance(namespaces, Namespaces):
Expand Down
8 changes: 4 additions & 4 deletions _delb/xpath/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def evaluate(self, node: NodeBase, namespaces: Namespaces) -> bool:
node = cast("TagNode", node)

if self.prefix:
return (node.namespace or "") == namespaces[self.prefix]
return node._namespace == namespaces[self.prefix]
else:
return True

Expand Down Expand Up @@ -440,14 +440,14 @@ def evaluate(self, node: NodeBase, namespaces: Namespaces) -> bool:

if namespace is None: # noqa: SIM114
# XPath spec behaviour
if node.namespace is not None:
if node._namespace:
return False
elif namespace == "":
# delb's specific behaviour
if node.namespace is not None:
if node._namespace:
return False
else:
if node.namespace != namespace:
if node._namespace != namespace:
return False

return node.local_name == self.local_name
Expand Down
20 changes: 1 addition & 19 deletions delb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
if TYPE_CHECKING:
from pathlib import Path

from _delb.typing import AttributeAccessor, Loader, NamespaceDeclarations
from _delb.typing import Loader, NamespaceDeclarations

# plugin loading

Expand Down Expand Up @@ -422,24 +422,6 @@ def merge_text_nodes(self):
"""
self.root.merge_text_nodes()

def new_tag_node(
self,
local_name: str,
attributes: Optional[dict[AttributeAccessor, str]] = None,
namespace: Optional[str] = None,
) -> TagNode:
"""
This method proxies to the :meth:`TagNode.new_tag_node` method of the
document's root node.
"""
warnings.warn(
"The this method will be removed. Use :func:`new_tag_node` instead.",
category=DeprecationWarning,
)
return self.root.new_tag_node(
local_name=local_name, attributes=attributes, namespace=namespace
)

def reduce_whitespace(self):
"""
Collapses and trims whitespace as described in this `TEI recommendation`_.
Expand Down
1 change: 0 additions & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ def test_no_preceding_node():
assert document.root.fetch_preceding() is None


@pytest.mark.filterwarnings("ignore::PendingDeprecationWarning")
@pytest.mark.parametrize("yes_or_no", (True, False))
def test_parse(yes_or_no):
data = "<node>foo<child><!--bar--></child></node>"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_indentation(indentation, in_, out):
(
'<r><ignored:d xmlns:ignored="d"/></r>',
{"wanted": "d"},
{None: "", "d": "wanted:"},
{"": "", "d": "wanted:"},
),
('<root xmlns:x="d" x:y=""/>', {"x": "d"}, {"": "", "d": "x:"}),
(
Expand Down
21 changes: 6 additions & 15 deletions tests/test_tag_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,6 @@ def test_location_path_and_xpath_concordance(file):
assert queried_nodes.first is node


def test_make_node_namespace_inheritance():
document = Document('<pfx:root xmlns:pfx="https://name.space"/>')
with pytest.deprecated_call():
node = document.new_tag_node("node")
assert node.namespace == "https://name.space"


def test_make_node_with_children():
result = new_tag_node("infocard", children=[tag("label")])
assert str(result) == "<infocard><label/></infocard>"
Expand Down Expand Up @@ -473,11 +466,8 @@ def test_make_node_outside_context():
assert str(root) == '<root xmlns="ham" xmlns:spam="https://spam"><spam:a/></root>'


def test_make_node_in_context_with_namespace():
document = Document("<root/>")

with pytest.deprecated_call():
node = document.new_tag_node("foo", namespace="https://name.space")
def test_make_node_with_namespace():
node = new_tag_node("foo", namespace="https://name.space")
assert node.namespace == "https://name.space"
assert node._etree_obj.tag == "{https://name.space}foo"

Expand Down Expand Up @@ -519,9 +509,10 @@ def test_new_tag_node_with_tag_attributes_input():
("http://", "c"): "d",
}

node = new_tag_node("node", namespace="http://")
new_node = node.new_tag_node("node_2", {"w": "x", "{gopher://}y": "z"})
assert new_node.attributes == {"{http://}w": "x", ("gopher://", "y"): "z"}
attributes = new_tag_node("node_1", {"a": "b"}, namespace="http://").attributes
assert new_tag_node("node_2", attributes).attributes == {
("http://", "a"): "b",
}


def test_fetch_following(files_path):
Expand Down

0 comments on commit 27fb08a

Please sign in to comment.