Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Release/2.1][JIT] Fix typed enum handling in 3.11 (#109807)
In Python-3.11+ typed enums (such as `enum.IntEnum`) retain `__new__`,`__str__` and so on method of the base class via `__init__subclass__()` method (see https://docs.python.org/3/whatsnew/3.11.html#enum ), i.e. following code ```python import sys import inspect from enum import Enum class IntColor(int, Enum): RED = 1 GREEN = 2 class Color(Enum): RED = 1 GREEN = 2 def get_methods(cls): def predicate(m): if not inspect.isfunction(m) and not inspect.ismethod(m): return False return m.__name__ in cls.__dict__ return inspect.getmembers(cls, predicate=predicate) if __name__ == "__main__": print(sys.version) print(f"IntColor methods {get_methods(IntColor)}") print(f"Color methods {get_methods(Color)}") ``` Returns empty list for both cases for older Python, but on Python-3.11+ it returns list contains of enum constructors and others: ```shell % conda run -n py310 python bar.py 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ] IntColor methods [] Color methods [] % conda run -n py311 python bar.py 3.11.0 | packaged by conda-forge | (main, Oct 25 2022, 06:21:25) [Clang 14.0.4 ] IntColor methods [('__format__', <function Enum.__format__ at 0x105006ac0>), ('__new__', <function Enum.__new__ at 0x105006660>), ('__repr__', <function Enum.__repr__ at 0x1050068e0>)] Color methods [] ``` This change allows typed enums to be scriptable on 3.11, by explicitly marking several `enum.Enum` method to be dropped by jit script and adds test that typed enums are jit-scriptable. Fixes #108933 Cherry-pick of #109717 into release/2.1 branch. Approved by: https://github.com/atalman, https://github.com/davidberard98 (cherry picked from commit 55685d5)
- Loading branch information