Skip to content

Comments

feat: support Optional[T] in Python#68

Merged
crepererum merged 1 commit intomainfrom
crepererum/py_typing_optional
Sep 8, 2025
Merged

feat: support Optional[T] in Python#68
crepererum merged 1 commit intomainfrom
crepererum/py_typing_optional

Conversation

@crepererum
Copy link
Collaborator

I actually wanted to implement this but it turns out that Python 3.14 does this automatically for us (this doesn't "just work" in Python 3.13). Since we're using the release candidate for the not-yet-released 3.14, that's fine. I however decided to add a guard/check so nobody tries to run our WASM payload with an older version (or a too-new one).

I actually wanted to implement this but it turns out that Python 3.14
does this automatically for us (this doesn't "just work" in Python
3.13). Since we're using the release candidate for the not-yet-released
3.14, that's fine. I however decided to add a guard/check so nobody
tries to run our WASM payload with an older version (or a too-new one).
@crepererum crepererum requested a review from a team September 8, 2025 10:45
@crepererum
Copy link
Collaborator Author

Here's why this works now:

Python 3.14.0rc2 (tags/v3.14.0rc2-dirty:31967d8, Aug 14 2025, 12:19:47) [Clang 18.1.2-wasi-sdk (https://github.com/llvm/llvm-project 26a1d6601d727a96f43 on wasi
Type "help", "copyright", "credits" or "license" for more information.
warning: can't use pyrepl: No module named 'termios'
>>> import typing
>>> import types
>>> x = typing.Optional[int]
>>> type(x)
<class 'typing.Union'>
>>> x.__args__
(<class 'int'>, <class 'NoneType'>)
>>>
>>> y = int | None
>>> type(y)
<class 'typing.Union'>
>>> y.__args__
(<class 'int'>, <class 'NoneType'>)

but this didn't used to work in older versions of Python:

Python 3.13.7 (main, Aug 15 2025, 12:34:02) [GCC 15.2.1 20250813] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> import types
>>> x = typing.Optional[int]
>>> type(x)
<class 'typing._UnionGenericAlias'>
>>> x.__args__
(<class 'int'>, <class 'NoneType'>)
>>>
>>> y = int | None
>>> type(y)
<class 'types.UnionType'>
>>> y.__args__
(<class 'int'>, <class 'NoneType'>)

so __args__ always worked, but the type was different. So I thought we needed some extra code, but no, Python just fixed that for us.

@crepererum crepererum added this pull request to the merge queue Sep 8, 2025
Merged via the queue into main with commit 88d827b Sep 8, 2025
1 check passed
@crepererum crepererum deleted the crepererum/py_typing_optional branch September 8, 2025 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants