1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
# Adapted from https://github.com/python/cpython/blob/master/Lib/test/test_operator.py
# Licensed under the Python Software Foundation License Version 2.
# Copyright © 2001-2020 Python Software Foundation. All rights reserved.
# Copyright © 2000 BeOpen.com. All rights reserved.
# Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
# Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
#
# stdlib
import pickle
from typing import Any
# 3rd party
import pytest
from funcy.funcs import rpartial # type: ignore
# this package
import domdf_python_tools
from domdf_python_tools.getters import attrgetter, itemgetter, methodcaller
evaluate = rpartial(eval, {"domdf_python_tools": domdf_python_tools}, {"domdf_python_tools": domdf_python_tools})
class TestAttrgetter:
def test_attrgetter(self):
class A:
pass
a = A()
a.name = "john" # type: ignore
b = A()
b.name = "graham" # type: ignore
f = attrgetter(0, "name")
assert f([a, b]) == "john"
f = attrgetter(1, "name")
assert f([a, b]) == "graham"
with pytest.raises(TypeError, match=r"__call__\(\) missing 1 required positional argument: 'obj'"):
f() # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) takes 2 positional arguments but 3 were given"):
f(a, "cleese") # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) got an unexpected keyword argument 'surname'"):
f(a, surname="cleese") # type: ignore
f = attrgetter(0, "rank")
with pytest.raises(AttributeError, match="'A' object has no attribute 'rank'"):
f([a, b])
with pytest.raises(TypeError, match="attribute name must be a string"):
attrgetter(0, 2) # type: ignore[arg-type]
with pytest.raises(TypeError, match="'idx' must be an integer"):
attrgetter("hello", 0) # type: ignore[arg-type]
with pytest.raises(TypeError, match=r"__init__\(\) missing 1 required positional argument: 'attr'"):
attrgetter(0) # type: ignore[call-arg]
f = attrgetter(1, "name")
with pytest.raises(IndexError, match="list index out of range"):
f([])
class C:
def __getattr__(self, name):
raise SyntaxError
with pytest.raises(SyntaxError, match="None"):
attrgetter(0, "foo")([C()])
# recursive gets
a = A()
a.name = "john" # type: ignore
a.child = A() # type: ignore
a.child.name = "thomas" # type: ignore
f = attrgetter(3, "child.name")
assert f([1, 2, 3, a]) == "thomas"
with pytest.raises(AttributeError, match="'A' object has no attribute 'child'"):
f([1, 2, 3, a.child]) # type: ignore
f = attrgetter(1, "child.name")
assert f([1, a]) == "thomas"
f = attrgetter(2, "child.child.name")
with pytest.raises(AttributeError, match="'A' object has no attribute 'child'"):
f([1, 2, a])
f = attrgetter(0, "child.")
with pytest.raises(AttributeError, match="'A' object has no attribute ''"):
f([a])
f = attrgetter(0, ".child")
with pytest.raises(AttributeError, match="'A' object has no attribute ''"):
f([a])
a.child.child = A() # type: ignore
a.child.child.name = "johnson" # type: ignore
f = attrgetter(0, "child.child.name")
assert f([a]) == "johnson"
@pytest.mark.parametrize("proto", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle(self, proto: int):
class A:
pass
a = A()
a.x = 'X' # type: ignore
a.y = 'Y' # type: ignore
a.z = 'Z' # type: ignore
a.t = A() # type: ignore
a.t.u = A() # type: ignore
a.t.u.v = 'V' # type: ignore
f = attrgetter(0, 'x')
f2 = copy(f, proto)
assert repr(f2) == repr(f)
assert f2([a]) == f([a])
def test_repr(self):
assert repr(attrgetter(0, "name")) == "domdf_python_tools.getters.attrgetter(idx=0, attr='name')"
assert repr(attrgetter(1, "value")) == "domdf_python_tools.getters.attrgetter(idx=1, attr='value')"
evaluate(repr(attrgetter(0, "name")))
evaluate(repr(attrgetter(1, "value")))
class TestItemgetter:
def test_itemgetter(self):
a = "ABCDE"
f = itemgetter(0, 2)
assert f([a]) == 'C'
f = itemgetter(2, 2)
assert f([1, 2, a]) == 'C'
with pytest.raises(TypeError, match=r"__call__\(\) missing 1 required positional argument: 'obj'"):
f() # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) takes 2 positional arguments but 3 were given"):
f(a, 3) # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) got an unexpected keyword argument 'size'"):
f(a, size=3) # type: ignore
f = itemgetter(1, 10)
with pytest.raises(IndexError, match="list index out of range"):
f([])
with pytest.raises(IndexError, match="list index out of range"):
f([1])
with pytest.raises(IndexError, match="string index out of range"):
f([(), a])
class C:
def __getitem__(self, name):
raise SyntaxError
with pytest.raises(SyntaxError, match="None"):
itemgetter(2, 42)([1, (), C()])
f = itemgetter(0, "name")
with pytest.raises(TypeError, match="string( index)? indices must be integers( or slices, not str)?"):
f([a])
with pytest.raises(
TypeError,
match=r"__init__\(\) missing 2 required positional arguments: 'idx' and 'item'",
):
itemgetter() # type: ignore
with pytest.raises(TypeError, match=r"__init__\(\) missing 1 required positional argument: 'item'"):
itemgetter(1) # type: ignore
with pytest.raises(TypeError, match=r"__init__\(\) missing 1 required positional argument: 'item'"):
itemgetter("abc") # type: ignore
with pytest.raises(TypeError, match="'idx' must be an integer"):
itemgetter("abc", 2) # type: ignore
d = dict(key="val")
f = itemgetter(1, "key")
assert f([{}, d]) == "val"
f = itemgetter(1, "nonkey")
with pytest.raises(KeyError, match="nonkey"):
f([{}, d])
inventory = [("apple", 3), ("pear", 5), ("banana", 2), ("orange", 1)]
getcount = itemgetter(0, 1)
assert list(map(getcount, inventory)) == ['p', 'e', 'a', 'r']
assert sorted(inventory, key=getcount) == [("banana", 2), ("pear", 5), ("apple", 3), ("orange", 1)]
# interesting indices
t = tuple("abcde")
assert itemgetter(-1, -1)([1, 2, t]) == 'e'
assert itemgetter(1, slice(2, 4))([1, t]) == ('c', 'd')
# interesting sequences
class T(tuple):
"""
Tuple subclass
"""
assert itemgetter(2, 0)([T("abc"), T("def"), T("ghi")]) == 'g'
assert itemgetter(2, 0)([range(100, 200), range(200, 300), range(300, 400)]) == 300
@pytest.mark.parametrize("proto", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle(self, proto: int):
a = "ABCDE"
f = itemgetter(0, 2)
f2 = copy(f, proto)
assert repr(f2) == repr(f)
assert f2([a]) == f([a])
def test_repr(self):
assert repr(itemgetter(0, 1)) == "domdf_python_tools.getters.itemgetter(idx=0, item=1)"
assert repr(itemgetter(1, 2)) == "domdf_python_tools.getters.itemgetter(idx=1, item=2)"
evaluate(repr(itemgetter(0, 1)))
evaluate(repr(itemgetter(1, 2)))
class TestMethodcaller:
def test_methodcaller(self):
with pytest.raises(
TypeError,
match=r"__init__\(\) missing 2 required positional arguments: '_idx' and '_name'",
):
methodcaller() # type: ignore
with pytest.raises(TypeError, match=r"__init__\(\) missing 1 required positional argument: '_name'"):
methodcaller(12) # type: ignore
with pytest.raises(TypeError, match=r"__init__\(\) missing 1 required positional argument: '_name'"):
methodcaller("name") # type: ignore
with pytest.raises(TypeError, match="'_idx' must be an integer"):
methodcaller("name", 12) # type: ignore
with pytest.raises(TypeError, match="method name must be a string"):
methodcaller(0, 12) # type: ignore
f = methodcaller(1, "foo")
with pytest.raises(IndexError, match="list index out of range"):
f([])
with pytest.raises(IndexError, match="list index out of range"):
f([1])
class A:
def foo(self, *args, **kwds):
return args[0] + args[1]
def bar(self, f=42): # noqa: MAN001,MAN002
return f
def baz(*args, **kwds): # noqa: MAN002
return kwds["name"], kwds["self"]
a = A()
f = methodcaller(2, "foo")
with pytest.raises(IndexError, match="tuple index out of range"):
f(["abc", 123, a])
f = methodcaller(1, "foo", 1, 2)
assert f([1, a]) == 3
with pytest.raises(TypeError, match=r"__call__\(\) missing 1 required positional argument: 'obj'"):
f() # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) takes 2 positional arguments but 3 were given"):
f(a, 3) # type: ignore
with pytest.raises(TypeError, match=r"__call__\(\) got an unexpected keyword argument 'spam'"):
f(a, spam=3) # type: ignore
f = methodcaller(0, "bar")
assert f([a]) == 42
with pytest.raises(TypeError, match=r"__call__\(\) takes 2 positional arguments but 3 were given"):
f([a], [a]) # type: ignore
f = methodcaller(0, "bar", f=5)
assert f([a]) == 5
f = methodcaller(0, "baz", name="spam", self="eggs")
assert f([a]) == ("spam", "eggs")
@pytest.mark.parametrize("proto", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle(self, proto: int):
class A:
def foo(self, *args, **kwds):
return args[0] + args[1]
def bar(self, f=42):
return f
def baz(*args, **kwds):
return kwds["name"], kwds["self"]
a = A()
f = methodcaller(0, "bar")
f2 = copy(f, proto)
assert repr(f2) == repr(f)
assert f2([a]) == f([a])
# positional args
f = methodcaller(0, "foo", 1, 2)
f2 = copy(f, proto)
assert repr(f2) == repr(f)
assert f2([a]) == f([a])
# keyword args
f = methodcaller(0, "bar", f=5)
f2 = copy(f, proto)
assert repr(f2) == repr(f)
assert f2([a]) == f([a])
f = methodcaller(0, "baz", self="eggs", name="spam")
f2 = copy(f, proto)
# Can't test repr consistently with multiple keyword args
assert f2([a]) == f([a])
def test_repr(self):
assert repr(methodcaller(0, "lower")) == "domdf_python_tools.getters.methodcaller(0, 'lower')"
assert repr(methodcaller(1, "__iter__")) == "domdf_python_tools.getters.methodcaller(1, '__iter__')"
assert repr(
methodcaller(1, "__iter__", "arg1")
) == "domdf_python_tools.getters.methodcaller(1, '__iter__', 'arg1')"
assert repr(
methodcaller(1, "__iter__", kw1="kwarg1")
) == "domdf_python_tools.getters.methodcaller(1, '__iter__', kw1='kwarg1')"
assert repr(
methodcaller(1, "__iter__", "arg1", "arg2", kw1="kwarg1", kw2="kwarg2")
) == "domdf_python_tools.getters.methodcaller(1, '__iter__', 'arg1', 'arg2', kw1='kwarg1', kw2='kwarg2')"
evaluate(repr(methodcaller(0, "lower")))
evaluate(repr(methodcaller(1, "__iter__")))
evaluate(repr(methodcaller(1, "__iter__", "arg1")))
evaluate(repr(methodcaller(1, "__iter__", kw1="kwarg1")))
evaluate(repr(methodcaller(1, "__iter__", "arg1", "arg2", kw1="kwarg1", kw2="kwarg2")))
def copy(obj: Any, proto: int):
pickled = pickle.dumps(obj, proto)
return pickle.loads(pickled) # nosec: B301
|