-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve NewTypes * Tweak tests * NewType fix * Improve docs
- Loading branch information
Showing
9 changed files
with
113 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,3 +282,4 @@ | |
"from enum import Enum, unique" | ||
) | ||
autodoc_typehints = "description" | ||
autosectionlabel_prefix_document = True |
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
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
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,57 @@ | ||
"""Tests for NewTypes.""" | ||
from typing import NewType | ||
|
||
import pytest | ||
|
||
from cattrs import BaseConverter | ||
|
||
PositiveIntNewType = NewType("PositiveIntNewType", int) | ||
BigPositiveIntNewType = NewType("BigPositiveIntNewType", PositiveIntNewType) | ||
|
||
|
||
def test_newtype_structure_hooks(genconverter: BaseConverter): | ||
"""NewTypes should work with `register_structure_hook`.""" | ||
|
||
assert genconverter.structure("0", int) == 0 | ||
assert genconverter.structure("0", PositiveIntNewType) == 0 | ||
assert genconverter.structure("0", BigPositiveIntNewType) == 0 | ||
|
||
genconverter.register_structure_hook( | ||
PositiveIntNewType, lambda v, _: int(v) if int(v) > 0 else 1 / 0 | ||
) | ||
|
||
with pytest.raises(ZeroDivisionError): | ||
genconverter.structure("0", PositiveIntNewType) | ||
|
||
assert genconverter.structure("1", PositiveIntNewType) == 1 | ||
|
||
with pytest.raises(ZeroDivisionError): | ||
genconverter.structure("0", BigPositiveIntNewType) | ||
|
||
genconverter.register_structure_hook( | ||
BigPositiveIntNewType, lambda v, _: int(v) if int(v) > 50 else 1 / 0 | ||
) | ||
|
||
with pytest.raises(ZeroDivisionError): | ||
genconverter.structure("1", BigPositiveIntNewType) | ||
|
||
assert genconverter.structure("1", PositiveIntNewType) == 1 | ||
assert genconverter.structure("51", BigPositiveIntNewType) == 51 | ||
|
||
|
||
def test_newtype_unstructure_hooks(genconverter: BaseConverter): | ||
"""NewTypes should work with `register_unstructure_hook`.""" | ||
|
||
assert genconverter.unstructure(0, int) == 0 | ||
assert genconverter.unstructure(0, PositiveIntNewType) == 0 | ||
assert genconverter.unstructure(0, BigPositiveIntNewType) == 0 | ||
|
||
genconverter.register_unstructure_hook(PositiveIntNewType, oct) | ||
|
||
assert genconverter.unstructure(0, PositiveIntNewType) == "0o0" | ||
assert genconverter.unstructure(0, BigPositiveIntNewType) == "0o0" | ||
|
||
genconverter.register_unstructure_hook(BigPositiveIntNewType, hex) | ||
|
||
assert genconverter.unstructure(0, PositiveIntNewType) == "0o0" | ||
assert genconverter.unstructure(0, BigPositiveIntNewType) == "0x0" |