Skip to content

Commit e5e2e73

Browse files
committed
feat: add support for deprecated type syntax
Signed-off-by: Louis Mandel <[email protected]>
1 parent 27bb0ea commit e5e2e73

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

src/pdl/pdl_ast.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,20 @@ class AnyPattern(Pattern):
154154

155155

156156
BasePdlType: TypeAlias = Literal[
157-
"null", "boolean", "string", "number", "integer", "array", "object"
157+
"null",
158+
"boolean",
159+
"string",
160+
"number",
161+
"integer",
162+
"array",
163+
"object",
164+
# deprecated syntax below
165+
"bool",
166+
"str",
167+
"float",
168+
"int",
169+
"list",
170+
"obj",
158171
]
159172
"""Base types."""
160173

src/pdl/pdl_dumper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
TextBlock,
5959
)
6060
from .pdl_lazy import PdlLazy
61+
from .pdl_schema_utils import OLD_PDLTYPE_TO_JSONSCHEMA_NAME
6162

6263
yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str # type: ignore
6364

@@ -327,6 +328,20 @@ def type_to_dict(t: PdlTypeType):
327328
d = None
328329
case "null" | "boolean" | "string" | "number" | "integer" | "array" | "object":
329330
d = t
331+
case "bool" | "str" | "float" | "int" | "list" | "obj":
332+
d = OLD_PDLTYPE_TO_JSONSCHEMA_NAME[t]
333+
case "bool":
334+
d = "boolean"
335+
case "str":
336+
d = "string"
337+
case "float":
338+
d = "number"
339+
case "int":
340+
d = "integer"
341+
case "list":
342+
d = "array"
343+
case "obj":
344+
d = "object"
330345
case EnumPdlType():
331346
d = t.model_dump()
332347
case [elem]:

src/pdl/pdl_schema_error_analyzer.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
def is_base_type(schema):
77
if "type" in schema:
88
the_type = schema["type"]
9-
if the_type in set(["string", "boolean", "integer", "number"]):
9+
if the_type in set(
10+
[
11+
"null",
12+
"string",
13+
"boolean",
14+
"integer",
15+
"number",
16+
"str",
17+
"bool",
18+
"int",
19+
"float",
20+
]
21+
):
1022
return True
1123
if "enum" in schema:
1224
return True
@@ -61,7 +73,11 @@ def analyze_errors(defs, schema, data, loc: PdlLocationType) -> list[str]: # no
6173
if is_base_type(schema):
6274
if "type" in schema:
6375
the_type = json_types_convert[schema["type"]]
64-
if not isinstance(data, the_type): # pyright: ignore
76+
if (
77+
the_type is None
78+
and data is not None
79+
or not isinstance(data, the_type) # type: ignore
80+
):
6581
ret.append(
6682
get_loc_string(loc)
6783
+ str(data)

src/pdl/pdl_schema_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import warnings
23
from typing import Any, Optional
34

@@ -10,12 +11,19 @@
1011
)
1112

1213
json_types_convert = {
14+
"null": None,
1315
"string": str,
1416
"boolean": bool,
1517
"integer": int,
1618
"number": float,
1719
"array": list,
1820
"object": dict,
21+
"str": str,
22+
"bool": bool,
23+
"int": int,
24+
"float": float,
25+
"list": list,
26+
"obj": dict,
1927
}
2028

2129

@@ -26,6 +34,16 @@ def convert_to_json_type(a_type):
2634
return None
2735

2836

37+
OLD_PDLTYPE_TO_JSONSCHEMA_NAME = {
38+
"bool": "boolean",
39+
"str": "string",
40+
"float": "number",
41+
"int": "integer",
42+
"list": "array",
43+
"obj": "object",
44+
}
45+
46+
2947
def pdltype_to_jsonschema(
3048
pdl_type: PdlTypeType, additional_properties: bool
3149
) -> dict[str, Any]:
@@ -35,6 +53,12 @@ def pdltype_to_jsonschema(
3553
schema = {} # Any type
3654
case "null" | "boolean" | "string" | "number" | "integer" | "array" | "object":
3755
schema = {"type": pdl_type}
56+
case "bool" | "str" | "float" | "int" | "list" | "obj":
57+
print(
58+
f"Deprecated type syntax: use {OLD_PDLTYPE_TO_JSONSCHEMA_NAME[pdl_type]} instead of {pdl_type}.",
59+
file=sys.stderr,
60+
)
61+
schema = {"type": OLD_PDLTYPE_TO_JSONSCHEMA_NAME[pdl_type]}
3862
case EnumPdlType(enum=choices):
3963
if pdl_type.__pydantic_extra__ is None:
4064
extra = {}

tests/test_type_checking.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import yaml
33

4-
from pdl.pdl import exec_dict
4+
from pdl.pdl import exec_dict, exec_str
55
from pdl.pdl_ast import pdl_type_adapter
66
from pdl.pdl_interpreter import PDLRuntimeError
77
from pdl.pdl_parser import PDLParseError
@@ -587,3 +587,21 @@ def test_hello1():
587587
def test_hello2():
588588
with pytest.raises(PDLRuntimeError):
589589
exec_dict(hello2)
590+
591+
592+
def do_test_stderr(capsys, prog, err):
593+
exec_str(prog)
594+
captured = capsys.readouterr()
595+
output = captured.err.split("\n")
596+
print(output)
597+
assert set(output) == set(err)
598+
599+
600+
def test_deprecated(capsys: pytest.CaptureFixture[str]):
601+
prog = """
602+
data: 1
603+
spec: int
604+
"""
605+
do_test_stderr(
606+
capsys, prog, ["Deprecated type syntax: use integer instead of int.", ""]
607+
)

0 commit comments

Comments
 (0)