MCPcopy Index your code
hub / github.com/python/mypy / _implements_new

Function _implements_new

mypy/plugins/enums.py:129–141  ·  view source on GitHub ↗

Check whether __new__ comes from enum.Enum or was implemented in a subclass of enum.Enum. In the latter case, we must infer Any as long as mypy can't infer the type of _value_ from assignments in __new__. If, however, __new__ comes from a user-defined class that is not an Enum subclass

(info: TypeInfo)

Source from the content-addressed store, hash-verified

127
128
129def _implements_new(info: TypeInfo) -> bool:
130 """Check whether __new__ comes from enum.Enum or was implemented in a
131 subclass of enum.Enum. In the latter case, we must infer Any as long as mypy can't infer
132 the type of _value_ from assignments in __new__.
133
134 If, however, __new__ comes from a user-defined class that is not an Enum subclass (i.e.
135 the data type) this is allowed, because we should in general infer that an enum entry's
136 value has that type.
137 """
138 type_with_new = _first(ti for ti in info.mro if ti.is_enum and ti.names.get("__new__"))
139 if type_with_new is None:
140 return False
141 return type_with_new.fullname not in ("enum.Enum", "enum.IntEnum", "enum.StrEnum")
142
143
144def enum_member_callback(ctx: mypy.plugin.FunctionContext) -> Type:

Callers 1

enum_value_callbackFunction · 0.85

Calls 2

_firstFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…