-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from oxan/pep695
PEP 695 support
- Loading branch information
Showing
5 changed files
with
106 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import unittest | ||
import sys | ||
|
||
|
||
def load_tests(loader: unittest.TestLoader, tests, pattern): | ||
# Manually load tests to avoid loading tests with syntax that's incompatible with the current Python version | ||
for module in ( | ||
'test_field_generation', | ||
'test_field_utils', | ||
'test_fields', | ||
'test_functional', | ||
'test_issues', | ||
'test_serializers', | ||
'test_typing_utils', | ||
): | ||
tests.addTests(loader.loadTestsFromName('tests.' + module)) | ||
|
||
if sys.version_info >= (3, 12, 0): | ||
tests.addTests(loader.loadTestsFromName('tests.test_py312')) | ||
|
||
return tests |
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,35 @@ | ||
import typing | ||
import unittest | ||
import sys | ||
|
||
from rest_framework_dataclasses import typing_utils | ||
|
||
|
||
@unittest.skipIf(sys.version_info < (3, 12, 0), 'Python 3.12 required') | ||
class Python312Test(unittest.TestCase): | ||
def test_resolve_pep695(self): | ||
type Str = str | ||
type StrList = list[str] | ||
type GenericList[T] = list[T] | ||
|
||
class Hinted: | ||
a: Str | ||
b: StrList | ||
c: GenericList | ||
|
||
hints = typing_utils.get_resolved_type_hints(Hinted) | ||
self.assertEqual(hints['a'], str) | ||
self.assertEqual(hints['b'], list[str]) | ||
self.assertEqual(typing.get_origin(hints['c']), list) | ||
|
||
def test_typevar_pep695(self): | ||
type GenericList[T: str] = list[T] | ||
def fn() -> GenericList: | ||
pass | ||
|
||
tp = typing_utils.get_resolved_type_hints(fn)['return'] | ||
|
||
self.assertTrue(typing_utils.is_iterable_type(tp)) | ||
element_type = typing_utils.get_iterable_element_type(tp) | ||
self.assertTrue(typing_utils.is_type_variable(element_type)) | ||
self.assertEqual(typing_utils.get_type_variable_substitution(element_type), str) |
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