-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds typeddict support, closes #277 * Fixes CI * Fixes CI * Fixes CI * Fixes CI * Fixes CI * Fixes CI * Fixes CI * Fixes CI
- Loading branch information
Showing
6 changed files
with
231 additions
and
30 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,51 @@ | ||
import sys | ||
|
||
import pytest | ||
from typing_extensions import TypedDict | ||
|
||
from classes import typeclass | ||
|
||
if sys.version_info[:2] >= (3, 9): # noqa: C901 | ||
pytestmark = pytest.mark.skip('Only python3.7 and python3.8 are supported') | ||
else: | ||
class _User(TypedDict): | ||
name: str | ||
registered: bool | ||
|
||
class _UserDictMeta(type): | ||
def __instancecheck__(cls, arg: object) -> bool: | ||
return ( | ||
isinstance(arg, dict) and | ||
isinstance(arg.get('name'), str) and | ||
isinstance(arg.get('registered'), bool) | ||
) | ||
|
||
_Meta = type('_Meta', (_UserDictMeta, type(TypedDict)), {}) | ||
|
||
class UserDict(_User, metaclass=_Meta): | ||
"""We use this class to represent a typed dict with instance check.""" | ||
|
||
@typeclass | ||
def get_name(instance) -> str: | ||
"""Example typeclass.""" | ||
|
||
@get_name.instance(delegate=UserDict) | ||
def _get_name_user_dict(instance: UserDict) -> str: | ||
return instance['name'] | ||
|
||
def test_correct_typed_dict(): | ||
"""Ensures that typed dict dispatch works.""" | ||
user: UserDict = {'name': 'sobolevn', 'registered': True} | ||
assert get_name(user) == 'sobolevn' | ||
|
||
@pytest.mark.parametrize('test_value', [ | ||
[], | ||
{}, | ||
{'name': 'sobolevn', 'registered': None}, | ||
{'name': 'sobolevn'}, | ||
{'registered': True}, | ||
]) | ||
def test_wrong_typed_dict(test_value): | ||
"""Ensures that typed dict dispatch works.""" | ||
with pytest.raises(NotImplementedError): | ||
get_name(test_value) |
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 |
---|---|---|
|
@@ -37,6 +37,6 @@ | |
class MyType(AssociatedType): | ||
pass | ||
@typeclass | ||
@typeclass(MyType) | ||
def sum_all(instance) -> int: | ||
pass |
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
121 changes: 121 additions & 0 deletions
121
typesafety/test_typeclass/test_generics/test_typed_dict.yml
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,121 @@ | ||
- case: typeclass_typed_dict1 | ||
disable_cache: false | ||
main: | | ||
from classes import typeclass, AssociatedType, Supports | ||
from typing_extensions import TypedDict | ||
class User(TypedDict): | ||
name: str | ||
registered: bool | ||
class UserDictMeta(type): | ||
def __instancecheck__(cls, arg: object) -> bool: | ||
return ( | ||
isinstance(arg, dict) and | ||
isinstance(arg.get('name'), str) and | ||
isinstance(arg.get('registered'), bool) | ||
) | ||
UserMeta = type('UserMeta', (UserDictMeta, type(TypedDict)), {}) | ||
class UserDict(User, metaclass=UserMeta): | ||
... | ||
class GetName(AssociatedType): | ||
... | ||
@typeclass(GetName) | ||
def get_name(instance) -> str: | ||
... | ||
@get_name.instance(delegate=UserDict) | ||
def _get_name_user_dict(instance: UserDict) -> str: | ||
return instance['name'] | ||
def callback(instance: Supports[GetName]) -> str: | ||
return get_name(instance) | ||
a: UserDict = {'name': 'sobolevn', 'registered': True} | ||
b: User = {'name': 'sobolevn', 'registered': True} | ||
c = {'name': 'sobolevn', 'registered': True} | ||
callback(a) # ok | ||
callback(b) | ||
callback(c) | ||
callback({}) | ||
out: | | ||
main:40: error: Argument 1 to "callback" has incompatible type "User"; expected "Supports[GetName]" | ||
main:41: error: Argument 1 to "callback" has incompatible type "Dict[str, object]"; expected "Supports[GetName]" | ||
main:42: error: Argument 1 to "callback" has incompatible type "Dict[<nothing>, <nothing>]"; expected "Supports[GetName]" | ||
- case: typeclass_typed_dict2 | ||
disable_cache: false | ||
main: | | ||
from classes import typeclass | ||
from typing_extensions import TypedDict | ||
class User(TypedDict): | ||
name: str | ||
registered: bool | ||
class UserDictMeta(type): | ||
def __instancecheck__(cls, arg: object) -> bool: | ||
return ( | ||
isinstance(arg, dict) and | ||
isinstance(arg.get('name'), str) and | ||
isinstance(arg.get('registered'), bool) | ||
) | ||
UserMeta = type('UserMeta', (UserDictMeta, type(TypedDict)), {}) | ||
class UserDict(User, metaclass=UserMeta): | ||
... | ||
@typeclass | ||
def get_name(instance) -> str: | ||
... | ||
@get_name.instance(delegate=UserDict) | ||
def _get_name_user_dict(instance: User) -> str: | ||
return instance['name'] | ||
out: | | ||
main:25: error: Instance "TypedDict('main.User', {'name': builtins.str, 'registered': builtins.bool})" does not match inferred type "main.UserDict" | ||
- case: typeclass_typed_dict3 | ||
disable_cache: false | ||
main: | | ||
from classes import typeclass | ||
from typing_extensions import TypedDict | ||
class User(TypedDict): | ||
name: str | ||
registered: bool | ||
class Other(TypedDict): # even has the same structure | ||
name: str | ||
registered: bool | ||
class UserDictMeta(type): | ||
def __instancecheck__(cls, arg: object) -> bool: | ||
return ( | ||
isinstance(arg, dict) and | ||
isinstance(arg.get('name'), str) and | ||
isinstance(arg.get('registered'), bool) | ||
) | ||
UserMeta = type('UserMeta', (UserDictMeta, type(TypedDict)), {}) | ||
class UserDict(User, metaclass=UserMeta): | ||
... | ||
@typeclass | ||
def get_name(instance) -> str: | ||
... | ||
@get_name.instance(delegate=UserDict) | ||
def _get_name_user_dict(instance: Other) -> str: | ||
return instance['name'] | ||
out: | | ||
main:29: error: Instance "TypedDict('main.Other', {'name': builtins.str, 'registered': builtins.bool})" does not match inferred type "main.UserDict" |