Skip to content

Commit e488097

Browse files
committed
Adopt PEP 563 annotations and modern union syntax
Use `from __future__ import annotations` consistently and replace `Union[X, Y]` with `X | Y` and `Optional[X]` with `X | None` in type annotations where possible. TYPE_CHECKING blocks are retained only where needed to satisfy linting (forward references for circular imports).
1 parent 3257477 commit e488097

15 files changed

Lines changed: 50 additions & 71 deletions

cuda_core/cuda/core/_device.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
from __future__ import annotations
6+
57
cimport cpython
68
from libc.stdint cimport uintptr_t
79

810
from cuda.bindings cimport cydriver
911
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
1012

1113
import threading
12-
from typing import TYPE_CHECKING
1314

1415
from cuda.core._context cimport Context
1516
from cuda.core._context import ContextOptions
@@ -34,9 +35,6 @@ from cuda.core._utils.cuda_utils import (
3435
)
3536
from cuda.core._stream cimport default_stream
3637

37-
if TYPE_CHECKING:
38-
from cuda.core._memory import Buffer, MemoryResource
39-
4038
# TODO: I prefer to type these as "cdef object" and avoid accessing them from within Python,
4139
# but it seems it is very convenient to expose them for testing purposes...
4240
_tls = threading.local()

cuda_core/cuda/core/_event.pyx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ from cuda.core._utils.cuda_utils cimport (
2626
import cython
2727
from dataclasses import dataclass
2828
import multiprocessing
29-
from typing import TYPE_CHECKING, Optional
3029

3130
from cuda.core._utils.cuda_utils import (
3231
CUDAError,
3332
check_multiprocessing_start_method,
3433
)
35-
if TYPE_CHECKING:
36-
import cuda.bindings
3734

3835

3936
@dataclass
@@ -56,9 +53,9 @@ cdef class EventOptions:
5653
5754
"""
5855

59-
enable_timing: Optional[bool] = False
60-
busy_waited_sync: Optional[bool] = False
61-
ipc_enabled: Optional[bool] = False
56+
enable_timing: bool | None = False
57+
busy_waited_sync: bool | None = False
58+
ipc_enabled: bool | None = False
6259

6360

6461
cdef class Event:

cuda_core/cuda/core/_graph.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
if TYPE_CHECKING:
1212
from cuda.core._stream import Stream
13+
1314
from cuda.core._utils.cuda_utils import (
1415
driver,
1516
get_binding_version,

cuda_core/cuda/core/_linker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ class LinkerOptions:
191191
prec_div: bool | None = None
192192
prec_sqrt: bool | None = None
193193
fma: bool | None = None
194-
kernels_used: Union[str, tuple[str], list[str]] | None = None
195-
variables_used: Union[str, tuple[str], list[str]] | None = None
194+
kernels_used: str | tuple[str] | list[str] | None = None
195+
variables_used: str | tuple[str] | list[str] | None = None
196196
optimize_unused_variables: bool | None = None
197-
ptxas_options: Union[str, tuple[str], list[str]] | None = None
197+
ptxas_options: str | tuple[str] | list[str] | None = None
198198
split_compile: int | None = None
199199
split_compile_extended: int | None = None
200200
no_cache: bool | None = None
@@ -203,7 +203,7 @@ def __post_init__(self):
203203
_lazy_init()
204204
self._name = self.name.encode()
205205

206-
def _prepare_nvjitlink_options(self, as_bytes: bool = False) -> Union[list[bytes], list[str]]:
206+
def _prepare_nvjitlink_options(self, as_bytes: bool = False) -> list[bytes] | list[str]:
207207
options = []
208208

209209
if self.arch is not None:

cuda_core/cuda/core/_memory/_buffer.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ from cuda.core._stream cimport Stream_accept, Stream
2626
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2727

2828
import sys
29-
from typing import TypeVar, Union
29+
from typing import TypeVar
3030

3131
if sys.version_info >= (3, 12):
3232
from collections.abc import Buffer as BufferProtocol
@@ -40,7 +40,7 @@ from cuda.core._device import Device
4040
__all__ = ['Buffer', 'MemoryResource']
4141

4242

43-
DevicePointerT = Union[driver.CUdeviceptr, int, None]
43+
DevicePointerT = driver.CUdeviceptr | int | None
4444
"""
4545
A type union of :obj:`~driver.CUdeviceptr`, `int` and `None` for hinting
4646
:attr:`Buffer.handle`.

cuda_core/cuda/core/_memory/_device_memory_resource.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ from cuda.core._utils.cuda_utils cimport (
1515

1616
from dataclasses import dataclass
1717
import multiprocessing
18-
from typing import TYPE_CHECKING
1918
import platform # no-cython-lint
2019
import uuid
2120

2221
from cuda.core._utils.cuda_utils import check_multiprocessing_start_method
2322
from cuda.core._resource_handles cimport as_cu
2423

25-
if TYPE_CHECKING:
26-
from .._device import Device
27-
2824
__all__ = ['DeviceMemoryResource', 'DeviceMemoryResourceOptions']
2925

3026

cuda_core/cuda/core/_memory/_graph_memory_resource.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ from cuda.core._stream cimport default_stream, Stream_accept, Stream
1818
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
1919

2020
from functools import cache
21-
from typing import TYPE_CHECKING
22-
23-
if TYPE_CHECKING:
24-
from cuda.core._memory.buffer import DevicePointerT
2521

2622
__all__ = ['GraphMemoryResource']
2723

cuda_core/cuda/core/_memory/_legacy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from typing import TYPE_CHECKING
88

9+
if TYPE_CHECKING:
10+
from cuda.core._memory._buffer import DevicePointerT
11+
912
from cuda.core._memory._buffer import Buffer, MemoryResource
1013
from cuda.core._utils.cuda_utils import (
1114
_check_driver_error as raise_if_driver_error,
@@ -14,9 +17,6 @@
1417
driver,
1518
)
1619

17-
if TYPE_CHECKING:
18-
from cuda.core._memory.buffer import DevicePointerT
19-
2020
__all__ = ["LegacyPinnedMemoryResource", "_SynchronousMemoryResource"]
2121

2222

cuda_core/cuda/core/_memory/_managed_memory_resource.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ from cuda.core._utils.cuda_utils cimport (
1111
)
1212

1313
from dataclasses import dataclass
14-
from typing import Optional
1514

1615
__all__ = ['ManagedMemoryResource', 'ManagedMemoryResourceOptions']
1716

@@ -28,7 +27,7 @@ cdef class ManagedMemoryResourceOptions:
2827
or None to let the driver decide.
2928
(Default to None)
3029
"""
31-
preferred_location : Optional[int] = None
30+
preferred_location: int | None = None
3231

3332

3433
cdef class ManagedMemoryResource(_MemPool):

cuda_core/cuda/core/_memory/_memory_pool.pyx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ from cuda.core._utils.cuda_utils cimport (
2828
HANDLE_RETURN,
2929
)
3030

31-
from typing import TYPE_CHECKING
3231
import platform # no-cython-lint
3332
import weakref
3433

3534
from cuda.core._utils.cuda_utils import driver
3635

37-
if TYPE_CHECKING:
38-
from cuda.core._memory.buffer import DevicePointerT
39-
from .._device import Device
40-
4136

4237
cdef class _MemPoolOptions:
4338

@@ -302,7 +297,7 @@ cdef class _MemPool(MemoryResource):
302297
return self._ipc_data is not None and self._ipc_data._is_mapped
303298

304299
@property
305-
def uuid(self) -> Optional[uuid.UUID]:
300+
def uuid(self) -> uuid.UUID | None:
306301
"""
307302
A universally unique identifier for this memory resource. Meaningful
308303
only for IPC-enabled memory resources.

0 commit comments

Comments
 (0)