Skip to content

Is there a way to use pydantic manually (without BaseModel) #2556

Answered by PrettyWood
erezinman asked this question in Question
Discussion options

You must be logged in to vote

Hi @erezinman
If you want to keep the default behaviour of pydantic with coercion, there you can use the parse_obj_as util

from pydantic import ValidationError, parse_obj_as

def is_valid(value, type_):
    try:
        parse_obj_as(type_, value)
        return True
    except ValidationError:
        return False

assert is_valid((1, 'q'), list[str])

If you want strictness, for now it's probably easier to use another lib like https://github.com/PrettyWood/typingx. Note there are probably better libs out there than mine.

Hope it helps

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@erezinman
Comment options

Answer selected by erezinman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants