Skip to content

Commit ef1b56e

Browse files
committed
Clean up logic for making fields nullable in v1, also clean up how tests are run against different versions
1 parent 1ac8244 commit ef1b56e

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

pydantic2ts/cli/script.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,13 @@ def _clean_json_schema(schema: Dict[str, Any], model: Any = None) -> None:
177177

178178
if _is_v1_model(model):
179179
fields: List["ModelField"] = list(model.__fields__.values())
180-
for field in fields:
180+
fields_that_should_be_nullable = [f for f in fields if f.allow_none]
181+
for field in fields_that_should_be_nullable:
181182
try:
182-
if not field.allow_none:
183-
continue
184183
name = field.alias
185-
prop = properties.get(name)
186-
if prop is None:
187-
continue
188-
if _is_nullable(prop):
189-
continue
190-
properties[name] = {"anyOf": [prop, {"type": "null"}]}
184+
prop = properties.get(field.alias)
185+
if prop and not _is_nullable(prop):
186+
properties[name] = {"anyOf": [prop, {"type": "null"}]}
191187
except Exception:
192188
LOG.error(
193189
f"Failed to ensure nullability for field {field.alias}.",

tests/test_script.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
from pydantic2ts.cli.script import parse_cli_args
1111
from pydantic2ts.pydantic_v2 import enabled as v2_enabled
1212

13-
_PYDANTIC_VERSIONS = ("v1", "v2") if v2_enabled else ("v1",)
13+
_PYDANTIC_VERSIONS = (1, 2) if v2_enabled else (1,)
1414
_RESULTS_DIRECTORY = Path(
1515
os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected_results")
1616
)
1717

1818

19-
def _get_input_module(test_name: str, pydantic_version: str) -> str:
20-
return str(_RESULTS_DIRECTORY / test_name / pydantic_version / "input.py")
19+
def _get_input_module(test_name: str, pydantic_version: int) -> str:
20+
return str(_RESULTS_DIRECTORY / test_name / f"v{pydantic_version}" / "input.py")
2121

2222

23-
def _get_expected_output(test_name: str, pydantic_version: str) -> str:
24-
return (_RESULTS_DIRECTORY / test_name / pydantic_version / "output.ts").read_text()
23+
def _get_expected_output(test_name: str, pydantic_version: int) -> str:
24+
return (_RESULTS_DIRECTORY / test_name / f"v{pydantic_version}" / "output.ts").read_text()
2525

2626

2727
def _run_test(
2828
tmp_path: Path,
2929
test_name: str,
30-
pydantic_version: str,
30+
pydantic_version: int,
3131
*,
3232
module_path: Optional[str] = None,
3333
call_from_python: bool = False,
@@ -38,7 +38,7 @@ def _run_test(
3838
Compare the output with the expected output, verifying it is identical.
3939
"""
4040
module_path = module_path or _get_input_module(test_name, pydantic_version)
41-
output_path = str(tmp_path / f"{test_name}_{pydantic_version}.ts")
41+
output_path = str(tmp_path / f"{test_name}_v{pydantic_version}.ts")
4242

4343
if call_from_python:
4444
generate_typescript_defs(module_path, output_path, exclude)
@@ -55,31 +55,31 @@ def _run_test(
5555
"pydantic_version, call_from_python",
5656
product(_PYDANTIC_VERSIONS, [False, True]),
5757
)
58-
def test_single_module(tmp_path: Path, pydantic_version: str, call_from_python: bool):
58+
def test_single_module(tmp_path: Path, pydantic_version: int, call_from_python: bool):
5959
_run_test(tmp_path, "single_module", pydantic_version, call_from_python=call_from_python)
6060

6161

6262
@pytest.mark.parametrize(
6363
"pydantic_version, call_from_python",
6464
product(_PYDANTIC_VERSIONS, [False, True]),
6565
)
66-
def test_submodules(tmp_path: Path, pydantic_version: str, call_from_python: bool):
66+
def test_submodules(tmp_path: Path, pydantic_version: int, call_from_python: bool):
6767
_run_test(tmp_path, "submodules", pydantic_version, call_from_python=call_from_python)
6868

6969

7070
@pytest.mark.parametrize(
7171
"pydantic_version, call_from_python",
7272
product(_PYDANTIC_VERSIONS, [False, True]),
7373
)
74-
def test_generics(tmp_path: Path, pydantic_version: str, call_from_python: bool):
74+
def test_generics(tmp_path: Path, pydantic_version: int, call_from_python: bool):
7575
_run_test(tmp_path, "generics", pydantic_version, call_from_python=call_from_python)
7676

7777

7878
@pytest.mark.parametrize(
7979
"pydantic_version, call_from_python",
8080
product(_PYDANTIC_VERSIONS, [False, True]),
8181
)
82-
def test_excluding_models(tmp_path: Path, pydantic_version: str, call_from_python: bool):
82+
def test_excluding_models(tmp_path: Path, pydantic_version: int, call_from_python: bool):
8383
_run_test(
8484
tmp_path,
8585
"excluding_models",
@@ -91,25 +91,25 @@ def test_excluding_models(tmp_path: Path, pydantic_version: str, call_from_pytho
9191

9292
@pytest.mark.parametrize(
9393
"pydantic_version, call_from_python",
94-
product([v for v in _PYDANTIC_VERSIONS if v != "v1"], [False, True]),
94+
product([v for v in _PYDANTIC_VERSIONS if v > 1], [False, True]),
9595
)
96-
def test_computed_fields(tmp_path: Path, pydantic_version: str, call_from_python: bool):
96+
def test_computed_fields(tmp_path: Path, pydantic_version: int, call_from_python: bool):
9797
_run_test(tmp_path, "computed_fields", pydantic_version, call_from_python=call_from_python)
9898

9999

100100
@pytest.mark.parametrize(
101101
"pydantic_version, call_from_python",
102102
product(_PYDANTIC_VERSIONS, [False, True]),
103103
)
104-
def test_extra_fields(tmp_path: Path, pydantic_version: str, call_from_python: bool):
104+
def test_extra_fields(tmp_path: Path, pydantic_version: int, call_from_python: bool):
105105
_run_test(tmp_path, "extra_fields", pydantic_version, call_from_python=call_from_python)
106106

107107

108108
def test_relative_filepath(tmp_path: Path):
109109
test_name = "single_module"
110110
pydantic_version = _PYDANTIC_VERSIONS[0]
111111
relative_path = (
112-
Path(".") / "tests" / "expected_results" / test_name / pydantic_version / "input.py"
112+
Path(".") / "tests" / "expected_results" / test_name / f"v{pydantic_version}" / "input.py"
113113
)
114114
_run_test(
115115
tmp_path,

0 commit comments

Comments
 (0)