Skip to content

Commit

Permalink
Fix annotation typing of Optional[tuple] (#5272)
Browse files Browse the repository at this point in the history
Allow it to use a Py-tuple instead of a ctuple
  • Loading branch information
da-woods authored Mar 1, 2023
1 parent c788bde commit 94bea66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cython/Compiler/PyrexTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4467,6 +4467,9 @@ def __init__(self, cname, components):
self.exception_check = True
self._convert_to_py_code = None
self._convert_from_py_code = None
# equivalent_type must be set now because it isn't available at import time
from .Builtin import tuple_type
self.equivalent_type = tuple_type

def __str__(self):
return "(%s)" % ", ".join(str(c) for c in self.components)
Expand Down
9 changes: 9 additions & 0 deletions tests/run/pep526_variable_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ def test_use_typing_attributes_as_non_annotations():
print(y1, str(y2) in allowed_optional_strings)
print(z1, str(z2) in allowed_optional_strings)

def test_optional_ctuple(x: typing.Optional[tuple[float]]):
"""
Should not be a C-tuple (because these can't be optional)
>>> test_optional_ctuple((1.0,))
tuple object
"""
print(cython.typeof(x) + (" object" if not cython.compiled else ""))


try:
import numpy.typing as npt
import numpy as np
Expand Down

0 comments on commit 94bea66

Please sign in to comment.