11from typing import Annotated , Any
22
3- from pydantic import BaseModel , ConfigDict , PlainSerializer , PlainValidator , WithJsonSchema
3+ from pydantic import BaseModel , ConfigDict , PlainValidator , WithJsonSchema
4+ from pydantic .json_schema import JsonSchemaValue
45from pydantic_core .core_schema import ValidationInfo
56from tree_sitter import Point , Range
67
@@ -21,50 +22,54 @@ def validate_range(value: Any, info: ValidationInfo) -> Range:
2122 return value
2223
2324
24- RangeAdapter = Annotated [
25- Range ,
26- PlainValidator (validate_range ),
27- PlainSerializer (
28- lambda range : {
29- "start_byte" : range .start_byte ,
30- "end_byte" : range .end_byte ,
25+ def range_json_schema () -> JsonSchemaValue :
26+ return {
27+ "type" : "object" ,
28+ "properties" : {
29+ "start_byte" : {"type" : "integer" },
30+ "end_byte" : {"type" : "integer" },
3131 "start_point" : {
32- "row" : range .start_point .row ,
33- "column" : range .start_point .column ,
32+ "type" : "object" ,
33+ "properties" : {
34+ "row" : {"type" : "integer" },
35+ "column" : {"type" : "integer" },
36+ },
3437 },
3538 "end_point" : {
36- "row" : range .end_point .row ,
37- "column" : range .end_point .column ,
38- },
39- }
40- ),
41- WithJsonSchema (
42- {
43- "type" : "object" ,
44- "properties" : {
45- "start_byte" : {"type" : "integer" },
46- "end_byte" : {"type" : "integer" },
47- "start_point" : {
48- "type" : "object" ,
49- "properties" : {
50- "row" : {"type" : "integer" },
51- "column" : {"type" : "integer" },
52- },
53- },
54- "end_point" : {
55- "type" : "object" ,
56- "properties" : {"row" : {"type" : "integer" }, "column" : {"type" : "integer" }},
57- },
39+ "type" : "object" ,
40+ "properties" : {"row" : {"type" : "integer" }, "column" : {"type" : "integer" }},
5841 },
59- }
60- ),
42+ },
43+ }
44+
45+
46+ RangeAdapter = Annotated [
47+ Range ,
48+ PlainValidator (validate_range ),
49+ WithJsonSchema (range_json_schema ()),
6150]
6251
6352
6453@apidoc
6554class Span (BaseModel ):
6655 """Range within the codebase"""
6756
68- model_config = ConfigDict (frozen = True )
57+ model_config = ConfigDict (
58+ frozen = True ,
59+ json_encoders = {
60+ Range : lambda r : {
61+ "start_byte" : r .start_byte ,
62+ "end_byte" : r .end_byte ,
63+ "start_point" : {
64+ "row" : r .start_point .row ,
65+ "column" : r .start_point .column ,
66+ },
67+ "end_point" : {
68+ "row" : r .end_point .row ,
69+ "column" : r .end_point .column ,
70+ },
71+ }
72+ },
73+ )
6974 range : RangeAdapter
7075 filepath : str
0 commit comments