Skip to content

Commit 94bea66

Browse files
authored
Fix annotation typing of Optional[tuple] (#5272)
Allow it to use a Py-tuple instead of a ctuple
1 parent c788bde commit 94bea66

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Cython/Compiler/PyrexTypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,6 +4467,9 @@ def __init__(self, cname, components):
44674467
self.exception_check = True
44684468
self._convert_to_py_code = None
44694469
self._convert_from_py_code = None
4470+
# equivalent_type must be set now because it isn't available at import time
4471+
from .Builtin import tuple_type
4472+
self.equivalent_type = tuple_type
44704473

44714474
def __str__(self):
44724475
return "(%s)" % ", ".join(str(c) for c in self.components)

tests/run/pep526_variable_annotations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ def test_use_typing_attributes_as_non_annotations():
280280
print(y1, str(y2) in allowed_optional_strings)
281281
print(z1, str(z2) in allowed_optional_strings)
282282

283+
def test_optional_ctuple(x: typing.Optional[tuple[float]]):
284+
"""
285+
Should not be a C-tuple (because these can't be optional)
286+
>>> test_optional_ctuple((1.0,))
287+
tuple object
288+
"""
289+
print(cython.typeof(x) + (" object" if not cython.compiled else ""))
290+
291+
283292
try:
284293
import numpy.typing as npt
285294
import numpy as np

0 commit comments

Comments
 (0)