Skip to content

Commit 4ac0f82

Browse files
authored
Disable annotation_typing (#2902)
* Disable annotation_typing * Fix CI * Fix merge * Use "not None" where appropriate * Add pre-commit check for "not None" * Add tests
1 parent 166cd5a commit 4ac0f82

36 files changed

Lines changed: 303 additions & 115 deletions

‎.pre-commit-config.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ repos:
5858
exclude: '(.*pixi\.lock)|(\.git_archival\.txt)|(.*\.patch$)|(^cuda_core/cuda/core/_vendored/)|(^cuda_core/third_party/)'
5959
args: ["--fix"]
6060

61+
- id: check-cython-not-none
62+
name: Check cdef-class-annotated def parameters carry 'not None'
63+
entry: python ./toolshed/check_cython_not_none.py
64+
language: python
65+
files: ^cuda_core/cuda/.*\.pyx$
66+
6167
- id: check-generated-file-seals
6268
name: Check generated-file seals
6369
entry: python ./toolshed/check_generated_file_seals.py

‎cuda_core/build_hooks.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,12 @@ def module_names():
538538
"CUDA_CORE_BUILD_MAJOR": int(cuda_major),
539539
"CUDA_CORE_BINDINGS_FLOOR": tuple(_floor_for(cuda_major)),
540540
}
541-
compiler_directives = {"embedsignature": True, "warn.deprecated.IF": False, "freethreading_compatible": True}
541+
compiler_directives = {
542+
"embedsignature": True,
543+
"warn.deprecated.IF": False,
544+
"freethreading_compatible": True,
545+
"annotation_typing": False,
546+
}
542547
_CythonOptions.warning_errors = True
543548
if COMPILE_FOR_COVERAGE:
544549
compiler_directives["linetrace"] = True

‎cuda_core/cuda/core/_context.pyx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ from collections.abc import Sequence
88
from dataclasses import dataclass
99
from typing import TYPE_CHECKING
1010

11-
import cython
12-
1311
from cuda.bindings cimport cydriver
1412
from cuda.core._device_resources cimport DeviceResources, SMResource, WorkqueueResource
1513
from cuda.core._device_resources import SMResource, WorkqueueResource
@@ -101,7 +99,6 @@ cdef class Context:
10199
Context_check_open(self)
102100
return DeviceResources._init_from_ctx(self._h_context, self._device_id)
103101

104-
@cython.annotation_typing(False)
105102
def create_stream(self, options: StreamOptions | None = None) -> Stream:
106103
"""Create a new stream bound to this green context.
107104

‎cuda_core/cuda/core/_device.pyx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ from cuda.core._rt cimport (
3030
)
3131

3232
from cuda.core._stream import IsStreamType, Stream, StreamOptions
33-
from cuda.core._utils.clear_error_support import assert_type
3433
from cuda.core._utils.cuda_utils import (
3534
ComputeCapability,
3635
CUDAError,
@@ -1208,9 +1207,7 @@ class Device:
12081207
return self._memory_resource
12091208

12101209
@memory_resource.setter
1211-
def memory_resource(self, mr: MemoryResource) -> None:
1212-
from cuda.core._memory import MemoryResource
1213-
assert_type(mr, MemoryResource)
1210+
def memory_resource(self, MemoryResource mr not None: MemoryResource) -> None:
12141211
self._memory_resource = mr
12151212

12161213
@property
@@ -1247,7 +1244,7 @@ class Device:
12471244
def __reduce__(self) -> tuple[object, ...]:
12481245
return Device, (self.device_id,)
12491246

1250-
def set_current(self, ctx: Context | None = None) -> Context | None:
1247+
def set_current(self, Context ctx: Context | None = None) -> Context | None:
12511248
"""Set device to be used for GPU executions.
12521249

12531250
Initializes CUDA and sets the calling thread to a valid CUDA
@@ -1291,8 +1288,6 @@ class Device:
12911288
cdef Context prev_owned = None
12921289

12931290
if ctx is not None:
1294-
# TODO: revisit once Context is cythonized
1295-
assert_type(ctx, Context)
12961291
Context_check_open(ctx)
12971292
if ctx._device_id != self._device_id:
12981293
# The CUDA context stack is per-thread, not per-Device-object,

‎cuda_core/cuda/core/_device_resources.pyx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ cdef class SMResource:
475475

476476
def split(
477477
self,
478-
options: SMResourceOptions,
478+
SMResourceOptions options not None: SMResourceOptions,
479479
*,
480480
bint dry_run=False
481481
) -> tuple[list[SMResource], SMResource]:
@@ -587,7 +587,7 @@ cdef class WorkqueueResource:
587587
"WorkqueueResource requires cuda.core to be built with CUDA 13.x bindings"
588588
)
589589

590-
def configure(self, options: WorkqueueResourceOptions) -> None:
590+
def configure(self, WorkqueueResourceOptions options not None: WorkqueueResourceOptions) -> None:
591591
"""Configure the workqueue resource in place.
592592

593593
Parameters

‎cuda_core/cuda/core/_event.pyx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ cdef class Event:
168168
def __rsub__(self, other: object):
169169
return NotImplemented
170170

171-
def __sub__(self, other: Event) -> float:
171+
def __sub__(self, Event other not None: Event) -> float:
172172
# return self - other (in milliseconds)
173173
Event_check_open(self)
174174
cdef Event other_event = Event_accept(other)
@@ -228,7 +228,7 @@ cdef class Event:
228228
return self._ipc_descriptor
229229

230230
@classmethod
231-
def from_ipc_descriptor(cls, ipc_descriptor: IPCEventDescriptor) -> Event:
231+
def from_ipc_descriptor(cls, IPCEventDescriptor ipc_descriptor not None: IPCEventDescriptor) -> Event:
232232
"""Import an event that was exported from another process.
233233

234234
Parameters
@@ -377,7 +377,7 @@ cdef class IPCEventDescriptor:
377377
return IPCEventDescriptor._init, (self._reserved, self._is_blocking_sync)
378378

379379

380-
def _reduce_event(event: Event) -> tuple[object, ...]:
380+
def _reduce_event(Event event not None: Event) -> tuple[object, ...]:
381381
check_multiprocessing_start_method()
382382
return event.from_ipc_descriptor, (event.ipc_descriptor,)
383383

‎cuda_core/cuda/core/_graphics.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ cdef class GraphicsResource:
223223
return None
224224
return self._mapped_buffer
225225

226-
def map(self, *, stream: Stream) -> Buffer:
226+
def map(self, *, Stream stream not None: Stream) -> Buffer:
227227
"""Map this graphics resource for CUDA access.
228228

229229
After mapping, a CUDA device pointer into the underlying graphics

‎cuda_core/cuda/core/_launcher.pyx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ __all__ = ['launch']
2929

3030
def launch(
3131
stream: Stream | GraphBuilder | IsStreamType,
32-
config: LaunchConfig,
33-
kernel: Kernel,
32+
LaunchConfig config not None: LaunchConfig,
33+
Kernel kernel not None: Kernel,
3434
*kernel_args
3535
) -> None:
3636
"""Launches a :obj:`~_module.Kernel`
@@ -59,8 +59,7 @@ def launch(
5959
cdef ParamHolder ker_args = ParamHolder(kernel_args)
6060
cdef void** args_ptr = <void**><uintptr_t>(ker_args.ptr)
6161

62-
cdef Kernel ker = <Kernel>kernel
63-
cdef cydriver.CUfunction func_handle = <cydriver.CUfunction>as_cu(ker._h_kernel)
62+
cdef cydriver.CUfunction func_handle = <cydriver.CUfunction>as_cu(kernel._h_kernel)
6463

6564
drv_cfg = conf._to_native_launch_config()
6665
drv_cfg.hStream = as_cu(s._h_stream)

‎cuda_core/cuda/core/_layout.pyx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ cdef class _StridedLayout:
9696
@classmethod
9797
def dense_like(
9898
cls,
99-
other : _StridedLayout,
99+
_StridedLayout other not None : _StridedLayout,
100100
stride_order : str | tuple[int] = "K"
101101
) -> _StridedLayout:
102102
"""

‎cuda_core/cuda/core/_memory/_buffer.pyx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ cdef class Buffer:
404404
@classmethod
405405
def from_ipc_descriptor(
406406
cls, mr: DeviceMemoryResource | PinnedMemoryResource | VirtualMemoryResource,
407-
ipc_descriptor: IPCBufferDescriptor,
408-
*, stream: Stream
407+
IPCBufferDescriptor ipc_descriptor not None: IPCBufferDescriptor,
408+
*, Stream stream not None: Stream
409409
) -> Buffer:
410410
"""Import a buffer that was exported from another process.
411411

@@ -505,7 +505,7 @@ cdef class Buffer:
505505
self.close()
506506
return False
507507

508-
def copy_to(self, dst: Buffer | None = None, *, stream: Stream | GraphBuilder,
508+
def copy_to(self, Buffer dst: Buffer | None = None, *, stream: Stream | GraphBuilder,
509509
options: CopyOptions | None = None) -> Buffer:
510510
"""Copy from this buffer to the dst buffer asynchronously on the given stream.
511511

@@ -555,7 +555,7 @@ cdef class Buffer:
555555
"buffer does not have a memory_resource)")
556556
dst = self._memory_resource.allocate(src_size, stream=s)
557557
else:
558-
Buffer_check_open(<Buffer>dst)
558+
Buffer_check_open(dst)
559559

560560
cdef size_t dst_size = dst._size
561561
if dst_size != src_size:
@@ -566,7 +566,7 @@ cdef class Buffer:
566566
as_cu(dst._h_ptr), as_cu(self._h_ptr), src_size, s, options, "copy_to")
567567
return dst
568568

569-
def copy_from(self, src: Buffer, *, stream: Stream | GraphBuilder,
569+
def copy_from(self, Buffer src not None: Buffer, *, stream: Stream | GraphBuilder,
570570
options: CopyOptions | None = None) -> None:
571571
"""Copy from the src buffer to this buffer asynchronously on the given stream.
572572

0 commit comments

Comments
 (0)