feat: speed up creating Variant objects (#144)
This commit is contained in:
@@ -19,5 +19,5 @@ cdef class SignatureTree:
|
|||||||
cdef class Variant:
|
cdef class Variant:
|
||||||
|
|
||||||
cdef public object type
|
cdef public object type
|
||||||
cdef public str signature
|
cdef public object signature
|
||||||
cdef public object value
|
cdef public object value
|
||||||
|
|||||||
@@ -405,31 +405,27 @@ class Variant:
|
|||||||
"""Init a new Variant."""
|
"""Init a new Variant."""
|
||||||
if type(signature) is SignatureTree:
|
if type(signature) is SignatureTree:
|
||||||
signature_tree = signature
|
signature_tree = signature
|
||||||
|
self.signature = signature_tree.signature
|
||||||
|
self.type = signature_tree.types[0]
|
||||||
elif type(signature) is SignatureType:
|
elif type(signature) is SignatureType:
|
||||||
signature_type = signature
|
|
||||||
signature_str = signature.signature
|
|
||||||
signature_tree = None
|
signature_tree = None
|
||||||
|
self.signature = signature.signature
|
||||||
|
self.type = signature
|
||||||
elif type(signature) is str:
|
elif type(signature) is str:
|
||||||
signature_tree = get_signature_tree(signature)
|
signature_tree = get_signature_tree(signature)
|
||||||
|
self.signature = signature
|
||||||
|
self.type = signature_tree.types[0]
|
||||||
else:
|
else:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"signature must be a SignatureTree, SignatureType, or a string"
|
"signature must be a SignatureTree, SignatureType, or a string"
|
||||||
)
|
)
|
||||||
|
self.value = value
|
||||||
if signature_tree:
|
if verify:
|
||||||
if verify and len(signature_tree.types) != 1:
|
if signature_tree and len(signature_tree.types) != 1:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"variants must have a signature for a single complete type"
|
"variants must have a signature for a single complete type"
|
||||||
)
|
)
|
||||||
signature_str = signature_tree.signature
|
self.type.verify(value)
|
||||||
signature_type = signature_tree.types[0]
|
|
||||||
|
|
||||||
if verify:
|
|
||||||
signature_type.verify(value)
|
|
||||||
|
|
||||||
self.type = signature_type
|
|
||||||
self.signature = signature_str
|
|
||||||
self.value = value
|
|
||||||
|
|
||||||
def __eq__(self, other: Any) -> bool:
|
def __eq__(self, other: Any) -> bool:
|
||||||
if type(other) is Variant:
|
if type(other) is Variant:
|
||||||
|
|||||||
@@ -220,3 +220,14 @@ def test_invalid_variants():
|
|||||||
|
|
||||||
with pytest.raises(SignatureBodyMismatchError):
|
with pytest.raises(SignatureBodyMismatchError):
|
||||||
tree.verify([con])
|
tree.verify([con])
|
||||||
|
|
||||||
|
|
||||||
|
def test_variant_signature_type():
|
||||||
|
tree = SignatureTree("as")
|
||||||
|
var = Variant(tree.types[0], ["foo", "bar"])
|
||||||
|
assert var.type == tree.types[0]
|
||||||
|
assert var.value == ["foo", "bar"]
|
||||||
|
assert var.signature == "as"
|
||||||
|
|
||||||
|
with pytest.raises(SignatureBodyMismatchError):
|
||||||
|
Variant(tree.types[0], "wrong")
|
||||||
|
|||||||
Reference in New Issue
Block a user