@@ -246,6 +246,11 @@ static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambd
246246static void py_register_dict_literal (PyLSPContext * ctx , const char * name , TSNode dict_node );
247247static TSNode py_lookup_lambda (PyLSPContext * ctx , const char * name );
248248static const char * py_lookup_dict_dispatch (PyLSPContext * ctx , const char * var , const char * key );
249+ static void py_process_function (PyLSPContext * ctx , TSNode func_node , const char * container_qn );
250+ static void py_process_class (PyLSPContext * ctx , TSNode class_node );
251+ static void py_process_class_in (PyLSPContext * ctx , TSNode class_node , const char * container_qn );
252+ typedef enum { PY_LOCAL_DEFS_BIND , PY_LOCAL_DEFS_PROCESS } PyLocalDefsMode ;
253+ static void py_visit_local_defs (PyLSPContext * ctx , TSNode block , PyLocalDefsMode mode , int depth );
249254
250255/* ── Scope mutation wrappers ─────────────────────────────────────────────
251256 *
@@ -4362,6 +4367,101 @@ static void py_bind_parameters(PyLSPContext *ctx, TSNode params) {
43624367 }
43634368}
43644369
4370+ /* Bind a def or class statement of the current function body as a local of
4371+ * that function (#1548): its graph QN extends the function's
4372+ * (enclosing_func_qn.name), the QN extraction gives it. An undecorated def
4373+ * registered with that QN becomes an exact callable alias; anything else
4374+ * (decorated, overloaded, not registered) is a hard local shadow, so no call
4375+ * falls through to a same-named module symbol. A class is bound by its type
4376+ * when registered, else shadowed. A parameter of the same name is already a
4377+ * local and keeps its own binding. */
4378+ static void py_bind_local_def (PyLSPContext * ctx , TSNode def , bool is_class ) {
4379+ TSNode name_node = ts_node_child_by_field_name (def , "name" , 4 );
4380+ char * name = ts_node_is_null (name_node ) ? NULL : py_node_text (ctx , name_node );
4381+ if (!name || !name [0 ] || !ctx -> enclosing_func_qn ) {
4382+ py_disable_callable_value_proof (ctx );
4383+ return ;
4384+ }
4385+ if (cbm_scope_lookup_local (ctx -> current_scope , name ))
4386+ return ;
4387+ const char * qn = cbm_arena_sprintf (ctx -> arena , "%s.%s" , ctx -> enclosing_func_qn , name );
4388+ if (!qn ) {
4389+ py_disable_callable_value_proof (ctx );
4390+ py_scope_bind (ctx , name , cbm_type_unknown ());
4391+ return ;
4392+ }
4393+ if (is_class ) {
4394+ py_scope_bind (ctx , name ,
4395+ cbm_registry_lookup_type (ctx -> registry , qn ) ? cbm_type_named (ctx -> arena , qn )
4396+ : cbm_type_unknown ());
4397+ return ;
4398+ }
4399+ const CBMRegisteredFunc * func = cbm_registry_lookup_func (ctx -> registry , qn );
4400+ if (py_func_is_exact_callable_value (func )) {
4401+ py_scope_bind_callable (ctx , name , cbm_type_unknown (), func -> qualified_name );
4402+ } else {
4403+ py_scope_bind (ctx , name , cbm_type_unknown ());
4404+ }
4405+ }
4406+
4407+ /* Statements a function body nests further statements in. A def inside any of
4408+ * them is still a local of the function; a nested def, class or lambda body
4409+ * is a scope of its own and is never entered. */
4410+ static bool py_is_statement_block (const char * kind ) {
4411+ static const char * const kinds [] = {"block" ,
4412+ "if_statement" ,
4413+ "elif_clause" ,
4414+ "else_clause" ,
4415+ "for_statement" ,
4416+ "while_statement" ,
4417+ "try_statement" ,
4418+ "except_clause" ,
4419+ "except_group_clause" ,
4420+ "finally_clause" ,
4421+ "with_statement" ,
4422+ "match_statement" ,
4423+ "case_clause" ,
4424+ NULL };
4425+ for (int i = 0 ; kinds [i ]; i ++ ) {
4426+ if (strcmp (kind , kinds [i ]) == 0 )
4427+ return true;
4428+ }
4429+ return false;
4430+ }
4431+
4432+ /* Visit the def/class statements of a function body, including those inside
4433+ * its compound statements: BIND binds each as a local (py_bind_local_def),
4434+ * PROCESS walks each nested def's own body (py_process_function). */
4435+ static void py_visit_local_defs (PyLSPContext * ctx , TSNode block , PyLocalDefsMode mode , int depth ) {
4436+ if (!ctx || ctx -> eval_failure || ts_node_is_null (block ) || depth >= cbm_lsp_max_walk_depth ())
4437+ return ;
4438+ PyKids kids = py_kids (ctx -> arena , block );
4439+ for (uint32_t i = 0 ; i < kids .n && !ctx -> eval_failure ; i ++ ) {
4440+ TSNode c = py_kid (& kids , i );
4441+ const char * ck = ts_node_type (c );
4442+ TSNode def = c ;
4443+ if (strcmp (ck , "decorated_definition" ) == 0 ) {
4444+ def = ts_node_child_by_field_name (c , "definition" , 10 );
4445+ if (ts_node_is_null (def ))
4446+ continue ;
4447+ ck = ts_node_type (def );
4448+ }
4449+ if (strcmp (ck , "function_definition" ) == 0 ) {
4450+ if (mode == PY_LOCAL_DEFS_BIND )
4451+ py_bind_local_def (ctx , def , false);
4452+ else
4453+ py_process_function (ctx , def , ctx -> enclosing_func_qn );
4454+ } else if (strcmp (ck , "class_definition" ) == 0 ) {
4455+ if (mode == PY_LOCAL_DEFS_BIND )
4456+ py_bind_local_def (ctx , def , true);
4457+ else
4458+ py_process_class_in (ctx , def , ctx -> enclosing_func_qn );
4459+ } else if (py_is_statement_block (ck )) {
4460+ py_visit_local_defs (ctx , c , mode , depth + 1 );
4461+ }
4462+ }
4463+ }
4464+
43654465static void py_process_function (PyLSPContext * ctx , TSNode func_node , const char * container_qn ) {
43664466 if (!ctx || ctx -> eval_failure )
43674467 return ;
@@ -4391,22 +4491,13 @@ static void py_process_function(PyLSPContext *ctx, TSNode func_node, const char
43914491
43924492 TSNode body = ts_node_child_by_field_name (func_node , "body" , 4 );
43934493 if (!ts_node_is_null (body )) {
4494+ /* Python resolves a bare name in the function's own scope first: every
4495+ * def/class statement of the body binds a local for the whole body
4496+ * (LEGB), whatever a module-level symbol of the same spelling says. */
4497+ py_visit_local_defs (ctx , body , PY_LOCAL_DEFS_BIND , 0 );
43944498 py_resolve_calls_in (ctx , body );
4395- // Also descend into nested function/class definitions in the body.
4396- PyKids bk = py_kids (ctx -> arena , body );
4397- for (uint32_t i = 0 ; i < bk .n ; i ++ ) {
4398- TSNode c = py_kid (& bk , i );
4399- const char * ck = ts_node_type (c );
4400- if (strcmp (ck , "function_definition" ) == 0 ) {
4401- py_process_function (ctx , c , ctx -> enclosing_func_qn );
4402- } else if (strcmp (ck , "decorated_definition" ) == 0 ) {
4403- TSNode def = ts_node_child_by_field_name (c , "definition" , 10 );
4404- if (!ts_node_is_null (def ) &&
4405- strcmp (ts_node_type (def ), "function_definition" ) == 0 ) {
4406- py_process_function (ctx , def , ctx -> enclosing_func_qn );
4407- }
4408- }
4409- }
4499+ // Also descend into nested function definitions in the body.
4500+ py_visit_local_defs (ctx , body , PY_LOCAL_DEFS_PROCESS , 0 );
44104501 }
44114502
44124503 py_scope_restore (ctx , saved );
@@ -4536,7 +4627,10 @@ static void py_exec_class_definition(PyLSPContext *ctx, TSNode class_node) {
45364627 py_scope_restore (ctx , saved_scope );
45374628}
45384629
4539- static void py_process_class (PyLSPContext * ctx , TSNode class_node ) {
4630+ /* Walk a class's methods with the class qualified under `container_qn`: the
4631+ * module for a module-level class, the enclosing def for a class declared in
4632+ * a function body (#1548: extraction gives it the def's QN as prefix). */
4633+ static void py_process_class_in (PyLSPContext * ctx , TSNode class_node , const char * container_qn ) {
45404634 if (!ctx || ctx -> eval_failure )
45414635 return ;
45424636 TSNode name_node = ts_node_child_by_field_name (class_node , "name" , 4 );
@@ -4547,7 +4641,8 @@ static void py_process_class(PyLSPContext *ctx, TSNode class_node) {
45474641 return ;
45484642
45494643 const char * prev_class = ctx -> enclosing_class_qn ;
4550- ctx -> enclosing_class_qn = cbm_arena_sprintf (ctx -> arena , "%s.%s" , ctx -> module_qn , cname );
4644+ ctx -> enclosing_class_qn =
4645+ cbm_arena_sprintf (ctx -> arena , "%s.%s" , container_qn ? container_qn : ctx -> module_qn , cname );
45514646
45524647 TSNode body = ts_node_child_by_field_name (class_node , "body" , 4 );
45534648 if (!ts_node_is_null (body )) {
@@ -4621,6 +4716,10 @@ static void py_process_class(PyLSPContext *ctx, TSNode class_node) {
46214716 ctx -> enclosing_class_qn = prev_class ;
46224717}
46234718
4719+ static void py_process_class (PyLSPContext * ctx , TSNode class_node ) {
4720+ py_process_class_in (ctx , class_node , NULL );
4721+ }
4722+
46244723/* Module bindings are replayed in source order before deferred function bodies
46254724 * are analyzed. Class definitions always replace an earlier callable identity;
46264725 * the registry decides whether the resulting class type is known precisely. */
@@ -4687,6 +4786,28 @@ static char *py_module_prefix(CBMArena *scratch, const char *module_qn, size_t l
46874786 return prefix ;
46884787}
46894788
4789+ /* A class declared inside a def -- django's per-test `class MyModelAdmin`, an
4790+ * app factory's local class, and anything nested in one -- is a local of that
4791+ * def, not a global of the module: bound here by its short name, every
4792+ * same-named local class of the file collapsed onto whichever registered last.
4793+ * Its QN extends a registered function's (proj.mod.Tests.test_x.MyModelAdmin),
4794+ * which tells it apart from a class nested in a class (proj.mod.Outer.Inner).
4795+ * Only prefixes longer than the module are probed. */
4796+ static bool py_type_is_function_local (const PyLSPContext * ctx , const char * qn , size_t module_len ) {
4797+ char scope [512 ];
4798+ size_t len = strlen (qn );
4799+ if (len >= sizeof (scope ))
4800+ return false;
4801+ memcpy (scope , qn , len + 1 );
4802+ for (char * dot = strrchr (scope , '.' ); dot && (size_t )(dot - scope ) > module_len ;
4803+ dot = strrchr (scope , '.' )) {
4804+ * dot = '\0' ;
4805+ if (cbm_registry_lookup_func (ctx -> registry , scope ))
4806+ return true;
4807+ }
4808+ return false;
4809+ }
4810+
46904811/* A project registry can contain another file's class in the same logical
46914812 * Python module. Keep those cross-file globals available, but do not prebind
46924813 * classes declared by this source: their binding epoch belongs in the ordered
@@ -4734,6 +4855,8 @@ static void py_bind_external_module_classes(PyLSPContext *ctx, const PyKids *rk)
47344855 continue ;
47354856 if (package_scope && strchr (qn + prefix_len + 1 , '.' ))
47364857 continue ; /* submodule/nested classes are not package globals */
4858+ if (py_type_is_function_local (ctx , qn , prefix_len ))
4859+ continue ; /* a def-local class is no module global (#1548) */
47374860 py_scope_bind (ctx , name , cbm_type_named (ctx -> arena , qn ));
47384861 }
47394862 if (prefix )
0 commit comments