-
Notifications
You must be signed in to change notification settings - Fork 7
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
adding FromCSVToList annotation #42
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6099b1
adding FromCSVToList annotation
benboger c3daad0
dropping support for python 3.7 and 3.8
benboger 884b9ab
updating dependencies and major version
benboger 955963c
adding checks for 3.11
benboger f0adb8e
more cleanup
benboger db34db5
updating pylint and cleaning up errors from newer version
benboger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll call this the y2.1k bug ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:P