From 17c218326dfd80a2e8337e485a5d7de5abd1c0af Mon Sep 17 00:00:00 2001 From: Oswin Chou Date: Wed, 16 Aug 2017 23:32:40 -0700 Subject: [PATCH 1/5] Add stubs for cython This enables us to do type annotations for Cython's built-in C types. For example, def foo(s: cython.str) -> cython.int: ... --- third_party/3/Cython/Shadow.pyi | 83 +++++++++++++++++++++++++++++++ third_party/3/Cython/__init__.pyi | 1 + third_party/3/cython.pyi | 1 + 3 files changed, 85 insertions(+) create mode 100644 third_party/3/Cython/Shadow.pyi create mode 100644 third_party/3/Cython/__init__.pyi create mode 100644 third_party/3/cython.pyi diff --git a/third_party/3/Cython/Shadow.pyi b/third_party/3/Cython/Shadow.pyi new file mode 100644 index 000000000000..a285ddf65695 --- /dev/null +++ b/third_party/3/Cython/Shadow.pyi @@ -0,0 +1,83 @@ +from builtins import (int as py_int, float as py_float, + bool as py_bool, str as py_str) +from typing import (Union, Dict, Any, Sequence, Optional, + List, TypeVar, Type, Generic) + +int = py_int +long = py_int +longlong = py_int +short = py_int +char = py_int +sint = py_int +slong = py_int +slonglong = py_int +sshort = py_int +schar = py_int +uint = py_int +ulong = py_int +ulonglong = py_int +ushort = py_int +uchar = py_int +size_t = py_int +Py_ssize_t = py_int +float = py_float +double = py_float +longdouble = py_float +bint = py_bool +void = Union[None] +basestring = py_str + +gs: Dict[str, Any] # Should match the return type of globals() + +T = TypeVar('T') + +class _ArrayType(object, Generic[T]): + is_array: bool + subtypes: Sequence[str] + dtype: T + ndim: int + is_c_contig: bool + is_f_contig: bool + inner_contig: bool + broadcasting: Any + + # broadcasting is not used, so it's not clear about its type + def __init__(self, dtype: T, ndim: int, is_c_contig: bool = ..., + is_f_contig: bool = ..., inner_contig: bool = ..., + broadcasting: Any = ...) -> None: ... + def __repr__(self) -> str: ... + +class CythonTypeObject(object): + ... + +class CythonType(CythonTypeObject): + ... + +class PointerType(CythonType, Generic[T]): + def __init__( + self, + value: Optional[Union[ArrayType[T], + PointerType[T], List[T], int]] = ... + ) -> None: ... + def __getitem__(self, ix: int) -> T: ... + def __setitem__(self, ix: int, value: T) -> None: ... + def __eq__(self, value: object) -> bool: ... + def __repr__(self) -> str: ... + +class ArrayType(PointerType[T]): + def __init__(self) -> None: ... + +def index_type( + base_type: T, item: Union[tuple, slice, int]) -> _ArrayType[T]: ... + +def pointer(basetype: T) -> Type[PointerType[T]]: ... + +def array(basetype: T, n: int) -> Type[ArrayType[T]]: ... + +class typedef(CythonType, Generic[T]): + name: str + + def __init__(self, type: T, name: Optional[str] = ...) -> None: ... + def __call__(self, *arg) -> T: ... + def __repr__(self) -> str: ... + __getitem__ = index_type diff --git a/third_party/3/Cython/__init__.pyi b/third_party/3/Cython/__init__.pyi new file mode 100644 index 000000000000..623999f77769 --- /dev/null +++ b/third_party/3/Cython/__init__.pyi @@ -0,0 +1 @@ +from .Shadow import * diff --git a/third_party/3/cython.pyi b/third_party/3/cython.pyi new file mode 100644 index 000000000000..5f7cc82d57c1 --- /dev/null +++ b/third_party/3/cython.pyi @@ -0,0 +1 @@ +from Cython.Shadow import * From b47658c7be5ee050fa13bf9e48b0f2380e9365ec Mon Sep 17 00:00:00 2001 From: Oswin Chou Date: Thu, 17 Aug 2017 00:12:19 -0700 Subject: [PATCH 2/5] Correct the naming for a private TypeVar --- third_party/3/Cython/Shadow.pyi | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/third_party/3/Cython/Shadow.pyi b/third_party/3/Cython/Shadow.pyi index a285ddf65695..4f4de0fba5b7 100644 --- a/third_party/3/Cython/Shadow.pyi +++ b/third_party/3/Cython/Shadow.pyi @@ -29,12 +29,12 @@ basestring = py_str gs: Dict[str, Any] # Should match the return type of globals() -T = TypeVar('T') +_T = TypeVar('T') -class _ArrayType(object, Generic[T]): +class _ArrayType(object, Generic[_T]): is_array: bool subtypes: Sequence[str] - dtype: T + dtype: _T ndim: int is_c_contig: bool is_f_contig: bool @@ -42,7 +42,7 @@ class _ArrayType(object, Generic[T]): broadcasting: Any # broadcasting is not used, so it's not clear about its type - def __init__(self, dtype: T, ndim: int, is_c_contig: bool = ..., + def __init__(self, dtype: _T, ndim: int, is_c_contig: bool = ..., is_f_contig: bool = ..., inner_contig: bool = ..., broadcasting: Any = ...) -> None: ... def __repr__(self) -> str: ... @@ -53,31 +53,31 @@ class CythonTypeObject(object): class CythonType(CythonTypeObject): ... -class PointerType(CythonType, Generic[T]): +class PointerType(CythonType, Generic[_T]): def __init__( self, - value: Optional[Union[ArrayType[T], - PointerType[T], List[T], int]] = ... + value: Optional[Union[ArrayType[_T], + PointerType[_T], List[_T], int]] = ... ) -> None: ... - def __getitem__(self, ix: int) -> T: ... - def __setitem__(self, ix: int, value: T) -> None: ... + def __getitem__(self, ix: int) -> _T: ... + def __setitem__(self, ix: int, value: _T) -> None: ... def __eq__(self, value: object) -> bool: ... def __repr__(self) -> str: ... -class ArrayType(PointerType[T]): +class ArrayType(PointerType[_T]): def __init__(self) -> None: ... def index_type( - base_type: T, item: Union[tuple, slice, int]) -> _ArrayType[T]: ... + base_type: _T, item: Union[tuple, slice, int]) -> _ArrayType[_T]: ... -def pointer(basetype: T) -> Type[PointerType[T]]: ... +def pointer(basetype: _T) -> Type[PointerType[_T]]: ... -def array(basetype: T, n: int) -> Type[ArrayType[T]]: ... +def array(basetype: _T, n: int) -> Type[ArrayType[_T]]: ... -class typedef(CythonType, Generic[T]): +class typedef(CythonType, Generic[_T]): name: str - def __init__(self, type: T, name: Optional[str] = ...) -> None: ... - def __call__(self, *arg) -> T: ... + def __init__(self, type: _T, name: Optional[str] = ...) -> None: ... + def __call__(self, *arg) -> _T: ... def __repr__(self) -> str: ... __getitem__ = index_type From fc406a5ebc817753192f985e5c03cf8cc38458b1 Mon Sep 17 00:00:00 2001 From: Oswin Chou Date: Thu, 17 Aug 2017 14:19:02 -0700 Subject: [PATCH 3/5] The argument of TypeVar should match the variable name --- third_party/3/Cython/Shadow.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/3/Cython/Shadow.pyi b/third_party/3/Cython/Shadow.pyi index 4f4de0fba5b7..a6d960126eaf 100644 --- a/third_party/3/Cython/Shadow.pyi +++ b/third_party/3/Cython/Shadow.pyi @@ -29,7 +29,7 @@ basestring = py_str gs: Dict[str, Any] # Should match the return type of globals() -_T = TypeVar('T') +_T = TypeVar('_T') class _ArrayType(object, Generic[_T]): is_array: bool From 157e6956917c83ad97d55a0a6b1d072d7d25c962 Mon Sep 17 00:00:00 2001 From: Oswin Chou Date: Mon, 21 Aug 2017 16:16:35 -0700 Subject: [PATCH 4/5] Make cython.pyi available for both 2 and 3 --- third_party/{3 => 2and3}/Cython/Shadow.pyi | 0 third_party/{3 => 2and3}/Cython/__init__.pyi | 0 third_party/{3 => 2and3}/cython.pyi | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename third_party/{3 => 2and3}/Cython/Shadow.pyi (100%) rename third_party/{3 => 2and3}/Cython/__init__.pyi (100%) rename third_party/{3 => 2and3}/cython.pyi (100%) diff --git a/third_party/3/Cython/Shadow.pyi b/third_party/2and3/Cython/Shadow.pyi similarity index 100% rename from third_party/3/Cython/Shadow.pyi rename to third_party/2and3/Cython/Shadow.pyi diff --git a/third_party/3/Cython/__init__.pyi b/third_party/2and3/Cython/__init__.pyi similarity index 100% rename from third_party/3/Cython/__init__.pyi rename to third_party/2and3/Cython/__init__.pyi diff --git a/third_party/3/cython.pyi b/third_party/2and3/cython.pyi similarity index 100% rename from third_party/3/cython.pyi rename to third_party/2and3/cython.pyi From ac5cf961216735be9674036092ca104a3c525eea Mon Sep 17 00:00:00 2001 From: Oswin Chou Date: Sat, 2 Sep 2017 17:34:05 -0400 Subject: [PATCH 5/5] Type varargs with Any --- third_party/2and3/Cython/Shadow.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/2and3/Cython/Shadow.pyi b/third_party/2and3/Cython/Shadow.pyi index a6d960126eaf..b5211909ae40 100644 --- a/third_party/2and3/Cython/Shadow.pyi +++ b/third_party/2and3/Cython/Shadow.pyi @@ -78,6 +78,6 @@ class typedef(CythonType, Generic[_T]): name: str def __init__(self, type: _T, name: Optional[str] = ...) -> None: ... - def __call__(self, *arg) -> _T: ... + def __call__(self, *arg: Any) -> _T: ... def __repr__(self) -> str: ... __getitem__ = index_type