MCPcopy Create free account
hub / github.com/pydantic/pydantic-settings / test_cli_decoding

Function test_cli_decoding

tests/test_source_cli.py:3332–3400  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3330
3331
3332def test_cli_decoding():
3333 PATH_A_STR = str(PureWindowsPath(Path.cwd()))
3334 PATH_B_STR = str(PureWindowsPath(Path.cwd() / 'subdir'))
3335
3336 class PathsDecode(BaseSettings):
3337 path_a: Path = Field(validation_alias=AliasPath('paths', 0))
3338 path_b: Path = Field(validation_alias=AliasPath('paths', 1))
3339 num_a: int = Field(validation_alias=AliasPath('nums', 0))
3340 num_b: int = Field(validation_alias=AliasPath('nums', 1))
3341
3342 assert CliApp.run(
3343 PathsDecode, cli_args=['--paths', PATH_A_STR, '--paths', PATH_B_STR, '--nums', '1', '--nums', '2']
3344 ).model_dump() == {
3345 'path_a': Path(PATH_A_STR),
3346 'path_b': Path(PATH_B_STR),
3347 'num_a': 1,
3348 'num_b': 2,
3349 }
3350
3351 class PathsListNoDecode(BaseSettings):
3352 paths: Annotated[list[Path], NoDecode]
3353 nums: Annotated[list[int], NoDecode]
3354
3355 @field_validator('paths', mode='before')
3356 @classmethod
3357 def decode_path_a(cls, paths: str) -> list[Path]:
3358 return [Path(p) for p in paths.split(',')]
3359
3360 @field_validator('nums', mode='before')
3361 @classmethod
3362 def decode_nums(cls, nums: str) -> list[int]:
3363 return [int(n) for n in nums.split(',')]
3364
3365 assert CliApp.run(
3366 PathsListNoDecode, cli_args=['--paths', f'{PATH_A_STR},{PATH_B_STR}', '--nums', '1,2']
3367 ).model_dump() == {'paths': [Path(PATH_A_STR), Path(PATH_B_STR)], 'nums': [1, 2]}
3368
3369 class PathsAliasNoDecode(BaseSettings):
3370 path_a: Annotated[Path, NoDecode] = Field(validation_alias=AliasPath('paths', 0))
3371 path_b: Annotated[Path, NoDecode] = Field(validation_alias=AliasPath('paths', 1))
3372 num_a: Annotated[int, NoDecode] = Field(validation_alias=AliasPath('nums', 0))
3373 num_b: Annotated[int, NoDecode] = Field(validation_alias=AliasPath('nums', 1))
3374
3375 @model_validator(mode='before')
3376 @classmethod
3377 def intercept_kwargs(cls, data: Any) -> Any:
3378 data['paths'] = [Path(p) for p in data['paths'].split(',')]
3379 data['nums'] = [int(n) for n in data['nums'].split(',')]
3380 return data
3381
3382 assert CliApp.run(
3383 PathsAliasNoDecode, cli_args=['--paths', f'{PATH_A_STR},{PATH_B_STR}', '--nums', '1,2']
3384 ).model_dump() == {
3385 'path_a': Path(PATH_A_STR),
3386 'path_b': Path(PATH_B_STR),
3387 'num_a': 1,
3388 'num_b': 2,
3389 }

Callers

nothing calls this directly

Calls 1

runMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…