@@ -4584,6 +4584,169 @@ static bool extract_sql_ddl_class_def(CBMExtractCtx *ctx, TSNode node, const cha
45844584 return true;
45854585}
45864586
4587+ /* ── Python annotated instance fields (#1277) ─────────────────────
4588+ * Exported as result->field_types (never graph nodes) so the cross-file LSP
4589+ * can type `obj.x` when obj's class lives in another file. Only DECLARED
4590+ * types count: a class-body annotation, a `self.x: T = v` annotation in
4591+ * __init__, or `self.x = p` where p is an annotated __init__ parameter. An
4592+ * unannotated right-hand side is never guessed at. */
4593+
4594+ static void py_push_field_type (CBMExtractCtx * ctx , const char * class_qn , TSNode name_node ,
4595+ const char * type_text ) {
4596+ char * name = cbm_node_text (ctx -> arena , name_node , ctx -> source );
4597+ if (!name || !name [0 ] || !type_text || !type_text [0 ]) {
4598+ return ;
4599+ }
4600+ CBMFieldType ft = {.class_qn = class_qn , .field_name = name , .type_text = type_text };
4601+ cbm_fieldtype_push (& ctx -> result -> field_types , ctx -> arena , ft );
4602+ }
4603+
4604+ /* The assignment inside an expression_statement, or a null node. */
4605+ static TSNode py_statement_assignment (TSNode stmt ) {
4606+ TSNode null_node = {0 };
4607+ if (strcmp (ts_node_type (stmt ), "expression_statement" ) != 0 ||
4608+ ts_node_named_child_count (stmt ) == 0 ) {
4609+ return null_node ;
4610+ }
4611+ TSNode inner = ts_node_named_child (stmt , 0 );
4612+ return strcmp (ts_node_type (inner ), "assignment" ) == 0 ? inner : null_node ;
4613+ }
4614+
4615+ /* Annotation text of the __init__ parameter called `name`, or NULL. */
4616+ static const char * py_init_param_annotation (CBMExtractCtx * ctx , TSNode params , const char * name ) {
4617+ uint32_t n = ts_node_named_child_count (params );
4618+ for (uint32_t i = 0 ; i < n ; i ++ ) {
4619+ TSNode p = ts_node_named_child (params , i );
4620+ const char * pk = ts_node_type (p );
4621+ TSNode pname = {0 };
4622+ if (strcmp (pk , "typed_default_parameter" ) == 0 ) {
4623+ pname = ts_node_child_by_field_name (p , TS_FIELD ("name" ));
4624+ } else if (strcmp (pk , "typed_parameter" ) == 0 && ts_node_named_child_count (p ) > 0 ) {
4625+ pname = ts_node_named_child (p , 0 );
4626+ } else {
4627+ continue ;
4628+ }
4629+ TSNode ptype = ts_node_child_by_field_name (p , TS_FIELD ("type" ));
4630+ if (ts_node_is_null (pname ) || ts_node_is_null (ptype ) ||
4631+ strcmp (ts_node_type (pname ), "identifier" ) != 0 ) {
4632+ continue ;
4633+ }
4634+ char * pn = cbm_node_text (ctx -> arena , pname , ctx -> source );
4635+ if (pn && strcmp (pn , name ) == 0 ) {
4636+ return cbm_node_text (ctx -> arena , ptype , ctx -> source );
4637+ }
4638+ }
4639+ return NULL ;
4640+ }
4641+
4642+ /* `self.x: T = v` / `self.x = p` inside __init__ (self = its first parameter). */
4643+ static void py_init_field_assignment (CBMExtractCtx * ctx , const char * class_qn , TSNode assign ,
4644+ TSNode params , const char * self_name ) {
4645+ TSNode left = ts_node_child_by_field_name (assign , TS_FIELD ("left" ));
4646+ if (ts_node_is_null (left ) || strcmp (ts_node_type (left ), "attribute" ) != 0 ) {
4647+ return ;
4648+ }
4649+ TSNode obj = ts_node_child_by_field_name (left , TS_FIELD ("object" ));
4650+ TSNode attr = ts_node_child_by_field_name (left , TS_FIELD ("attribute" ));
4651+ if (ts_node_is_null (obj ) || ts_node_is_null (attr ) ||
4652+ strcmp (ts_node_type (obj ), "identifier" ) != 0 ) {
4653+ return ;
4654+ }
4655+ char * obj_name = cbm_node_text (ctx -> arena , obj , ctx -> source );
4656+ if (!obj_name || strcmp (obj_name , self_name ) != 0 ) {
4657+ return ;
4658+ }
4659+ TSNode ann = ts_node_child_by_field_name (assign , TS_FIELD ("type" ));
4660+ if (!ts_node_is_null (ann )) {
4661+ py_push_field_type (ctx , class_qn , attr , cbm_node_text (ctx -> arena , ann , ctx -> source ));
4662+ return ;
4663+ }
4664+ TSNode right = ts_node_child_by_field_name (assign , TS_FIELD ("right" ));
4665+ if (ts_node_is_null (right ) || strcmp (ts_node_type (right ), "identifier" ) != 0 ) {
4666+ return ;
4667+ }
4668+ char * rhs = cbm_node_text (ctx -> arena , right , ctx -> source );
4669+ if (rhs && strcmp (rhs , self_name ) != 0 ) {
4670+ py_push_field_type (ctx , class_qn , attr , py_init_param_annotation (ctx , params , rhs ));
4671+ }
4672+ }
4673+
4674+ /* Every `self.x` field assignment in one __init__ body, nested blocks
4675+ * included; nested functions, lambdas and classes have their own `self`. */
4676+ static void py_extract_init_fields (CBMExtractCtx * ctx , const char * class_qn , TSNode fn ) {
4677+ TSNode params = ts_node_child_by_field_name (fn , TS_FIELD ("parameters" ));
4678+ TSNode body = ts_node_child_by_field_name (fn , TS_FIELD ("body" ));
4679+ if (ts_node_is_null (params ) || ts_node_is_null (body ) ||
4680+ ts_node_named_child_count (params ) == 0 ) {
4681+ return ;
4682+ }
4683+ TSNode self_node = ts_node_named_child (params , 0 );
4684+ if (strcmp (ts_node_type (self_node ), "identifier" ) != 0 ) {
4685+ return ;
4686+ }
4687+ char * self_name = cbm_node_text (ctx -> arena , self_node , ctx -> source );
4688+ if (!self_name || !self_name [0 ]) {
4689+ return ;
4690+ }
4691+ TSNodeStack stack ;
4692+ ts_nstack_init (& stack , ctx , CBM_SZ_32 );
4693+ ts_nstack_push (& stack , body );
4694+ while (stack .count > 0 ) {
4695+ TSNode n = ts_nstack_pop (& stack );
4696+ const char * k = ts_node_type (n );
4697+ if (strcmp (k , "function_definition" ) == 0 || strcmp (k , "class_definition" ) == 0 ||
4698+ strcmp (k , "lambda" ) == 0 ) {
4699+ continue ;
4700+ }
4701+ TSNode assign = py_statement_assignment (n );
4702+ if (!ts_node_is_null (assign )) {
4703+ py_init_field_assignment (ctx , class_qn , assign , params , self_name );
4704+ continue ;
4705+ }
4706+ /* Reverse push keeps source order on pop. */
4707+ for (uint32_t i = ts_node_named_child_count (n ); i > 0 ; i -- ) {
4708+ ts_nstack_push (& stack , ts_node_named_child (n , i - 1 ));
4709+ }
4710+ }
4711+ }
4712+
4713+ static void extract_py_field_types (CBMExtractCtx * ctx , TSNode class_node , const char * class_qn ) {
4714+ TSNode body = ts_node_child_by_field_name (class_node , TS_FIELD ("body" ));
4715+ if (ts_node_is_null (body )) {
4716+ return ;
4717+ }
4718+ uint32_t count = ts_node_named_child_count (body );
4719+ /* Class-body annotations first: `x: T` / `x: T = v`. */
4720+ for (uint32_t i = 0 ; i < count ; i ++ ) {
4721+ TSNode assign = py_statement_assignment (ts_node_named_child (body , i ));
4722+ if (ts_node_is_null (assign )) {
4723+ continue ;
4724+ }
4725+ TSNode left = ts_node_child_by_field_name (assign , TS_FIELD ("left" ));
4726+ TSNode ann = ts_node_child_by_field_name (assign , TS_FIELD ("type" ));
4727+ if (!ts_node_is_null (left ) && !ts_node_is_null (ann ) &&
4728+ strcmp (ts_node_type (left ), "identifier" ) == 0 ) {
4729+ py_push_field_type (ctx , class_qn , left , cbm_node_text (ctx -> arena , ann , ctx -> source ));
4730+ }
4731+ }
4732+ /* Then __init__. */
4733+ for (uint32_t i = 0 ; i < count ; i ++ ) {
4734+ TSNode fn = ts_node_named_child (body , i );
4735+ if (strcmp (ts_node_type (fn ), "decorated_definition" ) == 0 ) {
4736+ fn = ts_node_child_by_field_name (fn , TS_FIELD ("definition" ));
4737+ }
4738+ if (ts_node_is_null (fn ) || strcmp (ts_node_type (fn ), "function_definition" ) != 0 ) {
4739+ continue ;
4740+ }
4741+ TSNode fname = ts_node_child_by_field_name (fn , TS_FIELD ("name" ));
4742+ char * fn_name =
4743+ ts_node_is_null (fname ) ? NULL : cbm_node_text (ctx -> arena , fname , ctx -> source );
4744+ if (fn_name && strcmp (fn_name , "__init__" ) == 0 ) {
4745+ py_extract_init_fields (ctx , class_qn , fn );
4746+ }
4747+ }
4748+ }
4749+
45874750static void extract_class_def (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ) {
45884751 CBMArena * a = ctx -> arena ;
45894752 const char * kind = ts_node_type (node );
@@ -4970,6 +5133,10 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
49705133 // Extract class-level variables (field declarations)
49715134 extract_class_variables (ctx , node , class_qn , spec );
49725135
5136+ if (ctx -> language == CBM_LANG_PYTHON ) {
5137+ extract_py_field_types (ctx , node , class_qn );
5138+ }
5139+
49735140 // C# 12 primary-constructor parameters: declared on the class line
49745141 // (`class Foo(IBar bar, IBaz baz) : Base { ... }`) and bound to implicit
49755142 // captured fields accessible from any instance member. Tree-sitter c-sharp
0 commit comments