| 1 |
import pytest |
| 2 |
from pytest import raises, skip |
| 3 |
import sys |
| 4 |
|
| 5 |
IS_PYPY = "__pypy__" in sys.builtin_module_names |
| 6 |
|
| 7 |
class C: |
| 8 |
def foo(self): |
| 9 |
pass |
| 10 |
|
| 11 |
MethodType = type(C().foo) |
| 12 |
|
| 13 |
def test_attributes(): |
| 14 |
globals()['__name__'] = 'mymodulename' |
| 15 |
def f(): pass |
| 16 |
assert hasattr(f, '__code__') |
| 17 |
assert f.__defaults__ == None |
| 18 |
f.__defaults__ = None |
| 19 |
assert f.__defaults__ == None |
| 20 |
assert f.__dict__ == {} |
| 21 |
assert type(f.__globals__) == dict |
| 22 |
assert f.__closure__ is None |
| 23 |
assert f.__doc__ == None |
| 24 |
assert f.__name__ == 'f' |
| 25 |
assert f.__module__ == 'mymodulename' |
| 26 |
|
| 27 |
def test_qualname(): |
| 28 |
def f(): |
| 29 |
def g(): |
| 30 |
pass |
| 31 |
return g |
| 32 |
assert f.__qualname__ == 'test_qualname.<locals>.f' |
| 33 |
assert f().__qualname__ == 'test_qualname.<locals>.f.<locals>.g' |
| 34 |
f.__qualname__ = 'qualname' |
| 35 |
assert f.__qualname__ == 'qualname' |
| 36 |
with raises(TypeError): |
| 37 |
f.__qualname__ = b'name' |
| 38 |
|
| 39 |
def test_qualname_method(): |
| 40 |
class A: |
| 41 |
def f(self): |
| 42 |
pass |
| 43 |
assert A.f.__qualname__ == 'test_qualname_method.<locals>.A.f' |
| 44 |
|
| 45 |
def test_qualname_global(): |
| 46 |
def f(): |
| 47 |
global inner_global |
| 48 |
def inner_global(): |
| 49 |
def inner_function2(): |
| 50 |
pass |
| 51 |
return inner_function2 |
| 52 |
return inner_global |
| 53 |
assert f().__qualname__ == 'inner_global' |
| 54 |
assert f()().__qualname__ == 'inner_global.<locals>.inner_function2' |
| 55 |
|
| 56 |
def test_classmethod_reduce(): |
| 57 |
class X(object): |
| 58 |
@classmethod |
| 59 |
def y(cls): |
| 60 |
pass |
| 61 |
|
| 62 |
f, args = X.y.__reduce__() |
| 63 |
assert f(*args) == X.y |
| 64 |
|
| 65 |
|
| 66 |
assert f == getattr |
| 67 |
assert args == (X, "y") |
| 68 |
|
| 69 |
def test_annotations(): |
| 70 |
def f(): pass |
| 71 |
ann = f.__annotations__ |
| 72 |
assert ann == {} |
| 73 |
assert f.__annotations__ is ann |
| 74 |
with raises(TypeError): |
| 75 |
setattr(f, "__annotations__", 42) |
| 76 |
del f.__annotations__ |
| 77 |
assert f.__annotations__ is not ann |
| 78 |
f.__annotations__ = ann |
| 79 |
assert f.__annotations__ is ann |
| 80 |
|
| 81 |
def test_annotations_mangle(): |
| 82 |
class X: |
| 83 |
def foo(self, __a:5, b:6): |
| 84 |
pass |
| 85 |
assert X.foo.__annotations__ == {'_X__a': 5, 'b': 6} |
| 86 |
|
| 87 |
def test_kwdefaults(): |
| 88 |
def f(*, kw=3): return kw |
| 89 |
assert f.__kwdefaults__ == {"kw" : 3} |
| 90 |
f.__kwdefaults__["kw"] = 4 |
| 91 |
assert f() == 4 |
| 92 |
f.__kwdefaults__ = {"kw" : 5} |
| 93 |
assert f() == 5 |
| 94 |
del f.__kwdefaults__ |
| 95 |
assert f.__kwdefaults__ is None |
| 96 |
raises(TypeError, f) |
| 97 |
assert f(kw=42) == 42 |
| 98 |
def f(*, 日本=3): return kw |
| 99 |
assert f.__kwdefaults__ == {"日本" : 3} |
| 100 |
|
| 101 |
def test_kw_nonascii(): |
| 102 |
def f(日本: str=1): |
| 103 |
return 日本 |
| 104 |
assert f.__annotations__ == {'日本': str} |
| 105 |
assert f() == 1 |
| 106 |
assert f(日本='bar') == 'bar' |
| 107 |
|
| 108 |
def test_code_is_ok(): |
| 109 |
def f(): pass |
| 110 |
assert not hasattr(f.__code__, '__dict__') |
| 111 |
|
| 112 |
def test_underunder_attributes(): |
| 113 |
def f(): pass |
| 114 |
assert f.__name__ == 'f' |
| 115 |
assert f.__doc__ == None |
| 116 |
assert f.__dict__ == {} |
| 117 |
assert f.__code__.co_name == 'f' |
| 118 |
assert f.__defaults__ is None |
| 119 |
assert f.__globals__ is globals() |
| 120 |
assert hasattr(f, '__class__') |
| 121 |
|
| 122 |
def test_classmethod(): |
| 123 |
def f(a: int) -> int: |
| 124 |
"""echo input""" |
| 125 |
return a |
| 126 |
sm = staticmethod(f) |
| 127 |
assert sm(42) == 42 |
| 128 |
for cm in (classmethod(f), sm): |
| 129 |
assert cm.__func__ is f |
| 130 |
assert cm.__func__ == cm.__wrapped__ |
| 131 |
for attr in ('__module__', '__name__', '__qualname__', '__doc__', |
| 132 |
'__annotations__'): |
| 133 |
assert getattr(f, attr) == getattr(cm, attr) |
| 134 |
|
| 135 |
def test_static_class_method_repr(): |
| 136 |
def f(): |
| 137 |
pass |
| 138 |
for m in classmethod(f), staticmethod(f): |
| 139 |
assert repr(f) in repr(m) |
| 140 |
|
| 141 |
def test_cannot_pickle_static_and_classmethod(): |
| 142 |
with pytest.raises(TypeError): |
| 143 |
staticmethod(lambda : 1).__reduce_ex__(4) |
| 144 |
with pytest.raises(TypeError): |
| 145 |
classmethod(lambda cls: 1).__reduce_ex__(4) |
| 146 |
|
| 147 |
def test_write___doc__(): |
| 148 |
def f(): "hello" |
| 149 |
assert f.__doc__ == 'hello' |
| 150 |
f.__doc__ = 'good bye' |
| 151 |
assert f.__doc__ == 'good bye' |
| 152 |
del f.__doc__ |
| 153 |
assert f.__doc__ == None |
| 154 |
|
| 155 |
def test_write_module(): |
| 156 |
def f(): "hello" |
| 157 |
f.__module__ = 'ab.c' |
| 158 |
assert f.__module__ == 'ab.c' |
| 159 |
del f.__module__ |
| 160 |
assert f.__module__ is None |
| 161 |
|
| 162 |
def test_new(): |
| 163 |
def f(): return 42 |
| 164 |
FuncType = type(f) |
| 165 |
f2 = FuncType(f.__code__, f.__globals__, 'f2', None, None) |
| 166 |
assert f2() == 42 |
| 167 |
|
| 168 |
def g(x): |
| 169 |
def f(): |
| 170 |
return x |
| 171 |
return f |
| 172 |
f = g(42) |
| 173 |
with raises(TypeError): |
| 174 |
FuncType(f.__code__, f.__globals__, 'f2', None, None) |
| 175 |
|
| 176 |
def test_write_code(): |
| 177 |
def f(): |
| 178 |
return 42 |
| 179 |
def g(): |
| 180 |
return 41 |
| 181 |
assert f() == 42 |
| 182 |
assert g() == 41 |
| 183 |
with raises(TypeError): |
| 184 |
f.__code__ = 1 |
| 185 |
f.__code__ = g.__code__ |
| 186 |
assert f() == 41 |
| 187 |
def get_h(f=f): |
| 188 |
def h(): |
| 189 |
return f() |
| 190 |
return h |
| 191 |
h = get_h() |
| 192 |
with raises(ValueError): |
| 193 |
f.__code__ = h.__code__ |
| 194 |
|
| 195 |
def test_write_code_builtin_forbidden(): |
| 196 |
def f(*args): |
| 197 |
return 42 |
| 198 |
with raises(AttributeError): |
| 199 |
dir.__code__ = f.__code__ |
| 200 |
with raises(AttributeError): |
| 201 |
list.append.__code__ = f.__code__ |
| 202 |
|
| 203 |
def test_write_attributes_builtin_forbidden(): |
| 204 |
for func in [dir, dict.get]: |
| 205 |
with raises(AttributeError): |
| 206 |
func.__defaults__ = (1, ) |
| 207 |
with raises(AttributeError): |
| 208 |
del func.__defaults__ |
| 209 |
with raises(AttributeError): |
| 210 |
func.__doc__ = "" |
| 211 |
with raises(AttributeError): |
| 212 |
del func.__doc__ |
| 213 |
with raises(AttributeError): |
| 214 |
func.__name__ = "" |
| 215 |
if func is dir and not IS_PYPY: |
| 216 |
|
| 217 |
func.__module__ = "" |
| 218 |
del func.__module__ |
| 219 |
else: |
| 220 |
|
| 221 |
with raises(AttributeError): |
| 222 |
func.__module__ = "" |
| 223 |
with raises(AttributeError): |
| 224 |
del func.__module__ |
| 225 |
|
| 226 |
|
| 227 |
def test_write_attributes_builtin_forbidden_py3(): |
| 228 |
for func in [dir, dict.get]: |
| 229 |
with raises(AttributeError): |
| 230 |
func.__qualname__ = "abc" |
| 231 |
with raises(AttributeError): |
| 232 |
func.__annotations__ = {} |
| 233 |
del func.__annotations__ |
| 234 |
|
| 235 |
|
| 236 |
def test_func_nonascii(): |
| 237 |
def 日本(): |
| 238 |
pass |
| 239 |
assert repr(日本).startswith( |
| 240 |
'<function test_func_nonascii.<locals>.日本 at ') |
| 241 |
assert 日本.__name__ == '日本' |
| 242 |
|
| 243 |
def test_simple_call(): |
| 244 |
def func(arg1, arg2): |
| 245 |
return arg1, arg2 |
| 246 |
res = func(23,42) |
| 247 |
assert res[0] == 23 |
| 248 |
assert res[1] == 42 |
| 249 |
|
| 250 |
def test_simple_call_default(): |
| 251 |
def func(arg1, arg2=11, arg3=111): |
| 252 |
return arg1, arg2, arg3 |
| 253 |
res = func(1) |
| 254 |
assert res[0] == 1 |
| 255 |
assert res[1] == 11 |
| 256 |
assert res[2] == 111 |
| 257 |
res = func(1, 22) |
| 258 |
assert res[0] == 1 |
| 259 |
assert res[1] == 22 |
| 260 |
assert res[2] == 111 |
| 261 |
res = func(1, 22, 333) |
| 262 |
assert res[0] == 1 |
| 263 |
assert res[1] == 22 |
| 264 |
assert res[2] == 333 |
| 265 |
|
| 266 |
with raises(TypeError): |
| 267 |
func() |
| 268 |
with raises(TypeError): |
| 269 |
func(1, 2, 3, 4) |
| 270 |
|
| 271 |
def test_simple_varargs(): |
| 272 |
def func(arg1, *args): |
| 273 |
return arg1, args |
| 274 |
res = func(23,42) |
| 275 |
assert res[0] == 23 |
| 276 |
assert res[1] == (42,) |
| 277 |
|
| 278 |
res = func(23, *(42,)) |
| 279 |
assert res[0] == 23 |
| 280 |
assert res[1] == (42,) |
| 281 |
|
| 282 |
def test_simple_kwargs(): |
| 283 |
def func(arg1, **kwargs): |
| 284 |
return arg1, kwargs |
| 285 |
res = func(23, value=42) |
| 286 |
assert res[0] == 23 |
| 287 |
assert res[1] == {'value': 42} |
| 288 |
|
| 289 |
res = func(23, **{'value': 42}) |
| 290 |
assert res[0] == 23 |
| 291 |
assert res[1] == {'value': 42} |
| 292 |
|
| 293 |
def test_kwargs_sets_wrong_positional_raises(): |
| 294 |
def func(arg1): |
| 295 |
pass |
| 296 |
with raises(TypeError): |
| 297 |
func(arg2=23) |
| 298 |
|
| 299 |
def test_kwargs_sets_positional(): |
| 300 |
def func(arg1): |
| 301 |
return arg1 |
| 302 |
res = func(arg1=42) |
| 303 |
assert res == 42 |
| 304 |
|
| 305 |
def test_kwargs_sets_positional_mixed(): |
| 306 |
def func(arg1, **kw): |
| 307 |
return arg1, kw |
| 308 |
res = func(arg1=42, something=23) |
| 309 |
assert res[0] == 42 |
| 310 |
assert res[1] == {'something': 23} |
| 311 |
|
| 312 |
def test_kwargs_sets_positional_twice(): |
| 313 |
def func(arg1, **kw): |
| 314 |
return arg1, kw |
| 315 |
with raises(TypeError): |
| 316 |
func(42, {'arg1': 23}) |
| 317 |
|
| 318 |
def test_kwargs_nondict_mapping(): |
| 319 |
class Mapping: |
| 320 |
def keys(self): |
| 321 |
return ('a', 'b') |
| 322 |
def __getitem__(self, key): |
| 323 |
return key |
| 324 |
def func(arg1, **kw): |
| 325 |
return arg1, kw |
| 326 |
res = func(23, **Mapping()) |
| 327 |
assert res[0] == 23 |
| 328 |
assert res[1] == {'a': 'a', 'b': 'b'} |
| 329 |
with raises(TypeError) as excinfo: |
| 330 |
func(42, **[]) |
| 331 |
assert ('func() argument after ** must be a mapping, not list' in |
| 332 |
str(excinfo.value)) |
| 333 |
|
| 334 |
def test_star_error(): |
| 335 |
def f(): pass |
| 336 |
msg = "Value after * must be an iterable, not int" |
| 337 |
with raises(TypeError) as exc: |
| 338 |
f(1, *42) |
| 339 |
assert str(exc.value).endswith(msg) |
| 340 |
with raises(TypeError) as exc: |
| 341 |
(1, *1) |
| 342 |
assert str(exc.value).endswith(msg) |
| 343 |
with raises(TypeError) as exc: |
| 344 |
(*1, 22) |
| 345 |
|
| 346 |
def test_star_badarg(): |
| 347 |
class BrokenSequence: |
| 348 |
def __getitem__(self, idx): |
| 349 |
raise TypeError("myerror") |
| 350 |
|
| 351 |
def f(): pass |
| 352 |
|
| 353 |
with raises(TypeError) as e: |
| 354 |
f(1, 2, *BrokenSequence()) |
| 355 |
assert str(e.value) == "myerror" |
| 356 |
|
| 357 |
with raises(TypeError) as e: |
| 358 |
[*BrokenSequence()] |
| 359 |
assert str(e.value) == "myerror" |
| 360 |
|
| 361 |
def test_default_arg(): |
| 362 |
def func(arg1,arg2=42): |
| 363 |
return arg1, arg2 |
| 364 |
res = func(arg1=23) |
| 365 |
assert res[0] == 23 |
| 366 |
assert res[1] == 42 |
| 367 |
|
| 368 |
def test_defaults_keyword_overrides(): |
| 369 |
def func(arg1=42, arg2=23): |
| 370 |
return arg1, arg2 |
| 371 |
res = func(arg1=23) |
| 372 |
assert res[0] == 23 |
| 373 |
assert res[1] == 23 |
| 374 |
|
| 375 |
def test_defaults_keyword_override_but_leaves_empty_positional(): |
| 376 |
def func(arg1,arg2=42): |
| 377 |
return arg1, arg2 |
| 378 |
with raises(TypeError): |
| 379 |
func(arg2=23) |
| 380 |
|
| 381 |
def test_kwargs_disallows_same_name_twice(): |
| 382 |
def func(arg1, **kw): |
| 383 |
return arg1, kw |
| 384 |
with raises(TypeError): |
| 385 |
func(42, **{'arg1': 23}) |
| 386 |
|
| 387 |
def test_kwargs_bound_blind(): |
| 388 |
class A(object): |
| 389 |
def func(self, **kw): |
| 390 |
return self, kw |
| 391 |
func = A().func |
| 392 |
with raises(TypeError): |
| 393 |
func(self=23) |
| 394 |
with raises(TypeError): |
| 395 |
func(**{'self': 23}) |
| 396 |
|
| 397 |
def test_kwargs_confusing_name(): |
| 398 |
def func(self): |
| 399 |
return self*7 |
| 400 |
res = func(self=6) |
| 401 |
assert res == 42 |
| 402 |
|
| 403 |
def test_get(): |
| 404 |
def func(self): return self |
| 405 |
obj = object() |
| 406 |
meth = func.__get__(obj, object) |
| 407 |
assert meth() == obj |
| 408 |
|
| 409 |
def test_get_none_none_invalid(): |
| 410 |
def func(): ... |
| 411 |
|
| 412 |
with pytest.raises(TypeError): |
| 413 |
func.__get__(None, None) |
| 414 |
with pytest.raises(TypeError): |
| 415 |
func.__get__(None) |
| 416 |
assert func.__get__(None, type(func)) is func |
| 417 |
|
| 418 |
@pytest.mark.skipif(IS_PYPY, reason="XXX issue #2083") |
| 419 |
def test_none_get_interaction(): |
| 420 |
assert type(None).__repr__(None) == 'None' |
| 421 |
|
| 422 |
def test_none_get_interaction_2(): |
| 423 |
f = None.__repr__ |
| 424 |
assert f() == 'None' |
| 425 |
|
| 426 |
def test_no_get_builtin(): |
| 427 |
assert not hasattr(dir, '__get__') |
| 428 |
class A(object): |
| 429 |
ord = ord |
| 430 |
a = A() |
| 431 |
assert a.ord('a') == 97 |
| 432 |
|
| 433 |
def test_builtin_as_special_method_is_not_bound(): |
| 434 |
class A(object): |
| 435 |
__getattr__ = len |
| 436 |
a = A() |
| 437 |
assert a.a == 1 |
| 438 |
assert a.ab == 2 |
| 439 |
assert a.abcdefghij == 10 |
| 440 |
|
| 441 |
def test_call_builtin(): |
| 442 |
s = 'hello' |
| 443 |
with raises(TypeError): |
| 444 |
len() |
| 445 |
assert len(s) == 5 |
| 446 |
with raises(TypeError): |
| 447 |
len(s, s) |
| 448 |
with raises(TypeError): |
| 449 |
len(s, s, s) |
| 450 |
assert len(*[s]) == 5 |
| 451 |
assert len(s, *[]) == 5 |
| 452 |
with raises(TypeError): |
| 453 |
len(some_unknown_keyword=s) |
| 454 |
with raises(TypeError): |
| 455 |
len(s, some_unknown_keyword=s) |
| 456 |
with raises(TypeError): |
| 457 |
len(s, s, some_unknown_keyword=s) |
| 458 |
|
| 459 |
def test_call_error_message(): |
| 460 |
try: |
| 461 |
len() |
| 462 |
except TypeError as e: |
| 463 |
msg = str(e) |
| 464 |
if IS_PYPY: |
| 465 |
assert "len() missing 1 required positional argument: 'obj'" in msg |
| 466 |
else: |
| 467 |
assert "len() takes exactly 1 argument (0 given" in msg |
| 468 |
else: |
| 469 |
assert 0, "did not raise" |
| 470 |
|
| 471 |
try: |
| 472 |
len(1, 2) |
| 473 |
except TypeError as e: |
| 474 |
msg = str(e) |
| 475 |
if IS_PYPY: |
| 476 |
assert "len() takes 1 positional argument but 2 were given" in msg |
| 477 |
else: |
| 478 |
assert "len() takes exactly 1 argument (2 given" in msg |
| 479 |
else: |
| 480 |
assert 0, "did not raise" |
| 481 |
|
| 482 |
def test_unicode_docstring(): |
| 483 |
def f(): |
| 484 |
"hi" |
| 485 |
assert f.__doc__ == "hi" |
| 486 |
assert type(f.__doc__) is str |
| 487 |
|
| 488 |
def test_issue1293(): |
| 489 |
def f1(): "doc f1" |
| 490 |
def f2(): "doc f2" |
| 491 |
f1.__code__ = f2.__code__ |
| 492 |
assert f1.__doc__ == "doc f1" |
| 493 |
|
| 494 |
def test_subclassing(): |
| 495 |
|
| 496 |
def f(): |
| 497 |
pass |
| 498 |
with raises(TypeError): |
| 499 |
type('Foo', (type(f),), {}) |
| 500 |
with raises(TypeError): |
| 501 |
type('Foo', (type(len),), {}) |
| 502 |
|
| 503 |
def test_lambda_docstring(): |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
f = lambda: 42 |
| 508 |
assert f.__doc__ is None |
| 509 |
|
| 510 |
def test_simple_call(): |
| 511 |
class A(object): |
| 512 |
def func(self, arg2): |
| 513 |
return self, arg2 |
| 514 |
a = A() |
| 515 |
res = a.func(42) |
| 516 |
assert res[0] is a |
| 517 |
assert res[1] == 42 |
| 518 |
|
| 519 |
def test_simple_varargs(): |
| 520 |
class A(object): |
| 521 |
def func(self, *args): |
| 522 |
return self, args |
| 523 |
a = A() |
| 524 |
res = a.func(42) |
| 525 |
assert res[0] is a |
| 526 |
assert res[1] == (42,) |
| 527 |
|
| 528 |
res = a.func(*(42,)) |
| 529 |
assert res[0] is a |
| 530 |
assert res[1] == (42,) |
| 531 |
|
| 532 |
def test_obscure_varargs(): |
| 533 |
class A(object): |
| 534 |
def func(*args): |
| 535 |
return args |
| 536 |
a = A() |
| 537 |
res = a.func(42) |
| 538 |
assert res[0] is a |
| 539 |
assert res[1] == 42 |
| 540 |
|
| 541 |
res = a.func(*(42,)) |
| 542 |
assert res[0] is a |
| 543 |
assert res[1] == 42 |
| 544 |
|
| 545 |
def test_simple_kwargs(): |
| 546 |
class A(object): |
| 547 |
def func(self, **kwargs): |
| 548 |
return self, kwargs |
| 549 |
a = A() |
| 550 |
|
| 551 |
res = a.func(value=42) |
| 552 |
assert res[0] is a |
| 553 |
assert res[1] == {'value': 42} |
| 554 |
|
| 555 |
res = a.func(**{'value': 42}) |
| 556 |
assert res[0] is a |
| 557 |
assert res[1] == {'value': 42} |
| 558 |
|
| 559 |
def test_get(): |
| 560 |
def func(self): return self |
| 561 |
class Object(object): pass |
| 562 |
obj = Object() |
| 563 |
|
| 564 |
obj.meth = func.__get__(obj, Object) |
| 565 |
assert obj.meth() == obj |
| 566 |
|
| 567 |
meth2 = obj.meth.__get__(obj, Object) |
| 568 |
assert meth2() == obj |
| 569 |
|
| 570 |
def test_get_get(): |
| 571 |
|
| 572 |
def m(self): return self |
| 573 |
class C(object): pass |
| 574 |
class D(C): pass |
| 575 |
C.m = m |
| 576 |
D.m = C.m |
| 577 |
c = C() |
| 578 |
assert c.m() == c |
| 579 |
d = D() |
| 580 |
assert d.m() == d |
| 581 |
|
| 582 |
def test_method_eq(): |
| 583 |
class C(object): |
| 584 |
def m(): pass |
| 585 |
c = C() |
| 586 |
assert C.m == C.m |
| 587 |
assert c.m == c.m |
| 588 |
assert not (C.m == c.m) |
| 589 |
assert not (c.m == C.m) |
| 590 |
c2 = C() |
| 591 |
assert (c.m == c2.m) is False |
| 592 |
assert (c.m != c2.m) is True |
| 593 |
assert (c.m != c.m) is False |
| 594 |
|
| 595 |
def test_method_eq_bug(): |
| 596 |
|
| 597 |
|
| 598 |
class A: |
| 599 |
def __eq__(self, other): |
| 600 |
return True |
| 601 |
def f(self): pass |
| 602 |
|
| 603 |
assert A().f != A().f |
| 604 |
|
| 605 |
def test_method_hash(): |
| 606 |
class C(object): |
| 607 |
def m(): pass |
| 608 |
class D(C): |
| 609 |
pass |
| 610 |
c = C() |
| 611 |
assert hash(C.m) == hash(D.m) |
| 612 |
assert hash(c.m) == hash(c.m) |
| 613 |
|
| 614 |
def test_method_repr(): |
| 615 |
class A(object): |
| 616 |
def f(self): |
| 617 |
pass |
| 618 |
assert repr(A().f).startswith("<bound method %s.f of <" % |
| 619 |
A.__qualname__) |
| 620 |
assert repr(A().f).endswith(">>") |
| 621 |
|
| 622 |
def test_method_repr_2(): |
| 623 |
class ClsA(object): |
| 624 |
def f(self): |
| 625 |
pass |
| 626 |
class ClsB(ClsA): |
| 627 |
pass |
| 628 |
r = repr(ClsB().f) |
| 629 |
assert "ClsA.f of <" in r |
| 630 |
assert repr(type(ClsA.f)) == "<class 'function'>" |
| 631 |
assert repr(type(ClsA().f)) == "<class 'method'>" |
| 632 |
|
| 633 |
|
| 634 |
def test_method_call(): |
| 635 |
class C(object): |
| 636 |
def __init__(self, **kw): |
| 637 |
pass |
| 638 |
c = C(type='test') |
| 639 |
|
| 640 |
def test_method_w_callable(): |
| 641 |
class A(object): |
| 642 |
def __call__(self, x): |
| 643 |
return x |
| 644 |
im = MethodType(A(), 3) |
| 645 |
assert im() == 3 |
| 646 |
|
| 647 |
def test_method_w_callable_call_function(): |
| 648 |
class A(object): |
| 649 |
def __call__(self, x, y): |
| 650 |
return x+y |
| 651 |
im = MethodType(A(), 3) |
| 652 |
assert list(map(im, [4])) == [7] |
| 653 |
|
| 654 |
|
| 655 |
class CallableBadGetattr: |
| 656 |
def __getattr__(self, name): |
| 657 |
if name == "__qualname__": |
| 658 |
return self.__getattribute__(name) |
| 659 |
raise RuntimeError |
| 660 |
|
| 661 |
def __call__(self, a, b, c): |
| 662 |
return a, b, c |
| 663 |
|
| 664 |
def test_custom_callable_errors(): |
| 665 |
fn = CallableBadGetattr() |
| 666 |
with raises(TypeError) as excinfo: |
| 667 |
fn(*1) |
| 668 |
assert "argument after * must be an iterable" in excinfo.value.args[0] |
| 669 |
with raises(TypeError) as excinfo: |
| 670 |
fn() |
| 671 |
assert excinfo.value.args[0].startswith('CallableBadGetattr.__call__()') |
| 672 |
assert fn(1, 2, 3) == (1, 2, 3) |
| 673 |
|
| 674 |
def test_invalid_creation(): |
| 675 |
def f(): pass |
| 676 |
with raises(TypeError): |
| 677 |
MethodType(f, None) |
| 678 |
|
| 679 |
def test_empty_arg_kwarg_call(): |
| 680 |
def f(): |
| 681 |
pass |
| 682 |
|
| 683 |
with raises(TypeError): |
| 684 |
f(*0) |
| 685 |
with raises(TypeError): |
| 686 |
f(**0) |
| 687 |
|
| 688 |
def test_method_equal(): |
| 689 |
class A(object): |
| 690 |
def m(self): |
| 691 |
pass |
| 692 |
|
| 693 |
class X(object): |
| 694 |
def __eq__(self, other): |
| 695 |
return True |
| 696 |
|
| 697 |
assert A().m == X() |
| 698 |
assert X() == A().m |
| 699 |
|
| 700 |
def test_method_equals_with_identity(): |
| 701 |
class CallableBadEq(object): |
| 702 |
def __call__(self): |
| 703 |
pass |
| 704 |
def __eq__(self, other): |
| 705 |
raise ZeroDivisionError |
| 706 |
func = CallableBadEq() |
| 707 |
meth = MethodType(func, object) |
| 708 |
assert meth == meth |
| 709 |
assert meth == MethodType(func, object) |
| 710 |
|
| 711 |
def test_method_identity(): |
| 712 |
import sys |
| 713 |
class A(object): |
| 714 |
def m(self): |
| 715 |
pass |
| 716 |
def n(self): |
| 717 |
pass |
| 718 |
|
| 719 |
class B(A): |
| 720 |
pass |
| 721 |
|
| 722 |
class X(object): |
| 723 |
def __eq__(self, other): |
| 724 |
return True |
| 725 |
|
| 726 |
a = A() |
| 727 |
a2 = A() |
| 728 |
x = a.m; y = a.m |
| 729 |
assert x is not y |
| 730 |
assert id(x) != id(y) |
| 731 |
assert x == y |
| 732 |
assert x is not a.n |
| 733 |
assert id(x) != id(a.n) |
| 734 |
assert x is not a2.m |
| 735 |
assert id(x) != id(a2.m) |
| 736 |
|
| 737 |
if '__pypy__' in sys.builtin_module_names: |
| 738 |
assert A.m is A.m |
| 739 |
assert id(A.m) == id(A.m) |
| 740 |
assert A.m == A.m |
| 741 |
x = A.m |
| 742 |
assert x is not A.n |
| 743 |
assert id(x) != id(A.n) |
| 744 |
assert x is B.m |
| 745 |
assert id(x) == id(B.m) |
| 746 |
|
| 747 |
def test_method_ne_NotImplemented(): |
| 748 |
class A(object): |
| 749 |
def __ne__(self, other): |
| 750 |
return "ABC" |
| 751 |
meth = A.__ne__ |
| 752 |
assert meth.__ne__(1) is NotImplemented |
| 753 |
assert (meth != A()) == "ABC" |
| 754 |
|
| 755 |
def test_posonly(): |
| 756 |
def posonlyfunc(a, b, c, /, d): |
| 757 |
return (a, b, c, d) |
| 758 |
|
| 759 |
assert posonlyfunc(1, 2, 3, 4) == (1, 2, 3, 4) |
| 760 |
with raises(TypeError): |
| 761 |
posonlyfunc(a=1, b=2, c=3, d=4) |
| 762 |
|
| 763 |
def test_posonly_default(): |
| 764 |
def posonlyfunc(a, b=(), /, **kwds): |
| 765 |
return a, b, kwds |
| 766 |
assert posonlyfunc(1) == (1, (), {}) |
| 767 |
assert posonlyfunc(1, 2) == (1, 2, {}) |
| 768 |
assert posonlyfunc(1, 2, a=4, b=5) == (1, 2, {'a': 4, 'b': 5}) |
| 769 |
|
| 770 |
def test_posonly_annotations(): |
| 771 |
def posonlyfunc(x: int, /): |
| 772 |
pass |
| 773 |
print(posonlyfunc.__annotations__) |
| 774 |
assert posonlyfunc.__annotations__ == {"x": int} |
| 775 |
|
| 776 |
def global_inner_has_pos_only(): |
| 777 |
def f(x: int, /): ... |
| 778 |
return f |
| 779 |
|
| 780 |
def test_posonly_annotations_crash(): |
| 781 |
assert global_inner_has_pos_only().__annotations__ == {"x": int} |
| 782 |
|
| 783 |
def test_classmethod_of_random_callable(): |
| 784 |
class Callable: |
| 785 |
def __call__(self, cls): |
| 786 |
print(cls) |
| 787 |
assert cls is Class |
| 788 |
return "foo" |
| 789 |
class Class: |
| 790 |
f = classmethod(Callable()) |
| 791 |
assert Class().f() == "foo" |
| 792 |
|
| 793 |
|
| 794 |
def test_classmethod_of_other_descriptor(): |
| 795 |
class BoundWrapper: |
| 796 |
def __init__(self, wrapped): |
| 797 |
self.__wrapped__ = wrapped |
| 798 |
|
| 799 |
def __call__(self, *args, **kwargs): |
| 800 |
return self.__wrapped__(*args, **kwargs) |
| 801 |
|
| 802 |
class Wrapper: |
| 803 |
def __init__(self, wrapped): |
| 804 |
self.__wrapped__ = wrapped |
| 805 |
|
| 806 |
def __get__(self, instance, owner): |
| 807 |
bound_function = self.__wrapped__.__get__(instance, owner) |
| 808 |
return BoundWrapper(bound_function) |
| 809 |
|
| 810 |
def decorator(wrapped): |
| 811 |
return Wrapper(wrapped) |
| 812 |
|
| 813 |
class Class: |
| 814 |
@decorator |
| 815 |
@classmethod |
| 816 |
def inner(cls): |
| 817 |
|
| 818 |
assert cls is Class |
| 819 |
return 'spam' |
| 820 |
|
| 821 |
@classmethod |
| 822 |
@decorator |
| 823 |
def outer(cls): |
| 824 |
|
| 825 |
|
| 826 |
assert cls is Class |
| 827 |
return 'eggs' |
| 828 |
|
| 829 |
assert Class.inner() == 'spam' |
| 830 |
assert Class.outer() == 'eggs' |
| 831 |
assert Class().inner() == 'spam' |
| 832 |
assert Class().outer() == 'eggs' |
| 833 |
|
| 834 |
def test_classmethod_classmethod(): |
| 835 |
class A: |
| 836 |
@classmethod |
| 837 |
@classmethod |
| 838 |
def f(cls): |
| 839 |
return cls |
| 840 |
assert A().f() is A |
| 841 |
|
| 842 |
def test_duplicate_key_kwargs(): |
| 843 |
def f(**d): pass |
| 844 |
class A: |
| 845 |
def keys(self): return ['a', 'a', 'b'] |
| 846 |
def items(self): return [('a', None), ('a', None), ('b', None)] |
| 847 |
def __getitem__(self, key): 1 |
| 848 |
def __len__(self): return 3 |
| 849 |
with pytest.raises(TypeError): |
| 850 |
f(**A()) |
| 851 |
|
| 852 |
def test_unpack_dict_without_length(): |
| 853 |
def f(**d): return d |
| 854 |
class A: |
| 855 |
def keys(self): return ['a'] |
| 856 |
def items(self): return [('a', 1)] |
| 857 |
def __getitem__(self, key): return 1 |
| 858 |
assert f(**A()) == {'a': 1} |
| 859 |
assert f(b=2, **A()) == {'b': 2, 'a': 1} |
| 860 |
assert f(**A(), b=2) == {'b': 2, 'a': 1} |
| 861 |
|
| 862 |
def test_co_lines(): |
| 863 |
def f(abc): |
| 864 |
defghi = (abc + |
| 865 |
12 * |
| 866 |
abc) |
| 867 |
try: |
| 868 |
1 / abc |
| 869 |
except ZeroDivisionError: |
| 870 |
return 12 |
| 871 |
start = 0 |
| 872 |
for entry in f.__code__.co_lines(): |
| 873 |
assert entry[0] == start |
| 874 |
start = entry[1] |
| 875 |
assert isinstance(entry[2], int) or entry[2] is None |
| 876 |
assert entry[1] == len(f.__code__.co_code) |
| 877 |
|
| 878 |
|
| 879 |
res = [end - f.__code__.co_firstlineno |
| 880 |
for start, stop, end in f.__code__.co_lines() |
| 881 |
if end is not None] |
| 882 |
|
| 883 |
assert res == [1, 2, 3, 2, 1, 4, 5, 6, 7, 6] |
| 884 |
|
| 885 |
def test_co_lines_off_by_one(): |
| 886 |
def f(x): |
| 887 |
if x: |
| 888 |
pass |
| 889 |
else: |
| 890 |
return 2 |
| 891 |
return x |
| 892 |
c = f.__code__.replace(co_firstlineno=1) |
| 893 |
assert list(c.co_lines()) == [(0, 4, 2), (4, 6, 3), (6, 10, 5), (10, 14, 6)] |
| 894 |
|
| 895 |
def test_co_lines_expr_parens(): |
| 896 |
def expr_with_parens(x): |
| 897 |
( |
| 898 |
1 |
| 899 |
/ |
| 900 |
x |
| 901 |
) |
| 902 |
c = expr_with_parens.__code__.replace(co_firstlineno=1) |
| 903 |
assert [x[2] for x in c.co_lines()] == [3, 5, 3] |
| 904 |
|
| 905 |
def test_qualname_in_error(): |
| 906 |
class A: |
| 907 |
def f(self, x): |
| 908 |
return x |
| 909 |
|
| 910 |
with raises(TypeError) as info: |
| 911 |
A().f() |
| 912 |
assert "A.f" in str(info.value) |
| 913 |
|
| 914 |
def test_builtin_function_in_method_objclass(): |
| 915 |
assert list.append.__qualname__ == 'list.append' |
| 916 |
|
| 917 |
def test_classmethod_not_callable(): |
| 918 |
with raises(TypeError): |
| 919 |
classmethod(lambda x: x)(1) |
| 920 |
|
| 921 |
def test_builtin_functions___self__(): |
| 922 |
import builtins, math |
| 923 |
assert abs.__self__ is builtins |
| 924 |
|
| 925 |
assert math.isinf.__self__ is math |
| 926 |
|
| 927 |
def test_method_doc(): |
| 928 |
class X: |
| 929 |
@classmethod |
| 930 |
def cm(cls, x): |
| 931 |
'''A class method''' |
| 932 |
... |
| 933 |
|
| 934 |
doc1 = object.__getattribute__(X.cm, '__doc__') |
| 935 |
doc2 = X.cm.__doc__ |
| 936 |
assert doc1 == doc2 == 'A class method' |
| 937 |
assert isinstance(MethodType.__doc__, str) |
| 938 |
|
| 939 |
def test_method_getattr(): |
| 940 |
class A: |
| 941 |
def f(self): pass |
| 942 |
assert A().f.__code__ is A.f.__code__ |
| 943 |
|
| 944 |
def test_dict_merge_errors_funcname(): |
| 945 |
def f(): pass |
| 946 |
with pytest.raises(TypeError) as info: |
| 947 |
f(a=1, **{'a': 1}) |
| 948 |
assert ("f() got multiple values for keyword argument 'a'" in |
| 949 |
str(info.value)) |
| 950 |
with pytest.raises(TypeError) as info: |
| 951 |
f(a=1, **12) |
| 952 |
assert ("f() argument after ** must be a mapping, not int" in |
| 953 |
str(info.value)) |
| 954 |
with pytest.raises(TypeError) as info: |
| 955 |
f(a=1, **{'b': 1}, **12) |
| 956 |
assert ("f() argument after ** must be a mapping, not int" in |
| 957 |
str(info.value)) |
| 958 |
|
| 959 |
def test_argument_parse_errors_use_qualname(): |
| 960 |
with pytest.raises(TypeError) as info: |
| 961 |
set.__init__() |
| 962 |
assert 'set' in str(info.value) |
| 963 |
assert '__init__' in str(info.value) |
| 964 |
|
| 965 |
|
| 966 |
def test_text_signature_unset_raises(): |
| 967 |
def a(): pass |
| 968 |
raises(AttributeError, getattr, a, '__text_signature__') |
| 969 |
|
| 970 |
def test_text_signature_set_and_clear(): |
| 971 |
def a(): pass |
| 972 |
a.__text_signature__ = '($self, /)' |
| 973 |
assert a.__text_signature__ == '($self, /)' |
| 974 |
a.__text_signature__ = None |
| 975 |
assert a.__text_signature__ is None |
| 976 |
|
| 977 |
def test_function_type_text_signature(): |
| 978 |
assert type(lambda: None).__text_signature__ == \ |
| 979 |
'(code, globals, name=None, argdefs=None, closure=None)' |
| 980 |
|