◐ Shell
clean mode source ↗

bpo-31803: time.clock() now emits a DeprecationWarning by vstinner · Pull Request #4020 · python/cpython

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment, otherwise we can forgot why this was added.

But I prefer moving all the code from the loop body to a local function and calling it multiple times.

def test(name):
    ...
test('perf_counter')
test('process_time')
test('time')
if hasattr(time, 'monotonic'):
    test('monotonic')
with self.assertWarns(DeprecationWarning):
    test('clock')

This silences a warning only for clock, tests that a warning for clock is raised, and in case of test failure the traceback contains a line with a timer's name.