Cython 3.2.6__py3-none-any.whl → 3.3.0a2.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Cython might be problematic. Click here for more details.

Files changed (112) hide show
  1. Cython/Build/BuildExecutable.py +1 -14
  2. Cython/Build/Dependencies.py +6 -44
  3. Cython/Build/Inline.py +7 -3
  4. Cython/Build/IpythonMagic.py +0 -1
  5. Cython/Build/SharedModule.py +1 -2
  6. Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
  7. Cython/Build/Tests/TestDependencies.py +3 -3
  8. Cython/Build/Tests/TestInline.py +3 -3
  9. Cython/Build/Tests/TestIpythonMagic.py +35 -19
  10. Cython/Build/Tests/TestStripLiterals.py +2 -2
  11. Cython/Compiler/Buffer.py +5 -5
  12. Cython/Compiler/Builtin.py +57 -46
  13. Cython/Compiler/Code.pxd +12 -9
  14. Cython/Compiler/Code.py +220 -94
  15. Cython/Compiler/Dataclass.py +78 -52
  16. Cython/Compiler/Errors.py +21 -8
  17. Cython/Compiler/ExprNodes.py +871 -459
  18. Cython/Compiler/FlowControl.pxd +8 -6
  19. Cython/Compiler/FlowControl.py +111 -8
  20. Cython/Compiler/FusedNode.py +17 -12
  21. Cython/Compiler/Lexicon.py +123 -122
  22. Cython/Compiler/Main.py +3 -4
  23. Cython/Compiler/MatchCaseNodes.py +1948 -10
  24. Cython/Compiler/MemoryView.py +62 -33
  25. Cython/Compiler/ModuleNode.py +559 -283
  26. Cython/Compiler/Naming.py +4 -0
  27. Cython/Compiler/Nodes.py +333 -197
  28. Cython/Compiler/Optimize.py +268 -112
  29. Cython/Compiler/Options.py +0 -5
  30. Cython/Compiler/ParseTreeTransforms.pxd +2 -1
  31. Cython/Compiler/ParseTreeTransforms.py +180 -76
  32. Cython/Compiler/Parsing.py +55 -38
  33. Cython/Compiler/PyrexTypes.py +364 -128
  34. Cython/Compiler/Scanning.pxd +27 -5
  35. Cython/Compiler/Scanning.py +24 -14
  36. Cython/Compiler/StringEncoding.py +1 -2
  37. Cython/Compiler/Symtab.py +27 -19
  38. Cython/Compiler/Tests/TestBuiltin.py +41 -6
  39. Cython/Compiler/Tests/TestCmdLine.py +3 -2
  40. Cython/Compiler/Tests/TestCode.py +4 -3
  41. Cython/Compiler/Tests/TestFlowControl.py +2 -2
  42. Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
  43. Cython/Compiler/Tests/TestScanning.py +2 -4
  44. Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
  45. Cython/Compiler/Tests/TestStringEncoding.py +2 -3
  46. Cython/Compiler/Tests/TestTypes.py +46 -3
  47. Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
  48. Cython/Compiler/TypeInference.py +20 -0
  49. Cython/Compiler/TypeSlots.py +171 -16
  50. Cython/Compiler/UtilNodes.py +24 -0
  51. Cython/Compiler/UtilityCode.py +1 -0
  52. Cython/Compiler/Visitor.pxd +1 -1
  53. Cython/Compiler/Visitor.py +3 -2
  54. Cython/Debugger/Tests/TestLibCython.py +7 -2
  55. Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
  56. Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
  57. Cython/Includes/cpython/array.pxd +5 -5
  58. Cython/Includes/cpython/dict.pxd +1 -6
  59. Cython/Includes/cpython/float.pxd +19 -10
  60. Cython/Includes/cpython/frozendict.pxd +37 -0
  61. Cython/Includes/cpython/long.pxd +31 -0
  62. Cython/Includes/cpython/object.pxd +9 -12
  63. Cython/Includes/cpython/sentinel.pxd +17 -0
  64. Cython/Includes/cpython/type.pxd +93 -0
  65. Cython/Includes/libcpp/exception.pxd +90 -0
  66. Cython/LZSS.py +170 -0
  67. Cython/Shadow.py +550 -73
  68. Cython/StringIOTree.py +1 -2
  69. Cython/Tempita/_tempita.py +45 -46
  70. Cython/TestUtils.py +50 -8
  71. Cython/Tests/TestCythonUtils.py +2 -2
  72. Cython/Tests/TestJediTyper.py +1 -1
  73. Cython/Tests/TestShadow.py +7 -5
  74. Cython/Tests/TestStringIOTree.py +4 -3
  75. Cython/Tests/TestTestUtils.py +2 -3
  76. Cython/Utility/AsyncGen.c +59 -19
  77. Cython/Utility/Buffer.c +4 -3
  78. Cython/Utility/Builtins.c +139 -17
  79. Cython/Utility/CConvert.pyx +27 -12
  80. Cython/Utility/CommonStructures.c +55 -37
  81. Cython/Utility/Coroutine.c +112 -68
  82. Cython/Utility/CythonFunction.c +607 -330
  83. Cython/Utility/Embed.c +8 -0
  84. Cython/Utility/Exceptions.c +20 -3
  85. Cython/Utility/ExtensionTypes.c +414 -224
  86. Cython/Utility/FunctionArguments.c +83 -81
  87. Cython/Utility/ImportExport.c +72 -61
  88. Cython/Utility/MatchCase.c +979 -0
  89. Cython/Utility/MatchCase_Cy.pyx +12 -0
  90. Cython/Utility/MemoryView.pxd +9 -88
  91. Cython/Utility/MemoryView.pyx +173 -61
  92. Cython/Utility/MemoryView_C.c +3 -3
  93. Cython/Utility/ModuleSetupCode.c +306 -157
  94. Cython/Utility/ObjectHandling.c +483 -404
  95. Cython/Utility/Optimize.c +1117 -164
  96. Cython/Utility/Overflow.c +3 -9
  97. Cython/Utility/Profile.c +4 -0
  98. Cython/Utility/StringTools.c +187 -149
  99. Cython/Utility/Synchronization.c +39 -0
  100. Cython/Utility/TString.c +24 -13
  101. Cython/Utility/TypeConversion.c +405 -250
  102. Cython/Utility/arrayarray.h +24 -22
  103. Cython/Utils.py +11 -23
  104. Cython/__init__.py +3 -1
  105. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
  106. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
  107. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
  108. Cython/Shadow.pyi +0 -521
  109. Cython/Utility/Printing.c +0 -86
  110. Cython/__init__.pyi +0 -7
  111. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
  112. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
Cython/Shadow.py CHANGED
@@ -1,18 +1,47 @@
1
1
  # cython.* namespace for pure mode.
2
+ from __future__ import annotations
2
3
 
3
4
  # Possible version formats: "3.1.0", "3.1.0a1", "3.1.0a1.dev0"
4
- __version__ = "3.2.6"
5
-
5
+ __version__ = "3.3.0a2.dev0"
6
+
7
+ from types import TracebackType
8
+ from typing import (
9
+ TYPE_CHECKING,
10
+ Any, Iterable, Sequence, Optional, Type, TypeVar, Generic, Callable, overload,
11
+ )
12
+
13
+ if TYPE_CHECKING:
14
+ from builtins import (int as py_int, float as py_float,
15
+ bool as py_bool, str as py_str, complex as py_complex)
16
+ from typing import TypeAlias, Annotated, ParamSpec, Self
17
+ _P = ParamSpec('_P')
18
+
19
+ # TypeVars need to be defined at runtime for Generic types
20
+ _T = TypeVar('_T')
21
+ _C = TypeVar('_C', bound='Callable')
22
+ _TypeT = TypeVar('_TypeT', bound='Type')
23
+ _C_Or_TypeT = TypeVar('_C_Or_TypeT', _C, _TypeT)
24
+ _Decorator = Callable[[_C_Or_TypeT], _C_Or_TypeT]
25
+ _FuncDecorator = Callable[[_C], _C]
26
+ _ClassDecorator = Callable[[_TypeT], _TypeT]
6
27
 
7
28
  # BEGIN shameless copy from Cython/minivect/minitypes.py
8
29
 
9
- class _ArrayType:
10
-
11
- is_array = True
12
- subtypes = ['dtype']
13
-
14
- def __init__(self, dtype, ndim, is_c_contig=False, is_f_contig=False,
15
- inner_contig=False, broadcasting=None):
30
+ class _ArrayType(Generic[_T]):
31
+
32
+ is_array: bool = True
33
+ subtypes: Sequence[str] = ['dtype']
34
+ dtype: _T
35
+ ndim: int
36
+ is_c_contig: bool
37
+ is_f_contig: bool
38
+ inner_contig: bool
39
+ broadcasting: Any
40
+
41
+ # broadcasting is not used, so it's not clear about its type
42
+ def __init__(self, dtype: _T, ndim: int, is_c_contig: bool = False,
43
+ is_f_contig: bool = False, inner_contig: bool = False,
44
+ broadcasting: Any = None) -> None:
16
45
  self.dtype = dtype
17
46
  self.ndim = ndim
18
47
  self.is_c_contig = is_c_contig
@@ -20,7 +49,7 @@ class _ArrayType:
20
49
  self.inner_contig = inner_contig or is_c_contig or is_f_contig
21
50
  self.broadcasting = broadcasting
22
51
 
23
- def __repr__(self):
52
+ def __repr__(self) -> str:
24
53
  axes = [":"] * self.ndim
25
54
  if self.is_c_contig:
26
55
  axes[-1] = "::1"
@@ -30,7 +59,7 @@ class _ArrayType:
30
59
  return "%s[%s]" % (self.dtype, ", ".join(axes))
31
60
 
32
61
 
33
- def index_type(base_type, item):
62
+ def index_type(base_type: _T, item: tuple | slice | int) -> _ArrayType[_T]:
34
63
  """
35
64
  Support array type creation by slicing, e.g. double[:, :] specifies
36
65
  a 2D strided array of doubles. The syntax is the same as for
@@ -71,47 +100,73 @@ def index_type(base_type, item):
71
100
  # END shameless copy
72
101
 
73
102
 
74
- compiled = False
103
+ compiled: bool = False
75
104
 
76
105
  _Unspecified = object()
77
106
 
78
107
  # Function decorators
79
108
 
80
- def _empty_decorator(x):
109
+ def _empty_decorator(x: _C_Or_TypeT) -> _C_Or_TypeT:
81
110
  return x
82
111
 
83
- def locals(**arg_types):
112
+ _empty_func_decorator: _FuncDecorator = _empty_decorator
113
+ _empty_class_decorator: _ClassDecorator = _empty_decorator
114
+
115
+ def _compiler_directive(val: bool = ...) -> _Decorator:
84
116
  return _empty_decorator
85
117
 
86
- def test_assert_path_exists(*paths):
118
+ def locals(**arg_types: Any) -> _FuncDecorator:
119
+ return _empty_func_decorator
120
+
121
+ def test_assert_path_exists(*paths: str) -> _Decorator:
87
122
  return _empty_decorator
88
123
 
89
- def test_fail_if_path_exists(*paths):
124
+ def test_fail_if_path_exists(*paths: str) -> _Decorator:
90
125
  return _empty_decorator
91
126
 
92
127
  class _EmptyDecoratorAndManager:
128
+ @overload
129
+ def __call__(self, __val: bool) -> _Decorator: ...
130
+
131
+ @overload
132
+ def __call__(self, __func: _C) -> _C: ...
133
+
93
134
  def __call__(self, x):
94
135
  return x
95
- def __enter__(self):
136
+ def __enter__(self) -> None:
96
137
  pass
97
- def __exit__(self, exc_type, exc_value, traceback):
138
+ def __exit__(self, exc_type: Optional[Type[BaseException]],
139
+ exc: Optional[BaseException],
140
+ tb: Optional[TracebackType]) -> None:
98
141
  pass
99
142
 
100
143
  class _Optimization:
101
- pass
144
+ def use_switch(val: bool) -> _Decorator:
145
+ return _EmptyDecoratorAndManager()
146
+
147
+ def unpack_method_calls(val: bool) -> _Decorator:
148
+ return _EmptyDecoratorAndManager()
149
+
150
+ cclass = cfunc = ccall = _EmptyDecoratorAndManager()
151
+
152
+ ufunc = _empty_func_decorator
102
153
 
103
- cclass = ccall = cfunc = _EmptyDecoratorAndManager()
154
+ internal = c_api_binop_methods = type_version_tag = no_gc_clear = \
155
+ no_gc = total_ordering = _empty_class_decorator
104
156
 
105
157
  annotation_typing = returns = wraparound = boundscheck = initializedcheck = \
106
- nonecheck = embedsignature = cdivision = cdivision_warnings = collection_type = \
107
- always_allow_keywords = profile = linetrace = infer_types = \
108
- unraisable_tracebacks = freelist = auto_pickle = cpow = trashcan = \
109
- auto_cpdef = c_api_binop_methods = \
158
+ nonecheck = cdivision = cdivision_warnings = collection_type = \
159
+ profile = linetrace = infer_types = \
160
+ freelist = auto_pickle = cpow = trashcan = auto_cpdef = \
110
161
  allow_none_for_extension_args = callspec = show_performance_hints = \
111
- cpp_locals = py2_import = iterable_coroutine = remove_unreachable = \
112
- overflowcheck = test_body_needs_exception_handling = \
162
+ py2_import = iterable_coroutine = remove_unreachable = \
163
+ test_body_needs_exception_handling = \
113
164
  lambda _: _EmptyDecoratorAndManager()
114
165
 
166
+ binding = embedsignature = always_allow_keywords = unraisable_tracebacks = \
167
+ cpp_locals = \
168
+ _compiler_directive
169
+
115
170
  # Note that fast_getattr is untested and undocumented!
116
171
  fast_getattr = lambda _: _EmptyDecoratorAndManager()
117
172
  # c_compile_guard is largely for internal use
@@ -121,22 +176,67 @@ exceptval = lambda _=None, check=True: _EmptyDecoratorAndManager()
121
176
 
122
177
  optimize = _Optimization()
123
178
 
179
+ class _OverflowcheckClass:
180
+ def __call__(self, val: bool) -> _Decorator:
181
+ return _EmptyDecoratorAndManager()
182
+
183
+ def fold(self, val: bool) -> _Decorator:
184
+ return _EmptyDecoratorAndManager()
185
+
186
+ overflowcheck = _OverflowcheckClass()
187
+
188
+ if TYPE_CHECKING:
189
+ # May be a bit hard to read but essentially means:
190
+ # > Returns a callable that takes another callable with these parameters and *some*
191
+ # > return value, then returns another callable with the same parameters but
192
+ # > the return type is the previous 'type' parameter.
193
+ def returns(__type: Type[_T]) -> Callable[[Callable[_P, object]], Callable[_P, _T]]: ...
194
+
195
+ def exceptval(__val: Any, *, check: bool = False) -> _Decorator: ...
196
+
124
197
 
125
198
  embedsignature.format = overflowcheck.fold = optimize.use_switch = \
126
199
  optimize.unpack_method_calls = lambda arg: _EmptyDecoratorAndManager()
127
200
 
128
- final = internal = type_version_tag = no_gc_clear = no_gc = total_ordering = \
129
- ufunc = _empty_decorator
130
-
131
- binding = lambda _: _empty_decorator
201
+ final = _empty_decorator
132
202
 
133
203
  class warn:
134
- undeclared = unreachable = maybe_uninitialized = unused = \
135
- unused_arg = unused_result = \
136
- lambda _: _EmptyDecoratorAndManager()
204
+ @staticmethod
205
+ def undeclared(val: bool) -> _Decorator:
206
+ return _EmptyDecoratorAndManager()
207
+
208
+ @staticmethod
209
+ def unreachable(val: bool) -> _Decorator:
210
+ return _EmptyDecoratorAndManager()
211
+
212
+ @staticmethod
213
+ def maybe_uninitialized(val: bool) -> _Decorator:
214
+ return _EmptyDecoratorAndManager()
215
+
216
+ @staticmethod
217
+ def unused(val: bool) -> _Decorator:
218
+ return _EmptyDecoratorAndManager()
219
+
220
+ @staticmethod
221
+ def unused_argument(val: bool) -> _Decorator:
222
+ return _EmptyDecoratorAndManager()
223
+
224
+ @staticmethod
225
+ def multiple_declarators(val: bool) -> _Decorator:
226
+ return _EmptyDecoratorAndManager()
137
227
 
138
228
 
139
229
  _cython_inline = None
230
+ @overload
231
+ def inline(__func: _C) -> _C: ...
232
+
233
+ @overload
234
+ def inline(__code: str, *, get_type: Callable[[object, object], str] = ...,
235
+ lib_dir: str = ..., cython_include_dirs: Iterable[str] = ...,
236
+ cython_compiler_directives: Iterable[str] = ..., force: bool = ...,
237
+ quiet: bool = ..., locals: dict[str, str] = ...,
238
+ globals: dict[str, str] = ..., language_level: str = ...) -> Any: ...
239
+
140
240
  def inline(f, *args, **kwds):
141
241
  if isinstance(f, str):
142
242
  global _cython_inline
@@ -148,14 +248,14 @@ def inline(f, *args, **kwds):
148
248
  return f
149
249
 
150
250
 
151
- def compile(f):
251
+ def compile(f: _C) -> _C:
152
252
  from Cython.Build.Inline import RuntimeCompiledFunction
153
253
  return RuntimeCompiledFunction(f)
154
254
 
155
255
 
156
256
  # Special functions
157
257
 
158
- def cdiv(a, b):
258
+ def cdiv(a: int, b: int) -> int:
159
259
  if a < 0:
160
260
  a = -a
161
261
  b = -b
@@ -163,7 +263,7 @@ def cdiv(a, b):
163
263
  return (a + b + 1) // b
164
264
  return a // b
165
265
 
166
- def cmod(a, b):
266
+ def cmod(a: int, b: int) -> int:
167
267
  r = a % b
168
268
  if (a * b) < 0 and r:
169
269
  r -= b
@@ -172,6 +272,12 @@ def cmod(a, b):
172
272
 
173
273
  # Emulated language constructs
174
274
 
275
+ @overload
276
+ def cast(__t: Type[_T], __value: Any) -> _T: ...
277
+
278
+ @overload
279
+ def cast(__t: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: ...
280
+
175
281
  def cast(t, *args, **kwargs):
176
282
  kwargs.pop('typecheck', None)
177
283
  assert not kwargs
@@ -184,15 +290,15 @@ def cast(t, *args, **kwargs):
184
290
 
185
291
  return args[0]
186
292
 
187
- def sizeof(arg):
293
+ def sizeof(obj: object) -> int:
188
294
  return 1
189
295
 
190
- def typeof(arg):
191
- return arg.__class__.__name__
296
+ def typeof(obj: object) -> str:
297
+ return obj.__class__.__name__
192
298
  # return type(arg)
193
299
 
194
- def address(arg):
195
- return pointer(type(arg))([arg])
300
+ def address(obj: object) -> PointerType:
301
+ return pointer(type(obj))([obj])
196
302
 
197
303
  def _is_value_type(t):
198
304
  if isinstance(t, typedef):
@@ -200,6 +306,25 @@ def _is_value_type(t):
200
306
 
201
307
  return isinstance(t, type) and issubclass(t, (StructType, UnionType, ArrayType))
202
308
 
309
+ @overload
310
+ def declare(
311
+ t: Optional[Callable[..., _T]] = ...,
312
+ value: Any = ...,
313
+ ) -> _T:
314
+ ...
315
+
316
+ # This one is for attributes, they cannot have initializers through cython.declare() currently.
317
+ @overload
318
+ def declare(
319
+ t: Callable[..., _T],
320
+ *,
321
+ visibility: Literal['public', 'readonly', 'private'] = ...,
322
+ ) -> _T:
323
+ ...
324
+
325
+ @overload
326
+ def declare(**kwargs: type) -> None: ...
327
+
203
328
  def declare(t=None, value=_Unspecified, **kwds):
204
329
  if value is not _Unspecified:
205
330
  return cast(t, value)
@@ -211,6 +336,12 @@ def declare(t=None, value=_Unspecified, **kwds):
211
336
  class _nogil:
212
337
  """Support for 'with nogil' statement and @nogil decorator.
213
338
  """
339
+ @overload
340
+ def __call__(self, __val: bool) -> _FuncDecorator: ...
341
+
342
+ @overload
343
+ def __call__(self, __func: _C) -> _C: ...
344
+
214
345
  def __call__(self, x):
215
346
  if callable(x):
216
347
  # Used as function decorator => return the function unchanged.
@@ -218,10 +349,12 @@ class _nogil:
218
349
  # Used as conditional context manager or to create an "@nogil(True/False)" decorator => keep going.
219
350
  return self
220
351
 
221
- def __enter__(self):
352
+ def __enter__(self) -> None:
222
353
  pass
223
- def __exit__(self, exc_class, exc, tb):
224
- return exc_class is None
354
+ def __exit__(self, exc_type: Optional[Type[BaseException]],
355
+ exc: Optional[BaseException],
356
+ tb: Optional[TracebackType]) -> bool:
357
+ return exc_type is None
225
358
 
226
359
  nogil = _nogil()
227
360
  gil = _nogil()
@@ -236,20 +369,19 @@ class critical_section:
236
369
  self.arg0 = arg0
237
370
  def __call__(self, *args, **kwds):
238
371
  return self.arg0(*args, **kwds)
239
- def __enter__(self):
372
+ def __enter__(self) -> None:
240
373
  pass
241
- def __exit__(self, exc_class, exc, tb):
374
+ def __exit__(self, exc_type: Optional[Type[BaseException]],
375
+ exc: Optional[BaseException],
376
+ tb: Optional[TracebackType]) -> bool:
242
377
  return False
243
378
 
244
379
 
245
380
  # Emulated types
246
381
 
247
- class CythonMetaType(type):
248
-
249
- def __getitem__(type, ix):
250
- return array(type, ix)
251
-
252
- CythonTypeObject = CythonMetaType('CythonTypeObject', (object,), {})
382
+ class CythonTypeObject:
383
+ def __class_getitem__(cls, ix: int) -> Type[ArrayType[Self, int]]:
384
+ return array(cls, ix)
253
385
 
254
386
  class CythonType(CythonTypeObject):
255
387
 
@@ -258,9 +390,24 @@ class CythonType(CythonTypeObject):
258
390
  self = pointer(self)
259
391
  return self
260
392
 
393
+ if TYPE_CHECKING:
394
+ class PointerType(CythonType, Generic[_T]):
395
+ def __init__(
396
+ self,
397
+ value: Optional[ArrayType[_T] | PointerType[_T] | list[_T] | int] = ...
398
+ ) -> None: ...
399
+ def __getitem__(self, ix: int) -> _T: ...
400
+ def __setitem__(self, ix: int, value: _T) -> None: ...
401
+ def __eq__(self, value: object) -> bool: ...
402
+ def __repr__(self) -> str: ...
403
+
404
+ class ArrayType(PointerType[_T]):
405
+ def __init__(self) -> None: ...
406
+
261
407
  class PointerType(CythonType):
262
408
 
263
409
  def __init__(self, value=None):
410
+ from .Shadow import cast
264
411
  if isinstance(value, (ArrayType, PointerType)):
265
412
  self._items = [cast(self._basetype, a) for a in value._items]
266
413
  elif isinstance(value, list):
@@ -278,6 +425,7 @@ class PointerType(CythonType):
278
425
  def __setitem__(self, ix, value):
279
426
  if ix < 0:
280
427
  raise IndexError("negative indexing not allowed in C")
428
+ from .Shadow import cast
281
429
  self._items[ix] = cast(self._basetype, value)
282
430
 
283
431
  def __eq__(self, value):
@@ -370,6 +518,15 @@ class UnionType(CythonType):
370
518
  raise AttributeError("Union has no member '%s'" % key)
371
519
 
372
520
 
521
+ if TYPE_CHECKING:
522
+ class pointer(PointerType[_T]):
523
+ def __new__(cls, basetype: _T) -> Type[PointerType[_T]]: ...
524
+ def __class_getitem__(cls, basetype: _T) -> Type[PointerType[_T]]: ...
525
+
526
+ class array(ArrayType[_T]):
527
+ def __new__(basetype: _T, n: int) -> Type[ArrayType[_T, int]]: ...
528
+ def __class_getitem__(cls, item: tuple[_T, int]) -> Type[ArrayType[_T, int]]: ...
529
+
373
530
  class pointer(PointerType):
374
531
  # Implemented as class to support both 'pointer(int)' and 'pointer[int]'.
375
532
  def __new__(cls, basetype):
@@ -391,17 +548,17 @@ class array(ArrayType):
391
548
 
392
549
  def __class_getitem__(cls, item):
393
550
  basetype, n = item
394
- return cls(basetype, item)
551
+ return cls(basetype, n)
395
552
 
396
553
 
397
- def struct(**members):
554
+ def struct(**members: type) -> Type[Any]:
398
555
  class StructInstance(StructType):
399
556
  _members = members
400
557
  for key in members:
401
558
  setattr(StructInstance, key, None)
402
559
  return StructInstance
403
560
 
404
- def union(**members):
561
+ def union(**members: type) -> Type[Any]:
405
562
  class UnionInstance(UnionType):
406
563
  _members = members
407
564
  for key in members:
@@ -409,22 +566,37 @@ def union(**members):
409
566
  return UnionInstance
410
567
 
411
568
 
569
+ if TYPE_CHECKING:
570
+ class typedef(CythonType, Generic[_T]):
571
+ name: str
572
+
573
+ def __init__(self, type: _T, name: Optional[str] = ...) -> None: ...
574
+ def __call__(self, *arg: Any) -> _T: ...
575
+ def __repr__(self) -> str: ...
576
+ __getitem__ = index_type
577
+
412
578
  class typedef(CythonType):
579
+ name: str
413
580
 
414
- def __init__(self, type, name=None):
581
+ def __init__(self, type: _T, name: Optional[str] = None) -> None:
415
582
  self._basetype = type
416
583
  self.name = name
417
584
 
418
- def __call__(self, *arg):
585
+ def __call__(self, *arg: Any) -> _T:
419
586
  value = cast(self._basetype, *arg)
420
587
  return value
421
588
 
422
- def __repr__(self):
589
+ def __repr__(self) -> str:
423
590
  return self.name or str(self._basetype)
424
591
 
425
592
  __getitem__ = index_type
426
593
 
427
594
 
595
+ if TYPE_CHECKING:
596
+ const: TypeAlias = Annotated[_T, "cython.const"]
597
+ volatile: TypeAlias = Annotated[_T, "cython.volatile"]
598
+ restrict: TypeAlias = Annotated[_T, "cython.restrict"]
599
+
428
600
  class const(typedef):
429
601
  def __init__(self, type, name=None):
430
602
  name = f"const {name or repr(type)}"
@@ -443,11 +615,20 @@ class volatile(typedef):
443
615
  return volatile(base_type)
444
616
 
445
617
 
618
+ class restrict(typedef):
619
+ def __init__(self, type, name=None):
620
+ name = f"restrict {name or repr(type)}"
621
+ super().__init__(type, name)
622
+
623
+ def __class_getitem__(cls, base_type):
624
+ return restrict(base_type)
625
+
626
+
446
627
  class _FusedType(CythonType):
447
628
  __getitem__ = index_type
448
629
 
449
630
 
450
- def fused_type(*args):
631
+ def fused_type(*args: Any) -> Type[Any]:
451
632
  if not args:
452
633
  raise TypeError("Expected at least one type as argument")
453
634
 
@@ -468,11 +649,6 @@ def fused_type(*args):
468
649
  return _FusedType()
469
650
 
470
651
 
471
- def _specialized_from_args(signatures, args, kwargs):
472
- "Perhaps this should be implemented in a TreeFragment in Cython code"
473
- raise Exception("yet to be implemented")
474
-
475
-
476
652
  py_int = typedef(int, "int")
477
653
  py_long = typedef(int, "long") # for legacy Py2 code only
478
654
  py_float = typedef(float, "float")
@@ -539,6 +715,15 @@ for name in complex_types:
539
715
 
540
716
  del name, reprname
541
717
 
718
+ if TYPE_CHECKING:
719
+ Py_UCS4 = py_int | str
720
+ Py_UNICODE = py_int | str
721
+
722
+ bint = py_bool
723
+ void = Type[None]
724
+ basestring = py_str
725
+ unicode = py_str
726
+
542
727
  bint = typedef(bool, "bint")
543
728
  void = typedef(None, "void")
544
729
  Py_tss_t = typedef(None, "Py_tss_t")
@@ -566,7 +751,7 @@ for i in range(1, 4):
566
751
 
567
752
  del t, const_, i
568
753
 
569
- NULL = gs['p_void'](0)
754
+ NULL: pointer[Any] = gs['p_void'](0)
570
755
 
571
756
  del gs
572
757
 
@@ -667,24 +852,316 @@ sys.modules['cython.cimports'] = CythonCImports('cython.cimports', libc=sys.modu
667
852
 
668
853
  # In pure Python mode @cython.dataclasses.dataclass and dataclass field should just
669
854
  # shadow the standard library ones (if they are available)
855
+ if TYPE_CHECKING:
856
+ import dataclasses as dataclasses
670
857
  dataclasses = sys.modules['cython.dataclasses'] = CythonDotImportedFromElsewhere('dataclasses')
671
858
  del math, sys
672
859
 
673
- class pymutex:
674
- def __init__(self):
860
+
861
+ class _pymutex_base:
862
+ def __init__(self) -> None:
675
863
  import threading
676
864
  self._l = threading.Lock()
677
865
 
678
- def acquire(self):
866
+ def acquire(self) -> None:
679
867
  return self._l.acquire()
680
868
 
681
- def release(self):
869
+ def release(self) -> None:
682
870
  return self._l.release()
683
871
 
684
- def __enter__(self):
872
+ def locked(self) -> bool:
873
+ """
874
+ Check if the lock is currently held.
875
+ Returns True if locked, False otherwise.
876
+ """
877
+ return self._l.locked()
878
+
879
+ def can_check_locked(self) -> bool:
880
+ """
881
+ Check if the locked() method is available.
882
+ Always returns True - locked() is available on all Python versions.
883
+ """
884
+ return True
885
+
886
+ def __enter__(self) -> None:
685
887
  return self._l.__enter__()
686
888
 
687
- def __exit__(self, exc_type, exc_value, traceback):
688
- return self._l.__exit__(exc_type, exc_value, traceback)
889
+ def __exit__(self, exc_type: Optional[Type[BaseException]],
890
+ exc: Optional[BaseException],
891
+ tb: Optional[TracebackType]) -> None:
892
+ return self._l.__exit__(exc_type, exc, tb)
689
893
 
690
- pythread_type_lock = pymutex
894
+ class pymutex(_pymutex_base):
895
+ """
896
+ A low-cost mutex lock type.
897
+ In Python 3.13+, it uses the fast PyMutex implementation. In older Python versions,
898
+ it falls back to the heavier "PyThread_type_lock".
899
+ Can be used in 'with' statements and supports nogil context.
900
+ """
901
+ pass
902
+
903
+ class pythread_type_lock(_pymutex_base):
904
+ """
905
+ A mutex lock type using PyThread_type_lock.
906
+ Can be used in 'with' statements and supports nogil context.
907
+ Compatible with the Limited API.
908
+ """
909
+ pass
910
+
911
+
912
+ if TYPE_CHECKING:
913
+ const: TypeAlias = Annotated[_T, "cython.const"]
914
+ volatile: TypeAlias = Annotated[_T, "cython.volatile"]
915
+ restrict: TypeAlias = Annotated[_T, "cython.restrict"]
916
+
917
+ ##### START: GENERATED LIST OF GENERATED TYPES #####
918
+ # Generated by "Tools/cython-generate-shadow-pyi.py" on 2025-12-01 19:08:04.502148
919
+
920
+ const_bint : TypeAlias = const[bint]
921
+ p_const_bint = pointer[const[bint]]
922
+ pp_const_bint = pointer[pointer[const[bint]]]
923
+ ppp_const_bint = pointer[pointer[pointer[const[bint]]]]
924
+ p_bint = pointer[bint]
925
+ pp_bint = pointer[pointer[bint]]
926
+ ppp_bint = pointer[pointer[pointer[bint]]]
927
+ char : TypeAlias = py_int
928
+ const_char : TypeAlias = const[py_int]
929
+ p_const_char = pointer[const[py_int]]
930
+ pp_const_char = pointer[pointer[const[py_int]]]
931
+ ppp_const_char = pointer[pointer[pointer[const[py_int]]]]
932
+ p_char = pointer[py_int]
933
+ pp_char = pointer[pointer[py_int]]
934
+ ppp_char = pointer[pointer[pointer[py_int]]]
935
+ complex : TypeAlias = py_complex
936
+ const_complex : TypeAlias = const[py_complex]
937
+ p_const_complex = pointer[const[py_complex]]
938
+ pp_const_complex = pointer[pointer[const[py_complex]]]
939
+ ppp_const_complex = pointer[pointer[pointer[const[py_complex]]]]
940
+ p_complex = pointer[py_complex]
941
+ pp_complex = pointer[pointer[py_complex]]
942
+ ppp_complex = pointer[pointer[pointer[py_complex]]]
943
+ double : TypeAlias = py_float
944
+ const_double : TypeAlias = const[py_float]
945
+ p_const_double = pointer[const[py_float]]
946
+ pp_const_double = pointer[pointer[const[py_float]]]
947
+ ppp_const_double = pointer[pointer[pointer[const[py_float]]]]
948
+ p_double = pointer[py_float]
949
+ pp_double = pointer[pointer[py_float]]
950
+ ppp_double = pointer[pointer[pointer[py_float]]]
951
+ doublecomplex : TypeAlias = py_complex
952
+ const_doublecomplex : TypeAlias = const[py_complex]
953
+ p_const_doublecomplex = pointer[const[py_complex]]
954
+ pp_const_doublecomplex = pointer[pointer[const[py_complex]]]
955
+ ppp_const_doublecomplex = pointer[pointer[pointer[const[py_complex]]]]
956
+ p_doublecomplex = pointer[py_complex]
957
+ pp_doublecomplex = pointer[pointer[py_complex]]
958
+ ppp_doublecomplex = pointer[pointer[pointer[py_complex]]]
959
+ float : TypeAlias = py_float
960
+ const_float : TypeAlias = const[py_float]
961
+ p_const_float = pointer[const[py_float]]
962
+ pp_const_float = pointer[pointer[const[py_float]]]
963
+ ppp_const_float = pointer[pointer[pointer[const[py_float]]]]
964
+ p_float = pointer[py_float]
965
+ pp_float = pointer[pointer[py_float]]
966
+ ppp_float = pointer[pointer[pointer[py_float]]]
967
+ floatcomplex : TypeAlias = py_complex
968
+ const_floatcomplex : TypeAlias = const[py_complex]
969
+ p_const_floatcomplex = pointer[const[py_complex]]
970
+ pp_const_floatcomplex = pointer[pointer[const[py_complex]]]
971
+ ppp_const_floatcomplex = pointer[pointer[pointer[const[py_complex]]]]
972
+ p_floatcomplex = pointer[py_complex]
973
+ pp_floatcomplex = pointer[pointer[py_complex]]
974
+ ppp_floatcomplex = pointer[pointer[pointer[py_complex]]]
975
+ int : TypeAlias = py_int
976
+ const_int : TypeAlias = const[py_int]
977
+ p_const_int = pointer[const[py_int]]
978
+ pp_const_int = pointer[pointer[const[py_int]]]
979
+ ppp_const_int = pointer[pointer[pointer[const[py_int]]]]
980
+ p_int = pointer[py_int]
981
+ pp_int = pointer[pointer[py_int]]
982
+ ppp_int = pointer[pointer[pointer[py_int]]]
983
+ long : TypeAlias = py_int
984
+ const_long : TypeAlias = const[py_int]
985
+ p_const_long = pointer[const[py_int]]
986
+ pp_const_long = pointer[pointer[const[py_int]]]
987
+ ppp_const_long = pointer[pointer[pointer[const[py_int]]]]
988
+ p_long = pointer[py_int]
989
+ pp_long = pointer[pointer[py_int]]
990
+ ppp_long = pointer[pointer[pointer[py_int]]]
991
+ longdouble : TypeAlias = py_float
992
+ const_longdouble : TypeAlias = const[py_float]
993
+ p_const_longdouble = pointer[const[py_float]]
994
+ pp_const_longdouble = pointer[pointer[const[py_float]]]
995
+ ppp_const_longdouble = pointer[pointer[pointer[const[py_float]]]]
996
+ p_longdouble = pointer[py_float]
997
+ pp_longdouble = pointer[pointer[py_float]]
998
+ ppp_longdouble = pointer[pointer[pointer[py_float]]]
999
+ longdoublecomplex : TypeAlias = py_complex
1000
+ const_longdoublecomplex : TypeAlias = const[py_complex]
1001
+ p_const_longdoublecomplex = pointer[const[py_complex]]
1002
+ pp_const_longdoublecomplex = pointer[pointer[const[py_complex]]]
1003
+ ppp_const_longdoublecomplex = pointer[pointer[pointer[const[py_complex]]]]
1004
+ p_longdoublecomplex = pointer[py_complex]
1005
+ pp_longdoublecomplex = pointer[pointer[py_complex]]
1006
+ ppp_longdoublecomplex = pointer[pointer[pointer[py_complex]]]
1007
+ longlong : TypeAlias = py_int
1008
+ const_longlong : TypeAlias = const[py_int]
1009
+ p_const_longlong = pointer[const[py_int]]
1010
+ pp_const_longlong = pointer[pointer[const[py_int]]]
1011
+ ppp_const_longlong = pointer[pointer[pointer[const[py_int]]]]
1012
+ p_longlong = pointer[py_int]
1013
+ pp_longlong = pointer[pointer[py_int]]
1014
+ ppp_longlong = pointer[pointer[pointer[py_int]]]
1015
+ schar : TypeAlias = py_int
1016
+ const_schar : TypeAlias = const[py_int]
1017
+ p_const_schar = pointer[const[py_int]]
1018
+ pp_const_schar = pointer[pointer[const[py_int]]]
1019
+ ppp_const_schar = pointer[pointer[pointer[const[py_int]]]]
1020
+ p_schar = pointer[py_int]
1021
+ pp_schar = pointer[pointer[py_int]]
1022
+ ppp_schar = pointer[pointer[pointer[py_int]]]
1023
+ short : TypeAlias = py_int
1024
+ const_short : TypeAlias = const[py_int]
1025
+ p_const_short = pointer[const[py_int]]
1026
+ pp_const_short = pointer[pointer[const[py_int]]]
1027
+ ppp_const_short = pointer[pointer[pointer[const[py_int]]]]
1028
+ p_short = pointer[py_int]
1029
+ pp_short = pointer[pointer[py_int]]
1030
+ ppp_short = pointer[pointer[pointer[py_int]]]
1031
+ sint : TypeAlias = py_int
1032
+ const_sint : TypeAlias = const[py_int]
1033
+ p_const_sint = pointer[const[py_int]]
1034
+ pp_const_sint = pointer[pointer[const[py_int]]]
1035
+ ppp_const_sint = pointer[pointer[pointer[const[py_int]]]]
1036
+ p_sint = pointer[py_int]
1037
+ pp_sint = pointer[pointer[py_int]]
1038
+ ppp_sint = pointer[pointer[pointer[py_int]]]
1039
+ slong : TypeAlias = py_int
1040
+ const_slong : TypeAlias = const[py_int]
1041
+ p_const_slong = pointer[const[py_int]]
1042
+ pp_const_slong = pointer[pointer[const[py_int]]]
1043
+ ppp_const_slong = pointer[pointer[pointer[const[py_int]]]]
1044
+ p_slong = pointer[py_int]
1045
+ pp_slong = pointer[pointer[py_int]]
1046
+ ppp_slong = pointer[pointer[pointer[py_int]]]
1047
+ slonglong : TypeAlias = py_int
1048
+ const_slonglong : TypeAlias = const[py_int]
1049
+ p_const_slonglong = pointer[const[py_int]]
1050
+ pp_const_slonglong = pointer[pointer[const[py_int]]]
1051
+ ppp_const_slonglong = pointer[pointer[pointer[const[py_int]]]]
1052
+ p_slonglong = pointer[py_int]
1053
+ pp_slonglong = pointer[pointer[py_int]]
1054
+ ppp_slonglong = pointer[pointer[pointer[py_int]]]
1055
+ sshort : TypeAlias = py_int
1056
+ const_sshort : TypeAlias = const[py_int]
1057
+ p_const_sshort = pointer[const[py_int]]
1058
+ pp_const_sshort = pointer[pointer[const[py_int]]]
1059
+ ppp_const_sshort = pointer[pointer[pointer[const[py_int]]]]
1060
+ p_sshort = pointer[py_int]
1061
+ pp_sshort = pointer[pointer[py_int]]
1062
+ ppp_sshort = pointer[pointer[pointer[py_int]]]
1063
+ Py_hash_t : TypeAlias = py_int
1064
+ const_Py_hash_t : TypeAlias = const[py_int]
1065
+ p_const_Py_hash_t = pointer[const[py_int]]
1066
+ pp_const_Py_hash_t = pointer[pointer[const[py_int]]]
1067
+ ppp_const_Py_hash_t = pointer[pointer[pointer[const[py_int]]]]
1068
+ p_Py_hash_t = pointer[py_int]
1069
+ pp_Py_hash_t = pointer[pointer[py_int]]
1070
+ ppp_Py_hash_t = pointer[pointer[pointer[py_int]]]
1071
+ ptrdiff_t : TypeAlias = py_int
1072
+ const_ptrdiff_t : TypeAlias = const[py_int]
1073
+ p_const_ptrdiff_t = pointer[const[py_int]]
1074
+ pp_const_ptrdiff_t = pointer[pointer[const[py_int]]]
1075
+ ppp_const_ptrdiff_t = pointer[pointer[pointer[const[py_int]]]]
1076
+ p_ptrdiff_t = pointer[py_int]
1077
+ pp_ptrdiff_t = pointer[pointer[py_int]]
1078
+ ppp_ptrdiff_t = pointer[pointer[pointer[py_int]]]
1079
+ size_t : TypeAlias = py_int
1080
+ const_size_t : TypeAlias = const[py_int]
1081
+ p_const_size_t = pointer[const[py_int]]
1082
+ pp_const_size_t = pointer[pointer[const[py_int]]]
1083
+ ppp_const_size_t = pointer[pointer[pointer[const[py_int]]]]
1084
+ p_size_t = pointer[py_int]
1085
+ pp_size_t = pointer[pointer[py_int]]
1086
+ ppp_size_t = pointer[pointer[pointer[py_int]]]
1087
+ ssize_t : TypeAlias = py_int
1088
+ const_ssize_t : TypeAlias = const[py_int]
1089
+ p_const_ssize_t = pointer[const[py_int]]
1090
+ pp_const_ssize_t = pointer[pointer[const[py_int]]]
1091
+ ppp_const_ssize_t = pointer[pointer[pointer[const[py_int]]]]
1092
+ p_ssize_t = pointer[py_int]
1093
+ pp_ssize_t = pointer[pointer[py_int]]
1094
+ ppp_ssize_t = pointer[pointer[pointer[py_int]]]
1095
+ Py_ssize_t : TypeAlias = py_int
1096
+ const_Py_ssize_t : TypeAlias = const[py_int]
1097
+ p_const_Py_ssize_t = pointer[const[py_int]]
1098
+ pp_const_Py_ssize_t = pointer[pointer[const[py_int]]]
1099
+ ppp_const_Py_ssize_t = pointer[pointer[pointer[const[py_int]]]]
1100
+ p_Py_ssize_t = pointer[py_int]
1101
+ pp_Py_ssize_t = pointer[pointer[py_int]]
1102
+ ppp_Py_ssize_t = pointer[pointer[pointer[py_int]]]
1103
+ const_Py_tss_t : TypeAlias = const[Any]
1104
+ p_Py_tss_t = pointer[Any]
1105
+ pp_Py_tss_t = pointer[pointer[Any]]
1106
+ ppp_Py_tss_t = pointer[pointer[pointer[Any]]]
1107
+ uchar : TypeAlias = py_int
1108
+ const_uchar : TypeAlias = const[py_int]
1109
+ p_const_uchar = pointer[const[py_int]]
1110
+ pp_const_uchar = pointer[pointer[const[py_int]]]
1111
+ ppp_const_uchar = pointer[pointer[pointer[const[py_int]]]]
1112
+ p_uchar = pointer[py_int]
1113
+ pp_uchar = pointer[pointer[py_int]]
1114
+ ppp_uchar = pointer[pointer[pointer[py_int]]]
1115
+ const_Py_UCS4 : TypeAlias = const[py_int]
1116
+ p_const_Py_UCS4 = pointer[const[py_int]]
1117
+ pp_const_Py_UCS4 = pointer[pointer[const[py_int]]]
1118
+ ppp_const_Py_UCS4 = pointer[pointer[pointer[const[py_int]]]]
1119
+ p_Py_UCS4 = pointer[py_int]
1120
+ pp_Py_UCS4 = pointer[pointer[py_int]]
1121
+ ppp_Py_UCS4 = pointer[pointer[pointer[py_int]]]
1122
+ uint : TypeAlias = py_int
1123
+ const_uint : TypeAlias = const[py_int]
1124
+ p_const_uint = pointer[const[py_int]]
1125
+ pp_const_uint = pointer[pointer[const[py_int]]]
1126
+ ppp_const_uint = pointer[pointer[pointer[const[py_int]]]]
1127
+ p_uint = pointer[py_int]
1128
+ pp_uint = pointer[pointer[py_int]]
1129
+ ppp_uint = pointer[pointer[pointer[py_int]]]
1130
+ ulong : TypeAlias = py_int
1131
+ const_ulong : TypeAlias = const[py_int]
1132
+ p_const_ulong = pointer[const[py_int]]
1133
+ pp_const_ulong = pointer[pointer[const[py_int]]]
1134
+ ppp_const_ulong = pointer[pointer[pointer[const[py_int]]]]
1135
+ p_ulong = pointer[py_int]
1136
+ pp_ulong = pointer[pointer[py_int]]
1137
+ ppp_ulong = pointer[pointer[pointer[py_int]]]
1138
+ ulonglong : TypeAlias = py_int
1139
+ const_ulonglong : TypeAlias = const[py_int]
1140
+ p_const_ulonglong = pointer[const[py_int]]
1141
+ pp_const_ulonglong = pointer[pointer[const[py_int]]]
1142
+ ppp_const_ulonglong = pointer[pointer[pointer[const[py_int]]]]
1143
+ p_ulonglong = pointer[py_int]
1144
+ pp_ulonglong = pointer[pointer[py_int]]
1145
+ ppp_ulonglong = pointer[pointer[pointer[py_int]]]
1146
+ const_Py_UNICODE : TypeAlias = const[py_int]
1147
+ p_const_Py_UNICODE = pointer[const[py_int]]
1148
+ pp_const_Py_UNICODE = pointer[pointer[const[py_int]]]
1149
+ ppp_const_Py_UNICODE = pointer[pointer[pointer[const[py_int]]]]
1150
+ p_Py_UNICODE = pointer[py_int]
1151
+ pp_Py_UNICODE = pointer[pointer[py_int]]
1152
+ ppp_Py_UNICODE = pointer[pointer[pointer[py_int]]]
1153
+ ushort : TypeAlias = py_int
1154
+ const_ushort : TypeAlias = const[py_int]
1155
+ p_const_ushort = pointer[const[py_int]]
1156
+ pp_const_ushort = pointer[pointer[const[py_int]]]
1157
+ ppp_const_ushort = pointer[pointer[pointer[const[py_int]]]]
1158
+ p_ushort = pointer[py_int]
1159
+ pp_ushort = pointer[pointer[py_int]]
1160
+ ppp_ushort = pointer[pointer[pointer[py_int]]]
1161
+ const_void : TypeAlias = const[Any]
1162
+ p_void = pointer[Any]
1163
+ pp_void = pointer[pointer[Any]]
1164
+ ppp_void = pointer[pointer[pointer[Any]]]
1165
+
1166
+ ##### END: GENERATED LIST OF GENERATED TYPES #####
1167
+ pass