Python etc / unittest.mock.sentinel

Published: 20 October 2020, 18:00

Some functions can accept as an argument value of any type or no value at all. If you set the default value to None you can’t say if None was explicitly passed or not. For example, the default value for argparse.ArgumentParser.add_argument. For this purpose, you can create a new object and then use is check:

DEFAULT = object()

def f(arg=DEFAULT):
  if arg is DEFAULT:
      return 'no value passed'
  return f'passed {arg}'

f()     # 'no value passed'
f(None) # 'passed None'
f(1)    # 'passed 1'
f(object()) # 'passed <object object at ...>'

The module unittest.mock provides a sentinel registry to create unique (by name) objects for the testing purpose:

sentinel.ab.name # 'ab'
sentinel.ab is sentinel.ab  # True
sentinel.ab is sentinel.cd  # False