sharedparse.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from __future__ import annotations
  2. from typing import Final
  3. """Shared logic between our three mypy parser files."""
  4. _NON_BINARY_MAGIC_METHODS: Final = {
  5. "__abs__",
  6. "__call__",
  7. "__complex__",
  8. "__contains__",
  9. "__del__",
  10. "__delattr__",
  11. "__delitem__",
  12. "__enter__",
  13. "__exit__",
  14. "__float__",
  15. "__getattr__",
  16. "__getattribute__",
  17. "__getitem__",
  18. "__hex__",
  19. "__init__",
  20. "__init_subclass__",
  21. "__int__",
  22. "__invert__",
  23. "__iter__",
  24. "__len__",
  25. "__long__",
  26. "__neg__",
  27. "__new__",
  28. "__oct__",
  29. "__pos__",
  30. "__repr__",
  31. "__reversed__",
  32. "__setattr__",
  33. "__setitem__",
  34. "__str__",
  35. }
  36. MAGIC_METHODS_ALLOWING_KWARGS: Final = {
  37. "__init__",
  38. "__init_subclass__",
  39. "__new__",
  40. "__call__",
  41. "__setattr__",
  42. }
  43. BINARY_MAGIC_METHODS: Final = {
  44. "__add__",
  45. "__and__",
  46. "__divmod__",
  47. "__eq__",
  48. "__floordiv__",
  49. "__ge__",
  50. "__gt__",
  51. "__iadd__",
  52. "__iand__",
  53. "__idiv__",
  54. "__ifloordiv__",
  55. "__ilshift__",
  56. "__imatmul__",
  57. "__imod__",
  58. "__imul__",
  59. "__ior__",
  60. "__ipow__",
  61. "__irshift__",
  62. "__isub__",
  63. "__itruediv__",
  64. "__ixor__",
  65. "__le__",
  66. "__lshift__",
  67. "__lt__",
  68. "__matmul__",
  69. "__mod__",
  70. "__mul__",
  71. "__ne__",
  72. "__or__",
  73. "__pow__",
  74. "__radd__",
  75. "__rand__",
  76. "__rdiv__",
  77. "__rfloordiv__",
  78. "__rlshift__",
  79. "__rmatmul__",
  80. "__rmod__",
  81. "__rmul__",
  82. "__ror__",
  83. "__rpow__",
  84. "__rrshift__",
  85. "__rshift__",
  86. "__rsub__",
  87. "__rtruediv__",
  88. "__rxor__",
  89. "__sub__",
  90. "__truediv__",
  91. "__xor__",
  92. }
  93. assert not (_NON_BINARY_MAGIC_METHODS & BINARY_MAGIC_METHODS)
  94. MAGIC_METHODS: Final = _NON_BINARY_MAGIC_METHODS | BINARY_MAGIC_METHODS
  95. MAGIC_METHODS_POS_ARGS_ONLY: Final = MAGIC_METHODS - MAGIC_METHODS_ALLOWING_KWARGS
  96. def special_function_elide_names(name: str) -> bool:
  97. return name in MAGIC_METHODS_POS_ARGS_ONLY
  98. def argument_elide_name(name: str | None) -> bool:
  99. return name is not None and name.startswith("__") and not name.endswith("__")