enums.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. This file contains a variety of plugins for refining how mypy infers types of
  3. expressions involving Enums.
  4. Currently, this file focuses on providing better inference for expressions like
  5. 'SomeEnum.FOO.name' and 'SomeEnum.FOO.value'. Note that the type of both expressions
  6. will vary depending on exactly which instance of SomeEnum we're looking at.
  7. Note that this file does *not* contain all special-cased logic related to enums:
  8. we actually bake some of it directly in to the semantic analysis layer (see
  9. semanal_enum.py).
  10. """
  11. from __future__ import annotations
  12. from typing import Final, Iterable, Sequence, TypeVar, cast
  13. import mypy.plugin # To avoid circular imports.
  14. from mypy.nodes import TypeInfo
  15. from mypy.semanal_enum import ENUM_BASES
  16. from mypy.subtypes import is_equivalent
  17. from mypy.typeops import fixup_partial_type, make_simplified_union
  18. from mypy.types import CallableType, Instance, LiteralType, ProperType, Type, get_proper_type
  19. ENUM_NAME_ACCESS: Final = {f"{prefix}.name" for prefix in ENUM_BASES} | {
  20. f"{prefix}._name_" for prefix in ENUM_BASES
  21. }
  22. ENUM_VALUE_ACCESS: Final = {f"{prefix}.value" for prefix in ENUM_BASES} | {
  23. f"{prefix}._value_" for prefix in ENUM_BASES
  24. }
  25. def enum_name_callback(ctx: mypy.plugin.AttributeContext) -> Type:
  26. """This plugin refines the 'name' attribute in enums to act as if
  27. they were declared to be final.
  28. For example, the expression 'MyEnum.FOO.name' normally is inferred
  29. to be of type 'str'.
  30. This plugin will instead make the inferred type be a 'str' where the
  31. last known value is 'Literal["FOO"]'. This means it would be legal to
  32. use 'MyEnum.FOO.name' in contexts that expect a Literal type, just like
  33. any other Final variable or attribute.
  34. This plugin assumes that the provided context is an attribute access
  35. matching one of the strings found in 'ENUM_NAME_ACCESS'.
  36. """
  37. enum_field_name = _extract_underlying_field_name(ctx.type)
  38. if enum_field_name is None:
  39. return ctx.default_attr_type
  40. else:
  41. str_type = ctx.api.named_generic_type("builtins.str", [])
  42. literal_type = LiteralType(enum_field_name, fallback=str_type)
  43. return str_type.copy_modified(last_known_value=literal_type)
  44. _T = TypeVar("_T")
  45. def _first(it: Iterable[_T]) -> _T | None:
  46. """Return the first value from any iterable.
  47. Returns ``None`` if the iterable is empty.
  48. """
  49. for val in it:
  50. return val
  51. return None
  52. def _infer_value_type_with_auto_fallback(
  53. ctx: mypy.plugin.AttributeContext, proper_type: ProperType | None
  54. ) -> Type | None:
  55. """Figure out the type of an enum value accounting for `auto()`.
  56. This method is a no-op for a `None` proper_type and also in the case where
  57. the type is not "enum.auto"
  58. """
  59. if proper_type is None:
  60. return None
  61. proper_type = get_proper_type(fixup_partial_type(proper_type))
  62. if not (isinstance(proper_type, Instance) and proper_type.type.fullname == "enum.auto"):
  63. return proper_type
  64. assert isinstance(ctx.type, Instance), "An incorrect ctx.type was passed."
  65. info = ctx.type.type
  66. # Find the first _generate_next_value_ on the mro. We need to know
  67. # if it is `Enum` because `Enum` types say that the return-value of
  68. # `_generate_next_value_` is `Any`. In reality the default `auto()`
  69. # returns an `int` (presumably the `Any` in typeshed is to make it
  70. # easier to subclass and change the returned type).
  71. type_with_gnv = _first(ti for ti in info.mro if ti.names.get("_generate_next_value_"))
  72. if type_with_gnv is None:
  73. return ctx.default_attr_type
  74. stnode = type_with_gnv.names["_generate_next_value_"]
  75. # This should be a `CallableType`
  76. node_type = get_proper_type(stnode.type)
  77. if isinstance(node_type, CallableType):
  78. if type_with_gnv.fullname == "enum.Enum":
  79. int_type = ctx.api.named_generic_type("builtins.int", [])
  80. return int_type
  81. return get_proper_type(node_type.ret_type)
  82. return ctx.default_attr_type
  83. def _implements_new(info: TypeInfo) -> bool:
  84. """Check whether __new__ comes from enum.Enum or was implemented in a
  85. subclass. In the latter case, we must infer Any as long as mypy can't infer
  86. the type of _value_ from assignments in __new__.
  87. """
  88. type_with_new = _first(
  89. ti
  90. for ti in info.mro
  91. if ti.names.get("__new__") and not ti.fullname.startswith("builtins.")
  92. )
  93. if type_with_new is None:
  94. return False
  95. return type_with_new.fullname not in ("enum.Enum", "enum.IntEnum", "enum.StrEnum")
  96. def enum_value_callback(ctx: mypy.plugin.AttributeContext) -> Type:
  97. """This plugin refines the 'value' attribute in enums to refer to
  98. the original underlying value. For example, suppose we have the
  99. following:
  100. class SomeEnum:
  101. FOO = A()
  102. BAR = B()
  103. By default, mypy will infer that 'SomeEnum.FOO.value' and
  104. 'SomeEnum.BAR.value' both are of type 'Any'. This plugin refines
  105. this inference so that mypy understands the expressions are
  106. actually of types 'A' and 'B' respectively. This better reflects
  107. the actual runtime behavior.
  108. This plugin works simply by looking up the original value assigned
  109. to the enum. For example, when this plugin sees 'SomeEnum.BAR.value',
  110. it will look up whatever type 'BAR' had in the SomeEnum TypeInfo and
  111. use that as the inferred type of the overall expression.
  112. This plugin assumes that the provided context is an attribute access
  113. matching one of the strings found in 'ENUM_VALUE_ACCESS'.
  114. """
  115. enum_field_name = _extract_underlying_field_name(ctx.type)
  116. if enum_field_name is None:
  117. # We do not know the enum field name (perhaps it was passed to a
  118. # function and we only know that it _is_ a member). All is not lost
  119. # however, if we can prove that the all of the enum members have the
  120. # same value-type, then it doesn't matter which member was passed in.
  121. # The value-type is still known.
  122. if isinstance(ctx.type, Instance):
  123. info = ctx.type.type
  124. # As long as mypy doesn't understand attribute creation in __new__,
  125. # there is no way to predict the value type if the enum class has a
  126. # custom implementation
  127. if _implements_new(info):
  128. return ctx.default_attr_type
  129. stnodes = (info.get(name) for name in info.names)
  130. # Enums _can_ have methods and instance attributes.
  131. # Omit methods and attributes created by assigning to self.*
  132. # for our value inference.
  133. node_types = (
  134. get_proper_type(n.type) if n else None
  135. for n in stnodes
  136. if n is None or not n.implicit
  137. )
  138. proper_types = list(
  139. _infer_value_type_with_auto_fallback(ctx, t)
  140. for t in node_types
  141. if t is None or not isinstance(t, CallableType)
  142. )
  143. underlying_type = _first(proper_types)
  144. if underlying_type is None:
  145. return ctx.default_attr_type
  146. # At first we try to predict future `value` type if all other items
  147. # have the same type. For example, `int`.
  148. # If this is the case, we simply return this type.
  149. # See https://github.com/python/mypy/pull/9443
  150. all_same_value_type = all(
  151. proper_type is not None and proper_type == underlying_type
  152. for proper_type in proper_types
  153. )
  154. if all_same_value_type:
  155. if underlying_type is not None:
  156. return underlying_type
  157. # But, after we started treating all `Enum` values as `Final`,
  158. # we start to infer types in
  159. # `item = 1` as `Literal[1]`, not just `int`.
  160. # So, for example types in this `Enum` will all be different:
  161. #
  162. # class Ordering(IntEnum):
  163. # one = 1
  164. # two = 2
  165. # three = 3
  166. #
  167. # We will infer three `Literal` types here.
  168. # They are not the same, but they are equivalent.
  169. # So, we unify them to make sure `.value` prediction still works.
  170. # Result will be `Literal[1] | Literal[2] | Literal[3]` for this case.
  171. all_equivalent_types = all(
  172. proper_type is not None and is_equivalent(proper_type, underlying_type)
  173. for proper_type in proper_types
  174. )
  175. if all_equivalent_types:
  176. return make_simplified_union(cast(Sequence[Type], proper_types))
  177. return ctx.default_attr_type
  178. assert isinstance(ctx.type, Instance)
  179. info = ctx.type.type
  180. # As long as mypy doesn't understand attribute creation in __new__,
  181. # there is no way to predict the value type if the enum class has a
  182. # custom implementation
  183. if _implements_new(info):
  184. return ctx.default_attr_type
  185. stnode = info.get(enum_field_name)
  186. if stnode is None:
  187. return ctx.default_attr_type
  188. underlying_type = _infer_value_type_with_auto_fallback(ctx, get_proper_type(stnode.type))
  189. if underlying_type is None:
  190. return ctx.default_attr_type
  191. return underlying_type
  192. def _extract_underlying_field_name(typ: Type) -> str | None:
  193. """If the given type corresponds to some Enum instance, returns the
  194. original name of that enum. For example, if we receive in the type
  195. corresponding to 'SomeEnum.FOO', we return the string "SomeEnum.Foo".
  196. This helper takes advantage of the fact that Enum instances are valid
  197. to use inside Literal[...] types. An expression like 'SomeEnum.FOO' is
  198. actually represented by an Instance type with a Literal enum fallback.
  199. We can examine this Literal fallback to retrieve the string.
  200. """
  201. typ = get_proper_type(typ)
  202. if not isinstance(typ, Instance):
  203. return None
  204. if not typ.type.is_enum:
  205. return None
  206. underlying_literal = typ.last_known_value
  207. if underlying_literal is None:
  208. return None
  209. # The checks above have verified this LiteralType is representing an enum value,
  210. # which means the 'value' field is guaranteed to be the name of the enum field
  211. # as a string.
  212. assert isinstance(underlying_literal.value, str)
  213. return underlying_literal.value