-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[SOT] Support for simulation of enum types #73269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
|
||
@Dispatcher.register_decorator(operator.ne) | ||
def dispatch_enum_ne(lhs: EnumVariable, rhs: EnumVariable): | ||
return Dispatcher.call(operator.eq, lhs, rhs).bool_not() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO:之后有时间将 (VariableBase, VariableBase)
ne
转发到 eq
取反,这样就不必每个都单独写一遍了
这里可以把 TODO 记到代码里
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
union_free_vars( | ||
frame_value_tracer.free_vars, | ||
{ | ||
f"{self.get_py_value().__class__.__name__}": self.get_py_type() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的 看来是的:self.get_py_value().__class__.__name__
会在哪里用到呢?f"{self.value!s}"
会用到是么?
>>> from enum import Enum
>>>
>>> class Foo(Enum):
... A = 1
... B = 2
...
>>> str(Foo.A)
'Foo.A'
>>> repr(Foo.A)
'<Foo.A: 1>'
>>>
这里其实是有一点风险的,毕竟依赖于 str
的实现,而且还有多个 Foo
冲突的风险,比如
# a.py
class Foo(Enum):
A: 1
def fn(foo: Foo):
...
# b.py
class Foo(Enum):
B: 2
def fn(foo: Foo):
...
这里 a.fn.__globals__['Foo']
和 b.fn.__globals__['Foo']
完全不同,但是最终混合到生成的 guard 里会统一变成 Foo
,后者会把前者覆盖,那在访问前者 Foo.A
时就会挂掉了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里建议维护一个 name -> enum class
的映射
known_enum_classes = {}
def get_enum_class_id(enum_class: type[Enum]):
class_name = enum_class.__name__
known_enum_classes.setdefault(class_name, [])
same_name_enums = known_enum_classes[class_name]
id = 0
for i, cls in enumerate(same_name_enums):
if enum_class == cls:
id = i
break
else:
id = len(same_name_enums)
same_name_enums.append(enum_class)
return id
enum_class = instance.__class__
class_name = enum_class.__name__
enum_class_id = get_enum_class_id(enum_class)
extern_var_name = f"__{class_name}_{enum_class_id}"
# tracker 用 f"{extern_var_name}.{value.name}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有考虑到类名重复的问题,已修改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Category
Execute Infrastructure
PR Types
New features
Description
Support for simulation of enum types:
EnumVariable
operator.eq
andoperator.ne
forEnumVariable
spcard-67164