-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the bug that a string was unintentionally parsed as a datetime. (…
…#171) Co-authored-by: Ramon <p8u7wAPC5Pg9HYkkCkzA>
- Loading branch information
1 parent
0b4b777
commit 9abbf3a
Showing
5 changed files
with
45 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__title__ = 'jsons' | ||
__version__ = '1.6.2' | ||
__version__ = '1.6.3' | ||
__author__ = 'Ramon Hagenaars' | ||
__author_email__ = '[email protected]' | ||
__description__ = 'For serializing Python objects to JSON (dicts) and back' | ||
|
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,33 @@ | ||
from datetime import datetime | ||
from unittest import TestCase | ||
|
||
import jsons | ||
|
||
|
||
class TestStr(TestCase): | ||
def test_string_is_loaded_as_string(self): | ||
|
||
class C: | ||
def __init__(self, x: str): | ||
self.x = x | ||
|
||
fork = jsons.fork() | ||
jsons.set_deserializer(lambda obj, _, **kwargs: datetime.strptime(obj, '%Y'), datetime, fork_inst=fork) | ||
loaded = jsons.load({'x': '1025'}, C, strict=True, fork_inst=fork) | ||
|
||
self.assertIsInstance(loaded.x, str) | ||
|
||
def test_string_is_loaded_as_datetime(self): | ||
|
||
class C: | ||
def __init__(self, x): | ||
# x has no hint, so the type will be inferred. Since x is not | ||
# explicitly targeted as str, it may get parsed as a datetime. | ||
# And in this test, it should. | ||
self.x = x | ||
|
||
fork = jsons.fork() | ||
jsons.set_deserializer(lambda obj, _, **kwargs: datetime.strptime(obj, '%Y'), datetime, fork_inst=fork) | ||
loaded = jsons.load({'x': '1025'}, C, strict=True, fork_inst=fork) | ||
|
||
self.assertIsInstance(loaded.x, datetime) |