You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

deprecation.py 1.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import importlib
  9. import warnings
  10. from deprecated.sphinx import deprecated
  11. def deprecated_func(version, origin, name, tbd):
  12. r"""
  13. Args:
  14. version: version to deprecate this function
  15. origin: origin module path
  16. name: function name
  17. tbd: to be discussed, if true, ignore warnings
  18. """
  19. should_warning = not tbd
  20. def wrapper(*args, **kwargs):
  21. nonlocal should_warning
  22. module = importlib.import_module(origin)
  23. func = module.__getattribute__(name)
  24. if should_warning:
  25. with warnings.catch_warnings():
  26. warnings.simplefilter(action="always")
  27. warnings.warn(
  28. "Call to deprecated function {}. (use {}.{} instead) -- Deprecated since version {}.".format(
  29. name, origin, name, version
  30. ),
  31. category=DeprecationWarning,
  32. stacklevel=2,
  33. )
  34. should_warning = False
  35. return func(*args, **kwargs)
  36. return wrapper