util.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. """Various utilities that don't depend on other modules in mypyc.irbuild."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from mypy.nodes import (
  5. ARG_NAMED,
  6. ARG_NAMED_OPT,
  7. ARG_OPT,
  8. ARG_POS,
  9. GDEF,
  10. ArgKind,
  11. BytesExpr,
  12. CallExpr,
  13. ClassDef,
  14. Decorator,
  15. Expression,
  16. FloatExpr,
  17. FuncDef,
  18. IntExpr,
  19. NameExpr,
  20. OverloadedFuncDef,
  21. RefExpr,
  22. StrExpr,
  23. TupleExpr,
  24. UnaryExpr,
  25. Var,
  26. )
  27. DATACLASS_DECORATORS = {"dataclasses.dataclass", "attr.s", "attr.attrs"}
  28. def is_trait_decorator(d: Expression) -> bool:
  29. return isinstance(d, RefExpr) and d.fullname == "mypy_extensions.trait"
  30. def is_trait(cdef: ClassDef) -> bool:
  31. return any(is_trait_decorator(d) for d in cdef.decorators) or cdef.info.is_protocol
  32. def dataclass_decorator_type(d: Expression) -> str | None:
  33. if isinstance(d, RefExpr) and d.fullname in DATACLASS_DECORATORS:
  34. return d.fullname.split(".")[0]
  35. elif (
  36. isinstance(d, CallExpr)
  37. and isinstance(d.callee, RefExpr)
  38. and d.callee.fullname in DATACLASS_DECORATORS
  39. ):
  40. name = d.callee.fullname.split(".")[0]
  41. if name == "attr" and "auto_attribs" in d.arg_names:
  42. # Note: the mypy attrs plugin checks that the value of auto_attribs is
  43. # not computed at runtime, so we don't need to perform that check here
  44. auto = d.args[d.arg_names.index("auto_attribs")]
  45. if isinstance(auto, NameExpr) and auto.name == "True":
  46. return "attr-auto"
  47. return name
  48. else:
  49. return None
  50. def is_dataclass_decorator(d: Expression) -> bool:
  51. return dataclass_decorator_type(d) is not None
  52. def is_dataclass(cdef: ClassDef) -> bool:
  53. return any(is_dataclass_decorator(d) for d in cdef.decorators)
  54. def dataclass_type(cdef: ClassDef) -> str | None:
  55. for d in cdef.decorators:
  56. typ = dataclass_decorator_type(d)
  57. if typ is not None:
  58. return typ
  59. return None
  60. def get_mypyc_attr_literal(e: Expression) -> Any:
  61. """Convert an expression from a mypyc_attr decorator to a value.
  62. Supports a pretty limited range."""
  63. if isinstance(e, (StrExpr, IntExpr, FloatExpr)):
  64. return e.value
  65. elif isinstance(e, RefExpr) and e.fullname == "builtins.True":
  66. return True
  67. elif isinstance(e, RefExpr) and e.fullname == "builtins.False":
  68. return False
  69. elif isinstance(e, RefExpr) and e.fullname == "builtins.None":
  70. return None
  71. return NotImplemented
  72. def get_mypyc_attr_call(d: Expression) -> CallExpr | None:
  73. """Check if an expression is a call to mypyc_attr and return it if so."""
  74. if (
  75. isinstance(d, CallExpr)
  76. and isinstance(d.callee, RefExpr)
  77. and d.callee.fullname == "mypy_extensions.mypyc_attr"
  78. ):
  79. return d
  80. return None
  81. def get_mypyc_attrs(stmt: ClassDef | Decorator) -> dict[str, Any]:
  82. """Collect all the mypyc_attr attributes on a class definition or a function."""
  83. attrs: dict[str, Any] = {}
  84. for dec in stmt.decorators:
  85. d = get_mypyc_attr_call(dec)
  86. if d:
  87. for name, arg in zip(d.arg_names, d.args):
  88. if name is None:
  89. if isinstance(arg, StrExpr):
  90. attrs[arg.value] = True
  91. else:
  92. attrs[name] = get_mypyc_attr_literal(arg)
  93. return attrs
  94. def is_extension_class(cdef: ClassDef) -> bool:
  95. if any(
  96. not is_trait_decorator(d) and not is_dataclass_decorator(d) and not get_mypyc_attr_call(d)
  97. for d in cdef.decorators
  98. ):
  99. return False
  100. if cdef.info.typeddict_type:
  101. return False
  102. if cdef.info.is_named_tuple:
  103. return False
  104. if cdef.info.metaclass_type and cdef.info.metaclass_type.type.fullname not in (
  105. "abc.ABCMeta",
  106. "typing.TypingMeta",
  107. "typing.GenericMeta",
  108. ):
  109. return False
  110. return True
  111. def get_func_def(op: FuncDef | Decorator | OverloadedFuncDef) -> FuncDef:
  112. if isinstance(op, OverloadedFuncDef):
  113. assert op.impl
  114. op = op.impl
  115. if isinstance(op, Decorator):
  116. op = op.func
  117. return op
  118. def concrete_arg_kind(kind: ArgKind) -> ArgKind:
  119. """Find the concrete version of an arg kind that is being passed."""
  120. if kind == ARG_OPT:
  121. return ARG_POS
  122. elif kind == ARG_NAMED_OPT:
  123. return ARG_NAMED
  124. else:
  125. return kind
  126. def is_constant(e: Expression) -> bool:
  127. """Check whether we allow an expression to appear as a default value.
  128. We don't currently properly support storing the evaluated
  129. values for default arguments and default attribute values, so
  130. we restrict what expressions we allow. We allow literals of
  131. primitives types, None, and references to Final global
  132. variables.
  133. """
  134. return (
  135. isinstance(e, (StrExpr, BytesExpr, IntExpr, FloatExpr))
  136. or (isinstance(e, UnaryExpr) and e.op == "-" and isinstance(e.expr, (IntExpr, FloatExpr)))
  137. or (isinstance(e, TupleExpr) and all(is_constant(e) for e in e.items))
  138. or (
  139. isinstance(e, RefExpr)
  140. and e.kind == GDEF
  141. and (
  142. e.fullname in ("builtins.True", "builtins.False", "builtins.None")
  143. or (isinstance(e.node, Var) and e.node.is_final)
  144. )
  145. )
  146. )
  147. def bytes_from_str(value: str) -> bytes:
  148. """Convert a string representing bytes into actual bytes.
  149. This is needed because the literal characters of BytesExpr (the
  150. characters inside b'') are stored in BytesExpr.value, whose type is
  151. 'str' not 'bytes'.
  152. """
  153. return bytes(value, "utf8").decode("unicode-escape").encode("raw-unicode-escape")