Commit 3cab3ebe authored by Pierre Augier's avatar Pierre Augier
Browse files

Correct behavior of None for typing + Optional

parent 8c128597b465
......@@ -17,6 +17,7 @@ from transonic.typing import (
Set,
str2type,
typeof,
Optional,
)
......@@ -24,15 +25,16 @@ __all__ = [
"__version__",
"Transonic",
"boost",
"jit",
"Array",
"NDim",
"Type",
"Union",
"List",
"Dict",
"Tuple",
"Set",
"jit",
"Union",
"Optional",
"set_backend",
"set_backend_for_this_module",
"set_compile_jit",
......
......@@ -14,6 +14,7 @@ from transonic.typing import (
typeof,
str2shape,
MemLayout,
Optional,
)
from transonic.backends.typing import base_type_formatter
......@@ -151,3 +152,7 @@ def test_shape():
)
== "int[3, :]"
)
def test_optional():
assert repr(Optional[int]) == "Union[int, None]"
......@@ -218,7 +218,7 @@ class NDim(TemplateVar):
class UnionVar(TemplateVar):
"""TemplateVar used for the Union type"""
_type_values = type
_type_values = (type, type(None))
_letter = "U"
......@@ -673,10 +673,30 @@ class Tuple(metaclass=TupleMeta):
"""
class OptionalMeta(Meta):
def __getitem__(self, type_):
return Union[type_, None]
class Optional(metaclass=OptionalMeta):
"""Similar to typing.Optional
Meaning that these two expressions are equivalent:
>>> Optional[int]
>>> Union[int, None]
"""
def format_type_as_backend_type(type_, backend_type_formatter, **kwargs):
"""Format a Transonic type as a backend (Pythran, Cython, ...) type
"""
if type_ is None:
# None has a special meaning for typing...
return "None"
if isinstance(type_, str):
type_ = str2type(type_)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment