Skip to content

Update span to use v2 pydantic #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions src/codegen/sdk/codebase/span.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Annotated, Any

from pydantic import BaseModel, ConfigDict, PlainSerializer, PlainValidator, WithJsonSchema
from pydantic import BaseModel, ConfigDict, PlainValidator, WithJsonSchema
from pydantic.json_schema import JsonSchemaValue
from pydantic_core.core_schema import ValidationInfo
from tree_sitter import Point, Range

Expand All @@ -21,50 +22,54 @@ def validate_range(value: Any, info: ValidationInfo) -> Range:
return value


RangeAdapter = Annotated[
Range,
PlainValidator(validate_range),
PlainSerializer(
lambda range: {
"start_byte": range.start_byte,
"end_byte": range.end_byte,
def range_json_schema() -> JsonSchemaValue:
return {
"type": "object",
"properties": {
"start_byte": {"type": "integer"},
"end_byte": {"type": "integer"},
"start_point": {
"row": range.start_point.row,
"column": range.start_point.column,
"type": "object",
"properties": {
"row": {"type": "integer"},
"column": {"type": "integer"},
},
},
"end_point": {
"row": range.end_point.row,
"column": range.end_point.column,
},
}
),
WithJsonSchema(
{
"type": "object",
"properties": {
"start_byte": {"type": "integer"},
"end_byte": {"type": "integer"},
"start_point": {
"type": "object",
"properties": {
"row": {"type": "integer"},
"column": {"type": "integer"},
},
},
"end_point": {
"type": "object",
"properties": {"row": {"type": "integer"}, "column": {"type": "integer"}},
},
"type": "object",
"properties": {"row": {"type": "integer"}, "column": {"type": "integer"}},
},
}
),
},
}


RangeAdapter = Annotated[
Range,
PlainValidator(validate_range),
WithJsonSchema(range_json_schema()),
]


@apidoc
class Span(BaseModel):
"""Range within the codebase"""

model_config = ConfigDict(frozen=True)
model_config = ConfigDict(
frozen=True,
json_encoders={
Range: lambda r: {
"start_byte": r.start_byte,
"end_byte": r.end_byte,
"start_point": {
"row": r.start_point.row,
"column": r.start_point.column,
},
"end_point": {
"row": r.end_point.row,
"column": r.end_point.column,
},
}
},
)
range: RangeAdapter
filepath: str
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_api_doc_generation_sanity(codebase, language: ProgrammingLanguage) -> N
# assert "InviteFactoryCreateParams" in docs # Canonicals aren't in docs


@pytest.mark.timeout(120)
@pytest.mark.timeout(160)
@pytest.mark.xdist_group("codegen")
def test_mdx_api_doc_generation_sanity(codebase) -> None:
docs_json = generate_docs_json(codebase, "HEAD")
Expand Down