Skip to content

Commit dffec08

Browse files
authored
Merge pull request DeusData#2498 from DeusData/fix/issue-1277-py-crossfile-fields-v2
fix(python): retain typed fields across file boundaries
2 parents 3a26acf + bf2daa3 commit dffec08

12 files changed

Lines changed: 823 additions & 3 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ void cbm_typeassign_push(CBMTypeAssignArray *arr, CBMArena *a, CBMTypeAssign ta)
243243
arr->items[arr->count++] = ta;
244244
}
245245

246+
void cbm_fieldtype_push(CBMFieldTypeArray *arr, CBMArena *a, CBMFieldType ft) {
247+
GROW_ARRAY(arr, a);
248+
arr->items[arr->count++] = ft;
249+
}
250+
246251
void cbm_stringref_push(CBMStringRefArray *arr, CBMArena *a, CBMStringRef sr) {
247252
GROW_ARRAY(arr, a);
248253
arr->items[arr->count++] = sr;

‎internal/cbm/cbm.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ typedef struct {
409409
CBMChannelDirection direction;
410410
} CBMChannel;
411411

412+
/* Python: one annotated instance field of a class -- `x: T` or `x: T = v` in
413+
* the class body, `self.x: T = v` in __init__, or `self.x = p` where `p` is an
414+
* annotated __init__ parameter. Not a graph node: it only carries the field's
415+
* declared type to the cross-file LSP, so `obj.x.m()` on a class imported
416+
* from another file can be typed (#1277). */
417+
typedef struct {
418+
const char *class_qn; // QN of the owning class
419+
const char *field_name; // attribute name
420+
const char *type_text; // raw annotation text, resolved later per file
421+
} CBMFieldType;
422+
412423
// Rust: impl Trait for Struct
413424
typedef struct {
414425
const char *trait_name; // trait name (raw text)
@@ -526,6 +537,12 @@ typedef struct {
526537
int cap;
527538
} CBMChannelArray;
528539

540+
typedef struct {
541+
CBMFieldType *items;
542+
int count;
543+
int cap;
544+
} CBMFieldTypeArray;
545+
529546
// Full extraction result for one file.
530547
typedef struct CBMFileResult {
531548
CBMArena arena; // owns local memory; composites may also retain child arenas below
@@ -544,6 +561,7 @@ typedef struct CBMFileResult {
544561
CBMStringRefArray string_refs; // URL/config string literals from AST
545562
CBMInfraBindingArray infra_bindings; // topic→URL pairs from IaC configs
546563
CBMChannelArray channels; // Socket.IO / EventEmitter pub/sub participation
564+
CBMFieldTypeArray field_types; // Python: annotated instance fields (#1277)
547565

548566
const char *module_qn; // module qualified name
549567
const char *namespace_name; // declared namespace/package (Java/Kotlin/C#/PHP), NULL if none
@@ -900,6 +918,7 @@ void cbm_rw_push(CBMRWArray *arr, CBMArena *a, CBMReadWrite rw);
900918
void cbm_typerefs_push(CBMTypeRefArray *arr, CBMArena *a, CBMTypeRef tr);
901919
void cbm_envaccess_push(CBMEnvAccessArray *arr, CBMArena *a, CBMEnvAccess ea);
902920
void cbm_typeassign_push(CBMTypeAssignArray *arr, CBMArena *a, CBMTypeAssign ta);
921+
void cbm_fieldtype_push(CBMFieldTypeArray *arr, CBMArena *a, CBMFieldType ft);
903922
void cbm_stringref_push(CBMStringRefArray *arr, CBMArena *a, CBMStringRef sr);
904923
void cbm_infrabinding_push(CBMInfraBindingArray *arr, CBMArena *a, CBMInfraBinding ib);
905924
void cbm_impltrait_push(CBMImplTraitArray *arr, CBMArena *a, CBMImplTrait it);

‎internal/cbm/extract_defs.c‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
45874750
static 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

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4965,6 +4965,47 @@ static const char **py_split_pipe(CBMArena *arena, const char *text) {
49654965
return out;
49664966
}
49674967

4968+
/* Split a class def's field_defs ("name:QN|name:QN", built by the Python
4969+
* field fold in pass_lsp_cross.c) into the parallel NULL-terminated arrays a
4970+
* CBMRegisteredType carries. The types are already project QNs, so each one
4971+
* becomes a NAMED type verbatim. Without this, `obj.field.method()` on a class
4972+
* from another file lost the field's type (#1277); same-file classes get their
4973+
* fields from the per-file walk instead. Pure string work: no registry lookup
4974+
* happens here, so it is safe inside the type-registration pass. */
4975+
static void py_split_field_defs(CBMArena *arena, const char *field_defs, const char ***names_out,
4976+
const CBMType ***types_out) {
4977+
*names_out = NULL;
4978+
*types_out = NULL;
4979+
const char **pairs = py_split_pipe(arena, field_defs);
4980+
if (!pairs)
4981+
return;
4982+
int n = 0;
4983+
while (pairs[n])
4984+
n++;
4985+
const char **names =
4986+
(const char **)cbm_arena_alloc(arena, (size_t)(n + 1) * sizeof(const char *));
4987+
const CBMType **types =
4988+
(const CBMType **)cbm_arena_alloc(arena, (size_t)(n + 1) * sizeof(const CBMType *));
4989+
if (!names || !types)
4990+
return;
4991+
int kept = 0;
4992+
for (int i = 0; i < n; i++) {
4993+
char *colon = strchr(pairs[i], ':');
4994+
if (!colon || colon == pairs[i] || !colon[1])
4995+
continue;
4996+
*colon = '\0'; /* pairs[i] is this call's own arena copy */
4997+
names[kept] = pairs[i];
4998+
types[kept] = cbm_type_named(arena, colon + 1);
4999+
kept++;
5000+
}
5001+
if (kept == 0)
5002+
return;
5003+
names[kept] = NULL;
5004+
types[kept] = NULL;
5005+
*names_out = names;
5006+
*types_out = types;
5007+
}
5008+
49685009
/* Build a registry from CBMLSPDef[] supplied by the caller — covers both
49695010
* the source file's own defs and cross-file referenced defs. */
49705011
static void py_register_lsp_defs(CBMArena *arena, CBMArena *idx_arena, CBMTypeRegistry *reg,
@@ -4990,6 +5031,7 @@ static void py_register_lsp_defs(CBMArena *arena, CBMArena *idx_arena, CBMTypeRe
49905031
if (d->method_names_str && d->method_names_str[0]) {
49915032
rt.method_names = py_split_pipe(arena, d->method_names_str);
49925033
}
5034+
py_split_field_defs(arena, d->field_defs, &rt.field_names, &rt.field_types);
49935035
cbm_registry_add_type(reg, rt);
49945036
}
49955037
}

‎internal/cbm/lsp/type_registry.c‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,28 @@ CBMRegisteredType *cbm_registry_type_for_update(CBMTypeRegistry *head, const cha
677677
for (const CBMTypeRegistry *r = head->fallback; r; r = r->fallback) {
678678
const CBMRegisteredType *base = lookup_type_self(r, qualified_name);
679679
if (base) {
680+
CBMRegisteredType copy = *base;
681+
/* Field refinements overwrite entries in this array. The type
682+
* objects remain immutable, but the writable overlay must own
683+
* the pointer array rather than mutate its shared fallback. */
684+
if (base->field_types) {
685+
size_t count = 0;
686+
/* Types are parallel to names; a named field may have no
687+
* type, so an interior NULL does not terminate that array. */
688+
while (base->field_names ? base->field_names[count] != NULL
689+
: base->field_types[count] != NULL) {
690+
count++;
691+
}
692+
const CBMType **field_types = (const CBMType **)cbm_arena_alloc(
693+
head->arena, (count + 1) * sizeof(*field_types));
694+
if (!field_types) {
695+
return NULL;
696+
}
697+
memcpy(field_types, base->field_types, (count + 1) * sizeof(*field_types));
698+
copy.field_types = field_types;
699+
}
680700
int before = head->type_count;
681-
cbm_registry_add_type(head, *base);
701+
cbm_registry_add_type(head, copy);
682702
if (head->type_count == before + 1) {
683703
return &head->types[before];
684704
}

‎internal/cbm/result_compact.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ static void cr_walk(cr_ctx_t *c, CBMFileResult *r) {
378378
cr_str(c, &r->channels.items[i].transport);
379379
cr_str(c, &r->channels.items[i].enclosing_func_qn);
380380
}
381+
cr_array(c, (void **)&r->field_types.items, r->field_types.count, sizeof(CBMFieldType));
382+
for (int i = 0; i < r->field_types.count && r->field_types.items; i++) {
383+
cr_str(c, &r->field_types.items[i].class_qn);
384+
cr_str(c, &r->field_types.items[i].field_name);
385+
cr_str(c, &r->field_types.items[i].type_text);
386+
}
381387
cr_str(c, &r->module_qn);
382388
cr_str(c, &r->namespace_name);
383389
cr_list(c, &r->exports);
@@ -524,6 +530,7 @@ void cbm_result_compact(CBMFileResult *result) {
524530
tmp.string_refs.cap = tmp.string_refs.count;
525531
tmp.infra_bindings.cap = tmp.infra_bindings.count;
526532
tmp.channels.cap = tmp.channels.count;
533+
tmp.field_types.cap = tmp.field_types.count;
527534

528535
/* A composite kept its per-unit results only so shallow-copied strings
529536
* stayed valid; every string is now a copy of its own. */

0 commit comments

Comments
 (0)