Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
fluiddyn
transonic
Commits
8de0943befee
Commit
3cab3ebe
authored
Sep 21, 2019
by
Pierre Augier
Browse files
Correct behavior of None for typing + Optional
parent
8c128597b465
Changes
3
Hide whitespace changes
Inline
Side-by-side
transonic/__init__.py
View file @
8de0943b
...
...
@@ -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"
,
...
...
transonic/test_typing.py
View file @
8de0943b
...
...
@@ -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]"
transonic/typing.py
View file @
8de0943b
...
...
@@ -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_
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment