From 4f1f14e38b356c2b51fee08a7951abfe5903c5cd Mon Sep 17 00:00:00 2001 From: Nikita Shulga Date: Fri, 6 Jan 2023 21:45:04 +0000 Subject: [PATCH] [JIT] Skip builtins while enumerating class methods (#91805) This is needed to support `enum.Enum` derived classes in Python-3.11 that adds `_new_member_` to classdict, see: https://github.com/python/cpython/blob/15c44789bb125b93e96815a336ec73423c47508e/Lib/enum.py#L529 Following snippet illustrates the problem with the previous iteration of the code on 3.11: ```python from enum import Enum import inspect class Color(Enum): RED = 1 GREEN = 2 def print_routines(cls): print(cls.__name__) for name in cls.__dict__: fn = getattr(cls, name) if inspect.isroutine(fn): print(name, fn, f"has_globals: {hasattr(fn, '__globals__')}") print_routines(Color) ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/91805 Approved by: https://github.com/albanD, https://github.com/suo --- torch/_jit_internal.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/torch/_jit_internal.py b/torch/_jit_internal.py index 223d96aa84a..9d108651ffd 100644 --- a/torch/_jit_internal.py +++ b/torch/_jit_internal.py @@ -448,6 +448,10 @@ def createResolutionCallbackForClassMethods(cls): for name in cls.__dict__ if inspect.isroutine(getattr(cls, name)) ] + # Skip built-ins, as they do not have global scope nor type hints + # Needed to support `enum.Enum` derived classes in Python-3.11 + # That adds `_new_member_` property which is an alias to `__new__` + fns = [fn for fn in fns if not inspect.isbuiltin(fn)] captures = {} for fn in fns: