Skip to content

Conversation

alfiedouglas0
Copy link

@alfiedouglas0 alfiedouglas0 commented Sep 16, 2025

Previously, mypy would infer that Foo(Foo.x) is of type Foo (where Foo is an Enum). This is problematic as it means Enums can not be reconstructed for runtime saftey (see quote below for more detail).

Hence, this PR extends the enum plugin and refines the return type of Enum.__new__, ensuring reconstructed Enums are idempotent.

Fixes #19669

(Taken from #19669,) below gives an example as of why we care about reconstructed Enums:

It's useful for example in a case like this:

from enum import IntEnum

class Option(IntEnum):
    x=0
    y=1

class Base[T: Option]:
    option: T

    def __init__(self, option: T) -> None:
        self.option = Option(option) # for runtime safety

A user of the library that does not use a type-checker may pass a plain integer (Or maybe older versions of the code use plain integer rather than a dedicated enum). Therefore, it's best to be safe and apply the enum constructor anyway. But this currently raises an [assignment] error on the side of the library author.

@alfiedouglas0 alfiedouglas0 force-pushed the ensure-reconstructed-enums-are-idompotent branch from 7a06c7b to 2453222 Compare September 16, 2025 17:48

This comment has been minimized.

Previously, mypy would infer that `Foo(Foo.x)` is of type `Foo`. This is problematic as it means Enums can not be reconstructed for runtime saftey.

Hence, this commit extends the enum plugin and refines the return type of `Enum.__new__`, ensuring reconstructed Enums are idempotent

Fixes python#19669
@alfiedouglas0 alfiedouglas0 force-pushed the ensure-reconstructed-enums-are-idompotent branch from 2453222 to 8e0167e Compare September 16, 2025 18:34
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

AutoSplit (https://github.com/Toufool/AutoSplit)
-   File "/tmp/mypy_primer/mypy_old/venv/bin/mypy", line 7, in <module>
+   File "/tmp/mypy_primer/mypy_new/venv/bin/mypy", line 7, in <module>
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/main.py", line 127, in main
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/main.py", line 127, in main
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/main.py", line 211, in run_build
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/main.py", line 211, in run_build
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 196, in build
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 196, in build
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 272, in _build
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 272, in _build
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 2946, in dispatch
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 2946, in dispatch
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 3346, in process_graph
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 3346, in process_graph
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 3475, in process_stale_scc
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 3475, in process_stale_scc
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/build.py", line 2493, in write_cache
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/build.py", line 2493, in write_cache
-   File "/tmp/mypy_primer/mypy_old/venv/lib/python3.13/site-packages/mypy/cache.py", line 28, in __init__
+   File "/tmp/mypy_primer/mypy_new/venv/lib/python3.13/site-packages/mypy/cache.py", line 28, in __init__

@alfiedouglas0 alfiedouglas0 marked this pull request as ready for review September 16, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make enums idempotent
1 participant