-
-
Notifications
You must be signed in to change notification settings - Fork 29
Supports Type[T] type with delegate
#316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,48 @@ | ||
| from typing import Type | ||
|
|
||
| import pytest | ||
|
|
||
| from classes import typeclass | ||
|
|
||
|
|
||
| class _MyClass(object): | ||
| """We use this class to test `Type[MyClass]`.""" | ||
|
|
||
|
|
||
| class _MyClassTypeMeta(type): | ||
| def __instancecheck__(cls, typ) -> bool: | ||
| return typ is _MyClass | ||
|
|
||
|
|
||
| class _MyClassType(Type[_MyClass], metaclass=_MyClassTypeMeta): # type: ignore | ||
| """Delegate class.""" | ||
|
|
||
|
|
||
| @typeclass | ||
| def class_type(typ) -> Type: | ||
| """Returns the type representation.""" | ||
|
|
||
|
|
||
| @class_type.instance(delegate=_MyClassType) | ||
| def _my_class_type(typ: Type[_MyClass]) -> Type: | ||
| return typ | ||
|
|
||
|
|
||
| def test_correct_class_type(): | ||
| """Ensures `Type[T]` works correctly with delegate.""" | ||
| assert class_type(_MyClass) is _MyClass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('typ', [ | ||
| int, float, type, dict, list, | ||
| ]) | ||
| def test_wrong_class_type(typ): | ||
| """Ensures other types doesn't works with our delegate.""" | ||
| with pytest.raises(NotImplementedError): | ||
| class_type(typ) | ||
|
|
||
|
|
||
| def test_passing_class_type_instance(): | ||
| """Ensures passing a instance of the expected type doesn't work.""" | ||
| with pytest.raises(NotImplementedError): | ||
| class_type(_MyClass()) # type: ignore[arg-type] |
This file contains hidden or 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,26 @@ | ||
| - case: typing_type_correct | ||
| disable_cache: true | ||
| main: | | ||
| from typing import Type | ||
| from classes import typeclass | ||
|
|
||
| class _MyClass(object): | ||
| ... | ||
|
|
||
| class _MyClassType(Type[_MyClass]): | ||
| ... | ||
|
|
||
|
|
||
| @typeclass | ||
| def class_type(typ) -> Type: | ||
| ... | ||
|
|
||
| @class_type.instance(delegate=_MyClassType) | ||
| def _my_class_type(typ: Type[_MyClass]) -> Type: | ||
| return typ | ||
|
|
||
| class_type(_MyClass) | ||
| class_type(int) | ||
| out: | | ||
| main:7: error: Invalid base class "Type" | ||
| main:20: error: Argument 1 to "class_type" has incompatible type "Type[int]"; expected "Type[_MyClass]" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.