Skip to content

Commit 821630c

Browse files
authored
[mypyc] Fix interaction of Enum caching with _order_ (python#8120)
1 parent 27715ee commit 821630c

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

mypyc/genops.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,12 @@ def add_non_ext_class_attr(self, non_ext: NonExtClassInfo, lvalue: NameExpr,
14451445
self.add_to_non_ext_dict(non_ext, lvalue.name, rvalue, stmt.line)
14461446
# We cache enum attributes to speed up enum attribute lookup since they
14471447
# are final.
1448-
if cdef.info.bases and cdef.info.bases[0].type.fullname == 'enum.Enum':
1448+
if (
1449+
cdef.info.bases
1450+
and cdef.info.bases[0].type.fullname == 'enum.Enum'
1451+
# Skip "_order_", since Enum will remove it
1452+
and lvalue.name != '_order_'
1453+
):
14491454
attr_to_cache.append(lvalue)
14501455

14511456
def setup_non_ext_dict(self, cdef: ClassDef, bases: Value) -> Value:

mypyc/test-data/run-classes.test

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ assert o.get(20) == 20
126126
from enum import Enum
127127

128128
class TestEnum(Enum):
129+
_order_ = "a b"
129130
a : int = 1
130131
b : int = 2
131132

@@ -140,11 +141,14 @@ assert TestEnum.test() == 3
140141
# non-extension classes
141142

142143
[file driver.py]
143-
from native import TestEnum
144-
assert TestEnum.a.name == 'a'
145-
assert TestEnum.a.value == 1
146-
assert TestEnum.b.name == 'b'
147-
assert TestEnum.b.value == 2
144+
import sys
145+
# "_order_" isn't supported in 3.5
146+
if sys.version_info[:2] > (3, 5):
147+
from native import TestEnum
148+
assert TestEnum.a.name == 'a'
149+
assert TestEnum.a.value == 1
150+
assert TestEnum.b.name == 'b'
151+
assert TestEnum.b.value == 2
148152

149153
[case testRunDataclass]
150154
from dataclasses import dataclass, field

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy