diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-04-01 06:16:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-01 06:16:35 (GMT) |
commit | 42a139ed88c487f325a241c6ee8b308b3c045975 (patch) | |
tree | de56527188eff240b06496c3451e525af959dcad /Lib/test | |
parent | 5f2c50810a67982b0c80f6d3258fee3647f67005 (diff) | |
download | cpython-42a139ed88c487f325a241c6ee8b308b3c045975.zip cpython-42a139ed88c487f325a241c6ee8b308b3c045975.tar.gz cpython-42a139ed88c487f325a241c6ee8b308b3c045975.tar.bz2 |
bpo-36492: Deprecate passing some arguments as keyword arguments. (GH-12637)
Deprecated passing the following arguments as keyword arguments:
- "func" in functools.partialmethod(), weakref.finalize(),
profile.Profile.runcall(), cProfile.Profile.runcall(),
bdb.Bdb.runcall(), trace.Trace.runfunc() and
curses.wrapper().
- "function" in unittest.addModuleCleanup() and
unittest.TestCase.addCleanup().
- "fn" in the submit() method of concurrent.futures.ThreadPoolExecutor
and concurrent.futures.ProcessPoolExecutor.
- "callback" in contextlib.ExitStack.callback(),
contextlib.AsyncExitStack.callback() and
contextlib.AsyncExitStack.push_async_callback().
- "c" and "typeid" in the create() method of multiprocessing.managers.Server
and multiprocessing.managers.SharedMemoryServer.
- "obj" in weakref.finalize().
Also allowed to pass arbitrary keyword arguments (even "self" and "func")
if the above arguments are passed as positional argument.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_concurrent_futures.py | 10 | ||||
-rw-r--r-- | Lib/test/test_contextlib.py | 11 | ||||
-rw-r--r-- | Lib/test/test_contextlib_async.py | 10 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 11 | ||||
-rw-r--r-- | Lib/test/test_trace.py | 12 | ||||
-rw-r--r-- | Lib/test/test_weakref.py | 29 |
6 files changed, 83 insertions, 0 deletions
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index 01125c7..903afbd 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -49,6 +49,9 @@ INITIALIZER_STATUS = 'uninitialized' def mul(x, y): return x * y +def capture(*args, **kwargs): + return args, kwargs + def sleep_and_raise(t): time.sleep(t) raise Exception('this is an exception') @@ -658,6 +661,13 @@ class ExecutorTest: def test_submit_keyword(self): future = self.executor.submit(mul, 2, y=8) self.assertEqual(16, future.result()) + future = self.executor.submit(capture, 1, self=2, fn=3) + self.assertEqual(future.result(), ((1,), {'self': 2, 'fn': 3})) + with self.assertWarns(DeprecationWarning): + future = self.executor.submit(fn=capture, arg=1) + self.assertEqual(future.result(), ((), {'arg': 1})) + with self.assertRaises(TypeError): + self.executor.submit(arg=1) def test_map(self): self.assertEqual( diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 755d9b9..188a29d 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -574,6 +574,7 @@ class TestBaseExitStack: ((), dict(example=1)), ((1,), dict(example=1)), ((1,2), dict(example=1)), + ((1,2), dict(self=3, callback=4)), ] result = [] def _exit(*args, **kwds): @@ -596,6 +597,16 @@ class TestBaseExitStack: self.assertIsNone(wrapper[1].__doc__, _exit.__doc__) self.assertEqual(result, expected) + result = [] + with self.exit_stack() as stack: + with self.assertRaises(TypeError): + stack.callback(arg=1) + with self.assertRaises(TypeError): + self.exit_stack.callback(arg=2) + with self.assertWarns(DeprecationWarning): + stack.callback(callback=_exit, arg=3) + self.assertEqual(result, [((), {'arg': 3})]) + def test_push(self): exc_raised = ZeroDivisionError def _expect_exc(exc_type, exc, exc_tb): diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index 57716ae..492b226 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -352,6 +352,16 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase): self.assertEqual(result, expected) + result = [] + async with AsyncExitStack() as stack: + with self.assertRaises(TypeError): + stack.push_async_callback(arg=1) + with self.assertRaises(TypeError): + self.exit_stack.push_async_callback(arg=2) + with self.assertWarns(DeprecationWarning): + stack.push_async_callback(callback=_exit, arg=3) + self.assertEqual(result, [((), {'arg': 3})]) + @_async_test async def test_async_push(self): exc_raised = ZeroDivisionError diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 63a9ade..4b2b9ab 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -464,6 +464,7 @@ class TestPartialMethod(unittest.TestCase): positional = functools.partialmethod(capture, 1) keywords = functools.partialmethod(capture, a=2) both = functools.partialmethod(capture, 3, b=4) + spec_keywords = functools.partialmethod(capture, self=1, func=2) nested = functools.partialmethod(positional, 5) @@ -497,6 +498,8 @@ class TestPartialMethod(unittest.TestCase): self.assertEqual(self.A.both(self.a, 5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) + self.assertEqual(self.a.spec_keywords(), ((self.a,), {'self': 1, 'func': 2})) + def test_nested(self): self.assertEqual(self.a.nested(), ((self.a, 1, 5), {})) self.assertEqual(self.a.nested(6), ((self.a, 1, 5, 6), {})) @@ -550,6 +553,14 @@ class TestPartialMethod(unittest.TestCase): with self.assertRaises(TypeError): class B(object): method = functools.partialmethod(None, 1) + with self.assertRaises(TypeError): + class B: + method = functools.partialmethod() + with self.assertWarns(DeprecationWarning): + class B: + method = functools.partialmethod(func=capture, a=1) + b = B() + self.assertEqual(b.method(2, x=3), ((b, 2), {'a': 1, 'x': 3})) def test_repr(self): self.assertEqual(repr(vars(self.A)['both']), diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 5c333b7..afe7902 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -70,6 +70,9 @@ def traced_func_calling_generator(): def traced_doubler(num): return num * 2 +def traced_capturer(*args, **kwargs): + return args, kwargs + def traced_caller_list_comprehension(): k = 10 mylist = [traced_doubler(i) for i in range(k)] @@ -270,6 +273,15 @@ class TestFuncs(unittest.TestCase): } self.assertEqual(self.tracer.results().calledfuncs, expected) + def test_arg_errors(self): + res = self.tracer.runfunc(traced_capturer, 1, 2, self=3, func=4) + self.assertEqual(res, ((1, 2), {'self': 3, 'func': 4})) + with self.assertWarns(DeprecationWarning): + res = self.tracer.runfunc(func=traced_capturer, arg=1) + self.assertEqual(res, ((), {'arg': 1})) + with self.assertRaises(TypeError): + self.tracer.runfunc() + def test_loop_caller_importing(self): self.tracer.runfunc(traced_func_importing_caller, 1) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 1fac08d..50a46f8 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1839,6 +1839,35 @@ class FinalizeTestCase(unittest.TestCase): self.assertEqual(f.alive, False) self.assertEqual(res, [199]) + def test_arg_errors(self): + def fin(*args, **kwargs): + res.append((args, kwargs)) + + a = self.A() + + res = [] + f = weakref.finalize(a, fin, 1, 2, func=3, obj=4) + self.assertEqual(f.peek(), (a, fin, (1, 2), {'func': 3, 'obj': 4})) + f() + self.assertEqual(res, [((1, 2), {'func': 3, 'obj': 4})]) + + res = [] + with self.assertWarns(DeprecationWarning): + f = weakref.finalize(a, func=fin, arg=1) + self.assertEqual(f.peek(), (a, fin, (), {'arg': 1})) + f() + self.assertEqual(res, [((), {'arg': 1})]) + + res = [] + with self.assertWarns(DeprecationWarning): + f = weakref.finalize(obj=a, func=fin, arg=1) + self.assertEqual(f.peek(), (a, fin, (), {'arg': 1})) + f() + self.assertEqual(res, [((), {'arg': 1})]) + + self.assertRaises(TypeError, weakref.finalize, a) + self.assertRaises(TypeError, weakref.finalize) + def test_order(self): a = self.A() res = [] |