[3.12] gh-109219: propagate free vars through type param scopes (GH-109377) (#109410)

gh-109219: propagate free vars through type param scopes (GH-109377)
(cherry picked from commit 909adb5092)

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-09-14 15:42:39 -07:00 committed by GitHub
parent 52a9c5760c
commit 35c633d245
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View file

@ -694,6 +694,19 @@ class TypeParamsClassScopeTest(unittest.TestCase):
cls = ns["outer"]() cls = ns["outer"]()
self.assertEqual(cls.Alias.__value__, "class") self.assertEqual(cls.Alias.__value__, "class")
def test_nested_free(self):
ns = run_code("""
def f():
T = str
class C:
T = int
class D[U](T):
x = T
return C
""")
C = ns["f"]()
self.assertIn(int, C.D.__bases__)
self.assertIs(C.D.x, str)
class TypeParamsManglingTest(unittest.TestCase): class TypeParamsManglingTest(unittest.TestCase):
def test_mangling(self): def test_mangling(self):

View file

@ -0,0 +1,2 @@
Fix compiling type param scopes that use a name which is also free in an
inner scope.

View file

@ -801,8 +801,7 @@ update_symbols(PyObject *symbols, PyObject *scopes,
the class that has the same name as a local the class that has the same name as a local
or global in the class scope. or global in the class scope.
*/ */
if (classflag && if (classflag) {
PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS; long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
v_new = PyLong_FromLong(flags); v_new = PyLong_FromLong(flags);
if (!v_new) { if (!v_new) {
@ -1037,7 +1036,7 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
goto error; goto error;
/* Records the results of the analysis in the symbol table entry */ /* Records the results of the analysis in the symbol table entry */
if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, inlined_cells, if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, inlined_cells,
ste->ste_type == ClassBlock)) (ste->ste_type == ClassBlock) || ste->ste_can_see_class_scope))
goto error; goto error;
temp = PyNumber_InPlaceOr(free, newfree); temp = PyNumber_InPlaceOr(free, newfree);