-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mixin types feature to ADT (#10)
Now, by specifying mixin_types={'prod': cls} one can inject an additional base class to any production `prod`. This enables users to attach methods to the generated classes. It might also be useful to static type checkers by setting a base type to be one of Python 3.8's "Protocol" types.
- Loading branch information
1 parent
5838ad8
commit 5c29b7a
Showing
4 changed files
with
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Tests of mixin class feature | ||
""" | ||
|
||
import pytest | ||
|
||
from asdl_adt import ADT | ||
|
||
|
||
def test_basic_mixin(): | ||
""" | ||
Test that a basic mixin class is properly injected into the given sum type | ||
alternative, and not the others. | ||
""" | ||
|
||
class MixinA: # pylint: disable=C0115,C0116,R0903 | ||
def double(self): | ||
return self.update(x=2 * self.x) | ||
|
||
mixin_grammar = ADT( | ||
""" | ||
module test_basic_mixin { | ||
prod = ( int x, int y ) | ||
sum = A( int x ) | ||
| B( float y ) | ||
| C( int x, int y ) | ||
} | ||
""", | ||
mixin_types={ | ||
"A": MixinA, | ||
}, | ||
) | ||
|
||
obj = mixin_grammar.A(3) | ||
assert obj.double() == mixin_grammar.A(6) | ||
|
||
with pytest.raises(AttributeError, match="'B' object has no attribute 'double'"): | ||
mixin_grammar.B(3.14).double() |
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,16 @@ | ||
""" | ||
Coverage-focused test for object identity of same-named ADT types. | ||
""" | ||
|
||
from asdl_adt import ADT | ||
|
||
|
||
def test_module_caching(): | ||
""" | ||
Test that creating a second module with the same name returns the same | ||
object identically as the first call | ||
""" | ||
|
||
grammar_a = ADT("module cache_test { foo = ( int bar ) }") | ||
grammar_b = ADT("module cache_test { foo = ( int bar ) }") | ||
assert grammar_a is grammar_b |
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