mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
Add from-str constructors for several types (#533)
* Add from-str constructors for several types * Fix tests * Disable test
This commit is contained in:
parent
725003c64f
commit
e40527f845
@ -17,6 +17,7 @@ from internal.types.generator import *
|
|||||||
from internal.types.optional import *
|
from internal.types.optional import *
|
||||||
from internal.types.slice import *
|
from internal.types.slice import *
|
||||||
from internal.types.range import *
|
from internal.types.range import *
|
||||||
|
from internal.types.complex import *
|
||||||
from internal.internal import *
|
from internal.internal import *
|
||||||
from internal.types.strbuf import strbuf as _strbuf
|
from internal.types.strbuf import strbuf as _strbuf
|
||||||
from internal.types.collections.list import *
|
from internal.types.collections.list import *
|
||||||
|
@ -524,6 +524,26 @@ class complex:
|
|||||||
free(s)
|
free(s)
|
||||||
return complex(x, y)
|
return complex(x, y)
|
||||||
|
|
||||||
|
@extend
|
||||||
|
class float32:
|
||||||
|
def _from_str(s: str) -> float32:
|
||||||
|
return float32(float._from_str(s))
|
||||||
|
|
||||||
|
@extend
|
||||||
|
class float16:
|
||||||
|
def _from_str(s: str) -> float16:
|
||||||
|
return float16(float._from_str(s))
|
||||||
|
|
||||||
|
@extend
|
||||||
|
class bfloat16:
|
||||||
|
def _from_str(s: str) -> bfloat16:
|
||||||
|
return bfloat16(float._from_str(s))
|
||||||
|
|
||||||
|
@extend
|
||||||
|
class complex64:
|
||||||
|
def _from_str(s: str) -> complex64:
|
||||||
|
return complex64(complex._from_str(s))
|
||||||
|
|
||||||
def _jit_display(x, s: Static[str], bundle: Set[str] = Set[str]()):
|
def _jit_display(x, s: Static[str], bundle: Set[str] = Set[str]()):
|
||||||
if isinstance(x, None):
|
if isinstance(x, None):
|
||||||
return
|
return
|
||||||
|
@ -302,7 +302,10 @@ class complex64:
|
|||||||
return (f32(0.0), f32(0.0))
|
return (f32(0.0), f32(0.0))
|
||||||
|
|
||||||
def __new__(other):
|
def __new__(other):
|
||||||
return complex64(other.__complex__())
|
if isinstance(other, str):
|
||||||
|
return complex64._from_str(other)
|
||||||
|
else:
|
||||||
|
return complex64(other.__complex__())
|
||||||
|
|
||||||
def __new__(real: f32):
|
def __new__(real: f32):
|
||||||
return complex64(real, f32(0.0))
|
return complex64(real, f32(0.0))
|
||||||
|
@ -412,6 +412,9 @@ class float32:
|
|||||||
def __new__(what: float32) -> float32:
|
def __new__(what: float32) -> float32:
|
||||||
return what
|
return what
|
||||||
|
|
||||||
|
def __new__(what: str) -> float32:
|
||||||
|
return float32._from_str(what)
|
||||||
|
|
||||||
def __new__() -> float32:
|
def __new__() -> float32:
|
||||||
return float32.__new__(0.0)
|
return float32.__new__(0.0)
|
||||||
|
|
||||||
@ -763,6 +766,9 @@ class float16:
|
|||||||
def __new__(what: float16) -> float16:
|
def __new__(what: float16) -> float16:
|
||||||
return what
|
return what
|
||||||
|
|
||||||
|
def __new__(what: str) -> float16:
|
||||||
|
return float16._from_str(what)
|
||||||
|
|
||||||
def __new__() -> float16:
|
def __new__() -> float16:
|
||||||
return float16.__new__(0.0)
|
return float16.__new__(0.0)
|
||||||
|
|
||||||
@ -1060,6 +1066,9 @@ class bfloat16:
|
|||||||
def __new__(what: bfloat16) -> bfloat16:
|
def __new__(what: bfloat16) -> bfloat16:
|
||||||
return what
|
return what
|
||||||
|
|
||||||
|
def __new__(what: str) -> bfloat16:
|
||||||
|
return bfloat16._from_str(what)
|
||||||
|
|
||||||
def __new__() -> bfloat16:
|
def __new__() -> bfloat16:
|
||||||
return bfloat16.__new__(0.0)
|
return bfloat16.__new__(0.0)
|
||||||
|
|
||||||
|
@ -617,6 +617,13 @@ def test_complex_format():
|
|||||||
assert repr(complex64(math.nan, -math.inf)) == 'complex64(nan-infj)'
|
assert repr(complex64(math.nan, -math.inf)) == 'complex64(nan-infj)'
|
||||||
assert repr(complex64(math.nan, math.nan)) == 'complex64(nan+nanj)'
|
assert repr(complex64(math.nan, math.nan)) == 'complex64(nan+nanj)'
|
||||||
|
|
||||||
|
@test
|
||||||
|
def test_alt_types_from_str():
|
||||||
|
assert float32('3.14') == float32(3.14)
|
||||||
|
# TODO: fails on manylinux w/ "Symbols not found: [ __truncdfhf2, __extendhfsf2 ]"
|
||||||
|
# assert float16('3.14') == float16(3.14)
|
||||||
|
assert complex64('1+2j') == complex64(1+2j)
|
||||||
|
|
||||||
class A:
|
class A:
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return 42
|
return 42
|
||||||
@ -811,6 +818,7 @@ test_map_filter()
|
|||||||
test_gen_builtins()
|
test_gen_builtins()
|
||||||
test_int_format()
|
test_int_format()
|
||||||
test_complex_format()
|
test_complex_format()
|
||||||
|
test_alt_types_from_str()
|
||||||
test_reversed()
|
test_reversed()
|
||||||
test_divmod()
|
test_divmod()
|
||||||
test_pow()
|
test_pow()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user