Skip to content

Commit

Permalink
Merge pull request #30 from reworkd/refine-custom-pydantic-types-classes
Browse files Browse the repository at this point in the history
Improve code consistency across different types
  • Loading branch information
snshn authored May 31, 2024
2 parents 79fa5ec + 8aef439 commit e565019
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions harambe/parser/type_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class ParserTypeDate:
def __new__(self):
return Annotated[str, AfterValidator(self.validate)]
return Annotated[str, AfterValidator(self.validate_type)]

def validate(date: str):
def validate_type(date: str):
if not isinstance(date, str):
raise ValueError("Wrong input type")

Expand Down
8 changes: 4 additions & 4 deletions harambe/parser/type_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

class ParserTypeEnum:
def __new__(self, variants: List[Enum]):
return Annotated[Enum, AfterValidator(self.validate(variants))]
return Annotated[Enum, AfterValidator(self.validate_type(variants))]

def validate(variants: List[Enum]):
def _validate(value: Enum) -> Enum:
def validate_type(variants: List[Enum]):
def _validate_type(value: Enum) -> Enum:
# Check if the value exists among variants
if value not in variants:
raise ValueError(
f'Value "{value}" doesn\'t match any of the variants ({variants})'
)
return value

return _validate
return _validate_type
4 changes: 2 additions & 2 deletions harambe/parser/type_phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class ParserTypePhoneNumber:
def __new__(self):
return Annotated[str, AfterValidator(self.validate)]
return Annotated[str, AfterValidator(self.validate_type)]

def validate(number: str) -> str:
def validate_type(number: str) -> str:
# Trim whitespaces
number = number.strip()
# Attempt to parse phone number
Expand Down
8 changes: 4 additions & 4 deletions harambe/parser/type_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

class ParserTypeUrl:
def __new__(self, base_url: Optional[URL] = None):
return Annotated[URL, AfterValidator(self.validate_url(base_url))]
return Annotated[URL, AfterValidator(self.validate_type(base_url))]

def validate_url(base_url: Optional[URL]):
def _validate_url(url: URL) -> str:
def validate_type(base_url: Optional[URL]):
def _validate_type(url: URL) -> str:
# Transform relative URLs into absolute using base_url
if base_url is not None:
parsed_base_url = urlparse(base_url, allow_fragments=False)
Expand All @@ -39,4 +39,4 @@ def _validate_url(url: URL) -> str:

return url

return _validate_url
return _validate_type
8 changes: 4 additions & 4 deletions tests/parser/test_type_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"2019-01-01T00:00:00.000+00:00",
],
)
def test_pydantic_type_date_validate_success(date_string):
assert ParserTypeDate.validate(date_string)
def test_pydantic_type_date_validate_type_success(date_string):
assert ParserTypeDate.validate_type(date_string)


@pytest.mark.parametrize(
Expand All @@ -39,6 +39,6 @@ def test_pydantic_type_date_validate_success(date_string):
123, # ❌ Number instead of string
],
)
def test_pydantic_type_date_validate_error(date_string):
def test_pydantic_type_date_validate_type_error(date_string):
with pytest.raises(ValueError):
ParserTypeDate.validate(date_string)
ParserTypeDate.validate_type(date_string)
8 changes: 4 additions & 4 deletions tests/parser/test_type_phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"206-555-7115",
],
)
def test_pydantic_type_phone_number_validate_success(phone_number):
assert ParserTypePhoneNumber.validate(phone_number) == phone_number
def test_pydantic_type_phone_number_validate_type_success(phone_number):
assert ParserTypePhoneNumber.validate_type(phone_number) == phone_number


@pytest.mark.parametrize(
Expand All @@ -33,6 +33,6 @@ def test_pydantic_type_phone_number_validate_success(phone_number):
"415-111-1111 Directions", # ❌ Extra text
],
)
def test_pydantic_type_phone_number_validate_error(phone_number):
def test_pydantic_type_phone_number_validate_type_error(phone_number):
with pytest.raises(ValueError):
ParserTypePhoneNumber.validate(phone_number)
ParserTypePhoneNumber.validate_type(phone_number)
8 changes: 4 additions & 4 deletions tests/parser/test_type_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
),
],
)
def test_pydantic_type_url_validate_url_success(url, base_url, expected):
assert ParserTypeUrl.validate_url(base_url)(url) == expected
def test_pydantic_type_url_validate_type_success(url, base_url, expected):
assert ParserTypeUrl.validate_type(base_url)(url) == expected


@pytest.mark.parametrize(
Expand Down Expand Up @@ -153,6 +153,6 @@ def test_pydantic_type_url_validate_url_success(url, base_url, expected):
),
],
)
def test_pydantic_type_url_validate_url_error(url, base_url):
def test_pydantic_type_url_validate_type_error(url, base_url):
with pytest.raises(ValueError):
ParserTypeUrl.validate_url(base_url)(url)
ParserTypeUrl.validate_type(base_url)(url)

0 comments on commit e565019

Please sign in to comment.