|
19 | 19 | import mypy.plugin # To avoid circular imports.
|
20 | 20 | from mypy.checker_shared import TypeCheckerSharedApi
|
21 | 21 | from mypy.nodes import TypeInfo, Var
|
| 22 | +from mypy.plugin import FunctionContext # depending on where this runs |
22 | 23 | from mypy.subtypes import is_equivalent
|
23 | 24 | from mypy.typeops import fixup_partial_type, make_simplified_union
|
24 | 25 | from mypy.types import (
|
|
28 | 29 | LiteralType,
|
29 | 30 | ProperType,
|
30 | 31 | Type,
|
| 32 | + TypeVarType, |
| 33 | + UnionType, |
31 | 34 | get_proper_type,
|
32 | 35 | is_named_instance,
|
33 | 36 | )
|
@@ -297,3 +300,60 @@ def _extract_underlying_field_name(typ: Type) -> str | None:
|
297 | 300 | # as a string.
|
298 | 301 | assert isinstance(underlying_literal.value, str)
|
299 | 302 | return underlying_literal.value
|
| 303 | + |
| 304 | + |
| 305 | +def enum_new_callback(ctx: FunctionContext) -> Type: |
| 306 | + """This plugin refines the return type of `__new__`, ensuring reconstructed |
| 307 | + Enums are idempotent. |
| 308 | +
|
| 309 | + By default, mypy will infer that `Foo(Foo.x)` is of type `Foo`. This plugin |
| 310 | + ensures types are not loosened, meaning with this plugin enabled |
| 311 | + `Foo(Foo.x)` is of type `Literal[Foo.x]?`. |
| 312 | +
|
| 313 | + This means with this plugin: |
| 314 | + ``` |
| 315 | + reveal_type(Foo(Foo.x)) # mypy reveals Literal[Foo.x]? |
| 316 | + ``` |
| 317 | +
|
| 318 | + This plugin works by adjusting the return type of `__new__` to be the given |
| 319 | + argument type, if and only if `__new__` comes from `enum.Enum`. |
| 320 | +
|
| 321 | + This plugin supports arguments that are Final, Literial, Union of Literials |
| 322 | + and generic TypeVars. |
| 323 | + """ |
| 324 | + base_ret = ctx.default_return_type |
| 325 | + enum_inst = get_proper_type(base_ret) |
| 326 | + if not isinstance(enum_inst, Instance): |
| 327 | + return base_ret |
| 328 | + |
| 329 | + info: TypeInfo = enum_inst.type |
| 330 | + if not info.is_enum: |
| 331 | + return base_ret |
| 332 | + |
| 333 | + if _implements_new(info): |
| 334 | + return base_ret |
| 335 | + |
| 336 | + if not ctx.args or not ctx.args[0] or not ctx.arg_types or not ctx.arg_types[0]: |
| 337 | + return base_ret |
| 338 | + |
| 339 | + arg0_t = get_proper_type(ctx.arg_types[0][0]) |
| 340 | + |
| 341 | + if isinstance(arg0_t, Instance) and arg0_t.type is info: |
| 342 | + return arg0_t |
| 343 | + elif isinstance(arg0_t, LiteralType) and arg0_t.fallback.type is info: |
| 344 | + return arg0_t |
| 345 | + elif isinstance(arg0_t, UnionType): |
| 346 | + |
| 347 | + def is_memeber(given_t: Type) -> bool: |
| 348 | + return (isinstance(given_t, Instance) and given_t.type is info) or ( |
| 349 | + isinstance(given_t, LiteralType) and given_t.fallback.type is info |
| 350 | + ) |
| 351 | + |
| 352 | + items = [get_proper_type(it) for it in arg0_t.items] |
| 353 | + if items and all(is_memeber(item) for item in items): |
| 354 | + return arg0_t |
| 355 | + elif (isinstance(arg0_t, TypeVarType)) and isinstance(arg0_t.upper_bound, Instance): |
| 356 | + if arg0_t.upper_bound.type is info: |
| 357 | + return arg0_t |
| 358 | + |
| 359 | + return base_ret |
0 commit comments