Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

1871ed0f-2bf4-4eae-9d52-ee743678773f 1.9 KB
Raw

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  1. from __future__ import annotations
  2. import inspect
  3. import os
  4. import typing as t
  5. import warnings
  6. def warn(msg: str, category: t.Any, *, stacklevel: int, source: t.Any = None) -> None:
  7. """Like warnings.warn(), but category and stacklevel are required.
  8. You pretty much never want the default stacklevel of 1, so this helps
  9. encourage setting it explicitly."""
  10. warnings.warn(msg, category=category, stacklevel=stacklevel, source=source)
  11. def deprecated_method(method: t.Any, cls: t.Any, method_name: str, msg: str) -> None:
  12. """Show deprecation warning about a magic method definition.
  13. Uses warn_explicit to bind warning to method definition instead of triggering code,
  14. which isn't relevant.
  15. """
  16. warn_msg = f"{cls.__name__}.{method_name} is deprecated in traitlets 4.1: {msg}"
  17. for parent in inspect.getmro(cls):
  18. if method_name in parent.__dict__:
  19. cls = parent
  20. break
  21. # limit deprecation messages to once per package
  22. package_name = cls.__module__.split(".", 1)[0]
  23. key = (package_name, msg)
  24. if not should_warn(key):
  25. return
  26. try:
  27. fname = inspect.getsourcefile(method) or "<unknown>"
  28. lineno = inspect.getsourcelines(method)[1] or 0
  29. except (OSError, TypeError) as e:
  30. # Failed to inspect for some reason
  31. warn(
  32. warn_msg + ("\n(inspection failed) %s" % e),
  33. DeprecationWarning,
  34. stacklevel=2,
  35. )
  36. else:
  37. warnings.warn_explicit(warn_msg, DeprecationWarning, fname, lineno)
  38. _deprecations_shown = set()
  39. def should_warn(key: t.Any) -> bool:
  40. """Add our own checks for too many deprecation warnings.
  41. Limit to once per package.
  42. """
  43. env_flag = os.environ.get("TRAITLETS_ALL_DEPRECATIONS")
  44. if env_flag and env_flag != "0":
  45. return True
  46. if key not in _deprecations_shown:
  47. _deprecations_shown.add(key)
  48. return True
  49. else:
  50. return False
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...