Last active
June 3, 2023 14:03
-
-
Save gidgid/f44f72622957294efa11e20a6fa4f999 to your computer and use it in GitHub Desktop.
Leveraging validate_arguments to perform validations
This file contains hidden or 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
import pytest | |
from typing import Optional | |
from pydantic import validate_arguments, PositiveInt, ValidationError | |
@validate_arguments # 1 | |
def validated_get_payload(url: str, retries: PositiveInt) -> Optional[dict]: | |
if url.startswith("http"): | |
# send the actual request and return the payload if valid response | |
return {} | |
return None | |
@pytest.mark.parametrize( | |
"url,retries", | |
[([1, 2], 3), (4, 'wrong_args'), ("http://fine.url.com", -4)] # 2 | |
) | |
def test_validated_get_payload_raises_a_validation_error(url, retries): | |
with pytest.raises(ValidationError): # 3 | |
validated_get_payload(url=url, retries=retries) # 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are absolutely correct about the missing import. Thanks for pointing it out
And I appreciate the kind feedback :)