renaming.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding=utf-8
  2. #
  3. # This file is part of Hypothesis, which may be found at
  4. # https://github.com/HypothesisWorks/hypothesis-python
  5. #
  6. # Most of this work is copyright (C) 2013-2018 David R. MacIver
  7. # (david@drmaciver.com), but it contains contributions by others. See
  8. # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9. # consult the git log if you need to determine who owns an individual
  10. # contribution.
  11. #
  12. # This Source Code Form is subject to the terms of the Mozilla Public License,
  13. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14. # obtain one at http://mozilla.org/MPL/2.0/.
  15. #
  16. # END HEADER
  17. from __future__ import division, print_function, absolute_import
  18. from hypothesis._settings import note_deprecation
  19. from hypothesis.internal.reflection import proxies
  20. def renamed_arguments(**rename_mapping):
  21. """Helper function for deprecating arguments that have been renamed to a
  22. new form."""
  23. assert len(set(rename_mapping.values())) == len(rename_mapping)
  24. def accept(f):
  25. @proxies(f)
  26. def with_name_check(**kwargs):
  27. for k, v in list(kwargs.items()):
  28. if k in rename_mapping and v is not None:
  29. t = rename_mapping[k]
  30. note_deprecation((
  31. 'The argument %s has been renamed to %s. The old '
  32. 'name will go away in a future version of '
  33. 'Hypothesis.') % (k, t))
  34. kwargs[t] = kwargs.pop(k)
  35. return f(**kwargs)
  36. # This decorates things in the public API, which all have docstrings.
  37. # (If they're not in the public API, we don't need a deprecation path.)
  38. # But docstrings are stripped when running with PYTHONOPTIMIZE=2.
  39. #
  40. # If somebody's running with that flag, they don't expect any
  41. # docstrings to be present, so this message isn't useful. Absence of
  42. # a docstring is a strong indicator that they're running in this mode,
  43. # so skip adding this message if that's the case.
  44. if with_name_check.__doc__ is not None:
  45. with_name_check.__doc__ += '\n'.join((
  46. '', '',
  47. 'The following arguments have been renamed:',
  48. '',
  49. ) + tuple(
  50. ' * %s has been renamed to %s' % s
  51. for s in rename_mapping.items()
  52. ) + (
  53. '',
  54. 'Use of the old names has been deprecated and will be removed',
  55. 'in a future version of Hypothesis.'
  56. )
  57. )
  58. return with_name_check
  59. return accept