-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support
dataclasses.InitVar
for python>=3.8 (#171)
- Loading branch information
1 parent
e06b016
commit bffb1b8
Showing
5 changed files
with
99 additions
and
5 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
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,34 @@ | ||
from dataclasses import dataclass, InitVar | ||
import sys | ||
from typing import Any, List, Tuple, Type | ||
|
||
import pytest | ||
from typing_extensions import Literal | ||
|
||
from .testutils import TestSetup | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.version_info[:2] < (3, 8), | ||
reason="Before 3.8 `InitVar[tp] is InitVar` so it's impossible to retrieve field type", | ||
) | ||
@pytest.mark.parametrize( | ||
'tp, passed_value, expected', | ||
[ | ||
(int, '1', 1), | ||
(float, '1.4', 1.4), | ||
(Tuple[int, float], '2 -1.2', (2, -1.2)), | ||
(List[str], '12 abc', ['12', 'abc']), | ||
(Literal[1, 2, 3, '4'], '1', 1), | ||
(Literal[1, 2, 3, '4'], '4', '4'), | ||
], | ||
) | ||
def test_initvar(tp: Type[Any], passed_value: str, expected: Any) -> None: | ||
@dataclass | ||
class Foo(TestSetup): | ||
init_var: InitVar[tp] | ||
|
||
def __post_init__(self, init_var: tp) -> None: | ||
assert init_var == expected | ||
|
||
Foo.setup(f"--init_var {passed_value}") |