Skip to content

Commit

Permalink
style: resolve ruff suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Jan 16, 2025
1 parent c56dd8f commit ae92e90
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,5 @@ def typehints_formatter(annotation, config=None):

from sphinx_autodoc_typehints import format_annotation

return rf':py:class:`PyTree` \[{format_annotation(param,config=config)}]'
return rf':py:class:`PyTree` \[{format_annotation(param, config=config)}]'
return None
6 changes: 3 additions & 3 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class Foo1:
x: int
y: float

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):

@optree.dataclasses.dataclass(namespace='')
class Foo2:
Expand Down Expand Up @@ -684,7 +684,7 @@ def test_make_dataclass_with_invalid_namespace():
with pytest.raises(TypeError, match='The namespace must be a string'):
optree.dataclasses.make_dataclass('Foo1', ['x', ('y', int), ('z', float, 0.0)], namespace=1)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):
optree.dataclasses.make_dataclass(
'Foo2',
['x', ('y', int), ('z', float, 0.0)],
Expand Down Expand Up @@ -717,7 +717,7 @@ def test_make_dataclass_with_invalid_namespace():
namespace=None,
)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):
optree.dataclasses.make_dataclass(
'Foo2',
['x', ('y', int), ('z', float, 0.0)],
Expand Down
29 changes: 16 additions & 13 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ def test_max_depth():
lst = [lst]
with pytest.raises(
RecursionError,
match='Maximum recursion depth exceeded during flattening the tree.',
match=re.escape('Maximum recursion depth exceeded during flattening the tree.'),
):
list(optree.tree_iter(lst))
with pytest.raises(
RecursionError,
match='Maximum recursion depth exceeded during flattening the tree.',
match=re.escape('Maximum recursion depth exceeded during flattening the tree.'),
):
optree.tree_flatten(lst)
with pytest.raises(
RecursionError,
match='Maximum recursion depth exceeded during flattening the tree.',
match=re.escape('Maximum recursion depth exceeded during flattening the tree.'),
):
optree.tree_flatten_with_path(lst)
with pytest.raises(
RecursionError,
match='Maximum recursion depth exceeded during flattening the tree.',
match=re.escape('Maximum recursion depth exceeded during flattening the tree.'),
):
optree.tree_flatten_with_accessor(lst)

Expand Down Expand Up @@ -199,9 +199,9 @@ def test_flatten_dict_order():
def test_tree_unflatten_mismatch_number_of_leaves(tree, none_is_leaf, namespace):
leaves, treespec = optree.tree_flatten(tree, none_is_leaf=none_is_leaf, namespace=namespace)
if len(leaves) > 0:
with pytest.raises(ValueError, match='Too few leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match='Too few leaves for PyTreeSpec'):
optree.tree_unflatten(treespec, leaves[:-1])
with pytest.raises(ValueError, match='Too many leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match='Too many leaves for PyTreeSpec'):
optree.tree_unflatten(treespec, (*leaves, 0))


Expand Down Expand Up @@ -271,11 +271,11 @@ def f_leaf(leaf):
leaves, treespec = optree.tree_flatten(tree)

f_node, f_leaf, *_ = get_functions()
with pytest.raises(ValueError, match='Too few leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match=re.escape('Too few leaves for PyTreeSpec.')):
treespec.walk(leaves[:-1], f_node, f_leaf)

f_node, f_leaf, *_ = get_functions()
with pytest.raises(ValueError, match='Too many leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match=re.escape('Too many leaves for PyTreeSpec.')):
treespec.walk((*leaves, 0), f_node, f_leaf)

(
Expand Down Expand Up @@ -311,11 +311,11 @@ def f_leaf(leaf):
leaves, treespec = optree.tree_flatten(tree, none_is_leaf=True)

f_node, f_leaf, *_ = get_functions()
with pytest.raises(ValueError, match='Too few leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match=re.escape('Too few leaves for PyTreeSpec.')):
treespec.walk(leaves[:-1], f_node, f_leaf)

f_node, f_leaf, *_ = get_functions()
with pytest.raises(ValueError, match='Too many leaves for PyTreeSpec.'):
with pytest.raises(ValueError, match=re.escape('Too many leaves for PyTreeSpec.')):
treespec.walk((*leaves, 0), f_node, f_leaf)

(
Expand Down Expand Up @@ -2820,7 +2820,10 @@ class MyExtraDict(MyAnotherDict):
'b': MyExtraDict({'c': 4, 'd': 5, 'e': 6}),
},
)
with pytest.raises(ValueError, match='Tree structures must have the same namespace.'):
with pytest.raises(
ValueError,
match=re.escape('Tree structures must have the same namespace.'),
):
optree.tree_transpose(outer_treespec, inner_treespec, nested)

optree.register_pytree_node_class(MyExtraDict, namespace='namespace')
Expand Down Expand Up @@ -3125,7 +3128,7 @@ def test_tree_max():
assert optree.tree_max(None, default=0) == 0
assert optree.tree_max(None, none_is_leaf=True) is None
assert optree.tree_max(None, default=0, key=operator.neg) == 0
with pytest.raises(TypeError, match=".*operand type for unary .+: 'NoneType'"):
with pytest.raises(TypeError, match=r".*operand type for unary .+: 'NoneType'"):
assert optree.tree_max(None, default=0, key=operator.neg, none_is_leaf=True) is None


Expand All @@ -3144,7 +3147,7 @@ def test_tree_min():
assert optree.tree_min(None, default=0) == 0
assert optree.tree_min(None, none_is_leaf=True) is None
assert optree.tree_min(None, default=0, key=operator.neg) == 0
with pytest.raises(TypeError, match=".*operand type for unary .+: 'NoneType'"):
with pytest.raises(TypeError, match=r".*operand type for unary .+: 'NoneType'"):
assert optree.tree_min(None, default=0, key=operator.neg, none_is_leaf=True) is None


Expand Down
14 changes: 7 additions & 7 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def test_register_pytree_node_class_with_no_namespace():
with pytest.raises(
ValueError,
match='Must specify `namespace` when the first argument is a class.',
match=re.escape('Must specify `namespace` when the first argument is a class.'),
):

@optree.register_pytree_node_class
Expand All @@ -45,7 +45,7 @@ def tree_unflatten(cls, metadata, children):
def test_register_pytree_node_class_with_duplicate_namespace():
with pytest.raises(
ValueError,
match='Cannot specify `namespace` when the first argument is a string.',
match=re.escape('Cannot specify `namespace` when the first argument is a string.'),
):

@optree.register_pytree_node_class('mylist', namespace='mylist')
Expand Down Expand Up @@ -133,7 +133,7 @@ def tree_flatten(self):
def tree_unflatten(cls, metadata, children):
return cls(children)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):

@optree.register_pytree_node_class('')
class MyList2(UserList):
Expand All @@ -144,7 +144,7 @@ def tree_flatten(self):
def tree_unflatten(cls, metadata, children):
return cls(children)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):

@optree.register_pytree_node_class(namespace='')
class MyList3(UserList):
Expand All @@ -163,7 +163,7 @@ def tree_unflatten(cls, metadata, children):
namespace=1,
)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):
optree.register_pytree_node(
set,
lambda s: (sorted(s), None, None),
Expand Down Expand Up @@ -626,7 +626,7 @@ def test_unregister_pytree_node_with_invalid_namespace():
with pytest.raises(TypeError, match='The namespace must be a string'):
optree.unregister_pytree_node(set, namespace=1)

with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):
optree.unregister_pytree_node(set, namespace='')


Expand Down Expand Up @@ -898,7 +898,7 @@ def test_dict_insertion_order_with_invalid_namespace():
with pytest.raises(TypeError, match='The namespace must be a string'):
with optree.dict_insertion_ordered(True, namespace=1):
pass
with pytest.raises(ValueError, match='The namespace cannot be an empty string.'):
with pytest.raises(ValueError, match=re.escape('The namespace cannot be an empty string.')):
with optree.dict_insertion_ordered(True, namespace=''):
pass

Expand Down

0 comments on commit ae92e90

Please sign in to comment.