From 497306e0c3628d222334c1a076aa054e8910e361 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 8 Aug 2021 14:33:15 +0300 Subject: [PATCH 1/4] use new union syntax in ctypes/__init__.pyi --- stdlib/ctypes/__init__.pyi | 50 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 131bb4d7e1c0..2f57becab14d 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -38,19 +38,19 @@ class CDLL(object): if sys.version_info >= (3, 8): def __init__( self, - name: Optional[str], + name: str | None, mode: int = ..., - handle: Optional[int] = ..., + handle: int | None = ..., use_errno: bool = ..., use_last_error: bool = ..., - winmode: Optional[int] = ..., + winmode: int | None = ..., ) -> None: ... else: def __init__( self, - name: Optional[str], + name: str | None, mode: int = ..., - handle: Optional[int] = ..., + handle: int | None = ..., use_errno: bool = ..., use_last_error: bool = ..., ) -> None: ... @@ -95,7 +95,7 @@ class _CDataMeta(type): class _CData(metaclass=_CDataMeta): _b_base: int = ... _b_needsfree_: bool = ... - _objects: Optional[Mapping[Any, int]] = ... + _objects: Mapping[Any, int] | None = ... @classmethod def from_buffer(cls: Type[_CT], source: _WritableBuffer, offset: int = ...) -> _CT: ... @classmethod @@ -103,7 +103,7 @@ class _CData(metaclass=_CDataMeta): @classmethod def from_address(cls: Type[_CT], address: int) -> _CT: ... @classmethod - def from_param(cls: Type[_CT], obj: Any) -> _UnionT[_CT, _CArgObject]: ... + def from_param(cls: Type[_CT], obj: Any) -> _CT | _CArgObject: ... @classmethod def in_dll(cls: Type[_CT], library: CDLL, name: str) -> _CT: ... @@ -114,7 +114,7 @@ _ECT = Callable[[Optional[Type[_CData]], _FuncPointer, Tuple[_CData, ...]], _CDa _PF = _UnionT[Tuple[int], Tuple[int, str], Tuple[int, str, Any]] class _FuncPointer(_PointerLike, _CData): - restype: _UnionT[Type[_CData], Callable[[int], Any], None] = ... + restype: Type[_CData] | Callable[[int], Any] | None = ... argtypes: Sequence[Type[_CData]] = ... errcheck: _ECT = ... @overload @@ -122,7 +122,7 @@ class _FuncPointer(_PointerLike, _CData): @overload def __init__(self, callable: Callable[..., Any]) -> None: ... @overload - def __init__(self, func_spec: Tuple[_UnionT[str, int], CDLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... + def __init__(self, func_spec: Tuple[str | int, CDLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... @overload def __init__(self, vtlb_index: int, name: str, paramflags: Tuple[_PF, ...] = ..., iid: pointer[c_int] = ...) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... @@ -133,15 +133,15 @@ class _NamedFuncPointer(_FuncPointer): class ArgumentError(Exception): ... def CFUNCTYPE( - restype: Optional[Type[_CData]], *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ... + restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ... ) -> Type[_FuncPointer]: ... if sys.platform == "win32": def WINFUNCTYPE( - restype: Optional[Type[_CData]], *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ... + restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ... ) -> Type[_FuncPointer]: ... -def PYFUNCTYPE(restype: Optional[Type[_CData]], *argtypes: Type[_CData]) -> Type[_FuncPointer]: ... +def PYFUNCTYPE(restype: Type[_CData] | None, *argtypes: Type[_CData]) -> Type[_FuncPointer]: ... class _CArgObject: ... @@ -155,17 +155,17 @@ _CVoidPLike = _UnionT[_PointerLike, Array[Any], _CArgObject, int] _CVoidConstPLike = _UnionT[_CVoidPLike, bytes] def addressof(obj: _CData) -> int: ... -def alignment(obj_or_type: _UnionT[_CData, Type[_CData]]) -> int: ... +def alignment(obj_or_type: _CData | Type[_CData]) -> int: ... def byref(obj: _CData, offset: int = ...) -> _CArgObject: ... _CastT = TypeVar("_CastT", bound=_CanCastTo) -def cast(obj: _UnionT[_CData, _CArgObject, int], typ: Type[_CastT]) -> _CastT: ... -def create_string_buffer(init: _UnionT[int, bytes], size: Optional[int] = ...) -> Array[c_char]: ... +def cast(obj: _CData | _CArgObject | int, typ: Type[_CastT]) -> _CastT: ... +def create_string_buffer(init: int | bytes, size: int | None = ...) -> Array[c_char]: ... c_buffer = create_string_buffer -def create_unicode_buffer(init: _UnionT[int, str], size: Optional[int] = ...) -> Array[c_wchar]: ... +def create_unicode_buffer(init: int | str, size: int | None = ...) -> Array[c_wchar]: ... if sys.platform == "win32": def DllCanUnloadNow() -> int: ... @@ -204,11 +204,11 @@ def set_errno(value: int) -> int: ... if sys.platform == "win32": def set_last_error(value: int) -> int: ... -def sizeof(obj_or_type: _UnionT[_CData, Type[_CData]]) -> int: ... +def sizeof(obj_or_type: _CData | Type[_CData]) -> int: ... def string_at(address: _CVoidConstPLike, size: int = ...) -> bytes: ... if sys.platform == "win32": - def WinError(code: Optional[int] = ..., descr: Optional[str] = ...) -> OSError: ... + def WinError(code: int | None = ..., descr: str | None = ...) -> OSError: ... def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ... @@ -219,10 +219,10 @@ class _SimpleCData(Generic[_T], _CData): class c_byte(_SimpleCData[int]): ... class c_char(_SimpleCData[bytes]): - def __init__(self, value: _UnionT[int, bytes] = ...) -> None: ... + def __init__(self, value: int | bytes = ...) -> None: ... -class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]): - def __init__(self, value: Optional[_UnionT[int, bytes]] = ...) -> None: ... +class c_char_p(_PointerLike, _SimpleCData[bytes | None]): + def __init__(self, value: int | bytes | None = ...) -> None: ... class c_double(_SimpleCData[float]): ... class c_longdouble(_SimpleCData[float]): ... @@ -246,11 +246,11 @@ class c_uint64(_SimpleCData[int]): ... class c_ulong(_SimpleCData[int]): ... class c_ulonglong(_SimpleCData[int]): ... class c_ushort(_SimpleCData[int]): ... -class c_void_p(_PointerLike, _SimpleCData[Optional[int]]): ... +class c_void_p(_PointerLike, _SimpleCData[int | None]): ... class c_wchar(_SimpleCData[str]): ... -class c_wchar_p(_PointerLike, _SimpleCData[Optional[str]]): - def __init__(self, value: Optional[_UnionT[int, str]] = ...) -> None: ... +class c_wchar_p(_PointerLike, _SimpleCData[str | None]): + def __init__(self, value: int | str | None = ...) -> None: ... class c_bool(_SimpleCData[bool]): def __init__(self, value: bool = ...) -> None: ... @@ -265,7 +265,7 @@ class _CField: size: int = ... class _StructUnionMeta(_CDataMeta): - _fields_: Sequence[_UnionT[Tuple[str, Type[_CData]], Tuple[str, Type[_CData], int]]] = ... + _fields_: Sequence[Tuple[str, Type[_CData]] | Tuple[str, Type[_CData], int]] = ... _pack_: int = ... _anonymous_: Sequence[str] = ... def __getattr__(self, name: str) -> _CField: ... From 85f65d84b03299110eb3771f30f6302d9fef674c Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 8 Aug 2021 14:40:38 +0300 Subject: [PATCH 2/4] back to old syntax in base classes --- stdlib/ctypes/__init__.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 2f57becab14d..baa98c81d461 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -221,7 +221,7 @@ class c_byte(_SimpleCData[int]): ... class c_char(_SimpleCData[bytes]): def __init__(self, value: int | bytes = ...) -> None: ... -class c_char_p(_PointerLike, _SimpleCData[bytes | None]): +class c_char_p(_PointerLike, _SimpleCData[Union[bytes, None]]): def __init__(self, value: int | bytes | None = ...) -> None: ... class c_double(_SimpleCData[float]): ... @@ -246,10 +246,10 @@ class c_uint64(_SimpleCData[int]): ... class c_ulong(_SimpleCData[int]): ... class c_ulonglong(_SimpleCData[int]): ... class c_ushort(_SimpleCData[int]): ... -class c_void_p(_PointerLike, _SimpleCData[int | None]): ... +class c_void_p(_PointerLike, _SimpleCData[Union[int, None]]): ... class c_wchar(_SimpleCData[str]): ... -class c_wchar_p(_PointerLike, _SimpleCData[str | None]): +class c_wchar_p(_PointerLike, _SimpleCData[Union[str, None]]): def __init__(self, value: int | str | None = ...) -> None: ... class c_bool(_SimpleCData[bool]): From e17ee74aed9bc056e87aee4040436ca3086f1574 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 8 Aug 2021 14:40:48 +0300 Subject: [PATCH 3/4] black --- stdlib/ctypes/__init__.pyi | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index baa98c81d461..a8ad555eaac3 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -47,12 +47,7 @@ class CDLL(object): ) -> None: ... else: def __init__( - self, - name: str | None, - mode: int = ..., - handle: int | None = ..., - use_errno: bool = ..., - use_last_error: bool = ..., + self, name: str | None, mode: int = ..., handle: int | None = ..., use_errno: bool = ..., use_last_error: bool = ... ) -> None: ... def __getattr__(self, name: str) -> _NamedFuncPointer: ... def __getitem__(self, name: str) -> _NamedFuncPointer: ... From 9d754b8cfef699e69bce5caa3684a8b66baae0fb Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 8 Aug 2021 14:47:11 +0300 Subject: [PATCH 4/4] fixening --- stdlib/ctypes/__init__.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index a8ad555eaac3..e678ff5c3bf0 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -216,7 +216,7 @@ class c_byte(_SimpleCData[int]): ... class c_char(_SimpleCData[bytes]): def __init__(self, value: int | bytes = ...) -> None: ... -class c_char_p(_PointerLike, _SimpleCData[Union[bytes, None]]): +class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]): def __init__(self, value: int | bytes | None = ...) -> None: ... class c_double(_SimpleCData[float]): ... @@ -241,10 +241,10 @@ class c_uint64(_SimpleCData[int]): ... class c_ulong(_SimpleCData[int]): ... class c_ulonglong(_SimpleCData[int]): ... class c_ushort(_SimpleCData[int]): ... -class c_void_p(_PointerLike, _SimpleCData[Union[int, None]]): ... +class c_void_p(_PointerLike, _SimpleCData[Optional[int]]): ... class c_wchar(_SimpleCData[str]): ... -class c_wchar_p(_PointerLike, _SimpleCData[Union[str, None]]): +class c_wchar_p(_PointerLike, _SimpleCData[Optional[str]]): def __init__(self, value: int | str | None = ...) -> None: ... class c_bool(_SimpleCData[bool]):