Skip to content

Python dataclass field() introspection and field-level metadata not extracted #2057

Description

@Widthdom

Summary

Python @dataclass classes can define field metadata via field(default=..., metadata={...}), and code can introspect fields via dataclasses.fields(MyClass). While SymbolExtractor detects class attributes via annotation/assignment (lines 184-282 of SymbolExtractor.Python.cs), it does not model field() calls as distinct symbols, nor does it capture references to field metadata keys. Dataclass field introspection is also not registered as a reference to the dataclass itself (unlike the regex on line 102 for dataclasses.fields()). Field-level type constraints in Callable and Protocol parameterized fields are invisible.

Where

  • src/CodeIndex/Indexer/Symbols/SymbolExtractor.Python.cs:184-282 — ExtractPythonClassAttributeSymbols processes annotated/assigned attributes but does not distinguish field() construction
  • src/CodeIndex/Indexer/References/Languages/PythonReferenceExtractor.cs:102-104 — DataclassesFieldsTargetRegex exists but only matches dataclasses.fields() call sites, not the field() definitions themselves

Suggested approach

  1. Add regex to detect field(...) calls in class body, extracting the field name and metadata dict keys
  2. Register each field() metadata key as a symbol or annotation reference
  3. Extend reference extraction to cover field default factories (e.g., field(default_factory=list) → extract list as a reference)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions