Skip to content

Commit

Permalink
Merge pull request #25 from Ricardaux/master
Browse files Browse the repository at this point in the history
Add optional parsing
  • Loading branch information
Ricardaux authored Jul 19, 2024
2 parents 91adcc2 + ed6a6da commit 63ec4d9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/pyckson/parsers/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from decimal import Decimal
from enum import Enum

from pyckson.const import BASIC_TYPES


class ParserException(Exception):
pass

Expand All @@ -20,7 +23,7 @@ def __init__(self, cls):
self.cls = cls

def parse(self, json_value):
if not isinstance(json_value, self.cls):
if not any(isinstance(json_value, basic_type) for basic_type in BASIC_TYPES):
raise ParserException(f'"{json_value}" is supposed to be a {self.cls.__name__}.')
return self.cls(json_value)

Expand Down
2 changes: 1 addition & 1 deletion tests/parsers/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_should_handle_simple_type(self):
def test_should_raise_when_it_is_not_the_correct_type(self):
parser = BasicParserWithCast(str)

assert_that(parser.parse).raises(ParserException).when_called_with(5)
assert_that(parser.parse).raises(ParserException).when_called_with(['yo'])


class TestUnionParser:
Expand Down

0 comments on commit 63ec4d9

Please sign in to comment.