-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from tamagoko/csv-annototion-support
adding FromCSVToList annotation
- Loading branch information
Showing
11 changed files
with
138 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.7.2 | ||
3.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Copyright 2023 Adobe | ||
All Rights Reserved. | ||
NOTICE: Adobe permits you to use, modify, and distribute this file in accordance | ||
with the terms of the Adobe license agreement accompanying it. | ||
""" | ||
|
||
from typing import TypeVar, Annotated | ||
|
||
from pydantic import BeforeValidator | ||
|
||
# pylint: disable=invalid-name | ||
T = TypeVar('T') | ||
|
||
|
||
def _transform_csv(value: str) -> T: | ||
if not value: | ||
return None | ||
|
||
if isinstance(value, str): | ||
return list(map(str.strip, value.split(','))) | ||
|
||
if isinstance(value, list): | ||
return value | ||
# if we don't have a string or type T we aren't going to be able to transform it | ||
return [value] | ||
|
||
|
||
# Annotation that helps transform a CSV string into a list of type T | ||
FromCSVToList = Annotated[T, BeforeValidator(_transform_csv)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Copyright 2023 Adobe | ||
All Rights Reserved. | ||
NOTICE: Adobe permits you to use, modify, and distribute this file in accordance | ||
with the terms of the Adobe license agreement accompanying it. | ||
""" | ||
from typing import List, Union | ||
|
||
import pytest | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from dysql.annotations import FromCSVToList | ||
|
||
|
||
class StrCSVModel(BaseModel): | ||
values: FromCSVToList[List[str]] | ||
|
||
|
||
class IntCSVModel(BaseModel): | ||
values: FromCSVToList[List[int]] | ||
|
||
|
||
class NullableStrCSVModel(BaseModel): | ||
values: FromCSVToList[Union[List[str], None]] | ||
|
||
|
||
class NullableIntCSVModel(BaseModel): | ||
values: FromCSVToList[Union[List[int], None]] | ||
|
||
|
||
@pytest.mark.parametrize('cls, values, expected', [ | ||
(StrCSVModel, '1,2,3', ['1', '2', '3']), | ||
(StrCSVModel, 'a,b', ['a', 'b']), | ||
(StrCSVModel, 'a', ['a']), | ||
(NullableStrCSVModel, '', None), | ||
(NullableStrCSVModel, None, None), | ||
(StrCSVModel, ['a', 'b'], ['a', 'b']), | ||
(StrCSVModel, ['a', 'b', 'c'], ['a', 'b', 'c']), | ||
(IntCSVModel, '1,2,3', [1, 2, 3]), | ||
(IntCSVModel, '1', [1]), | ||
(NullableIntCSVModel, '', None), | ||
(NullableIntCSVModel, None, None), | ||
(IntCSVModel, ['1', '2', '3'], [1, 2, 3]), | ||
(IntCSVModel, ['1', '2', '3', 4, 5], [1, 2, 3, 4, 5]) | ||
]) | ||
def test_from_csv_to_list(cls, values, expected): | ||
assert expected == cls(values=values).values | ||
|
||
|
||
@pytest.mark.parametrize('cls, values', [ | ||
(StrCSVModel, ''), | ||
(StrCSVModel, None), | ||
(IntCSVModel, 'a,b,c'), | ||
(IntCSVModel, ''), | ||
(IntCSVModel, None), | ||
(IntCSVModel, ['a', 'b', 'c']), | ||
]) | ||
def test_from_csv_to_list_invalid(cls, values): | ||
with pytest.raises(ValidationError): | ||
cls(values=values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pytest==6.2.4 | ||
pytest-randomly==3.10.1 | ||
pytest-cov==2.12.1 | ||
pylint==2.10.2 | ||
pylint>2.10.2 | ||
pylintfileheader==0.3.0 | ||
pycodestyle==2.8.0 |