Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be70e3c

Browse files
committed
Use _PyST_IsFunctionLike in comprehension inlining code
1 parent 785dd45 commit be70e3c

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

Lib/test/test_type_params.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ class Child[T](Base[lambda: (int, outer_var, T)]): ...
391391
T, = Child.__type_params__
392392
self.assertEqual(func(), (int, "outer", T))
393393

394+
def test_comprehension_01(self):
395+
type Alias[T: ([T for T in (T, [1])[1]], T)] = [T for T in T.__name__]
396+
self.assertEqual(Alias.__value__, ["T"])
397+
T, = Alias.__type_params__
398+
self.assertEqual(T.__constraints__, ([1], T))
399+
394400

395401
def global_generic_func[T]():
396402
pass

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5420,7 +5420,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
54205420
// assignment expression to a nonlocal in the comprehension, these don't
54215421
// need handling here since they shouldn't be isolated
54225422
if (symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) {
5423-
if (c->u->u_ste->ste_type != FunctionBlock) {
5423+
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
54245424
// non-function scope: override this name to use fast locals
54255425
PyObject *orig = PyDict_GetItem(c->u->u_metadata.u_fasthidden, k);
54265426
if (orig != Py_True) {

Python/symtable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
648648
if (PyLong_AsLong(existing) & DEF_BOUND) {
649649
// cell vars in comprehension that are locals in outer scope
650650
// must be promoted to cell so u_cellvars isn't wrong
651-
if (scope == CELL && ste->ste_type == FunctionBlock) {
651+
if (scope == CELL && _PyST_IsFunctionLike(ste)) {
652652
SET_SCOPE(scopes, k, scope);
653653
}
654654

0 commit comments

Comments
 (0)