[ https://issues.apache.org/jira/browse/ARROW-15765?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17500816#comment-17500816 ]
Vibhatha Lakmal Abeykoon edited comment on ARROW-15765 at 3/28/22, 10:18 AM: ----------------------------------------------------------------------------- [~apitrou] [~jorisvandenbossche] [~westonpace] This is a very naive attempt on extracting types on a so-called class which extends the Generics. The following example reflects extracting types from such a class. {code:java} from typing import Any, List import inspect from typing import TypeVar, Generic T = TypeVar('T') class Array(object): def __init__(self, data: List[Any]): self._data = data @property def data(self): return self._data def __repr__(self): str1 = "" for datum in self.data: str1 += str(datum) + ", " return str1 class ArrayLike(Array, Generic[T]): def __init__(self, data:List[Any]): super(data) class DataType(object): def __init__(self): pass class Int32Type(DataType): def __init__(self): self._type_id = "int32" @property def id(self): return self._type_id # test a : Array = None data : List[int] = [10, 20 , 30] b: ArrayLike[Int32Type] = Array(data) print(b) # define a function with the generics def sample_udf(array: ArrayLike[Int32Type]) -> ArrayLike[Int32Type]: return arraysig = inspect.signature(sample_udf) input_types = sig.parameters.values() annotations = [val.annotation for val in input_types] annotation = annotations[0] inner_type = annotation.__args__[0] outer_type = annotation.__origin__ expr_arg = inner_type == Int32Type assert(expr_arg) expr_outer = outer_type == ArrayLike assert(expr_outer) {code} was (Author: vibhatha): [~apitrou] [~jorisvandenbossche] [~westonpace] This is a very naive attempt on extracting types on a so-called class which extends the Generics. The following example reflects extracting types from such a class. {code:java} from typing import Any, List import inspectfrom typing import TypeVar, Generic T = TypeVar('T') class Array(object): def __init__(self, data: List[Any]): self._data = data @property def data(self): return self._data def __repr__(self): str1 = "" for datum in self.data: str1 += str(datum) + ", " return str1 class ArrayLike(Array, Generic[T]): def __init__(self, data:List[Any]): super(data) class DataType(object): def __init__(self): pass class Int32Type(DataType): def __init__(self): self._type_id = "int32" @property def id(self): return self._type_id # test a : Array = None data : List[int] = [10, 20 , 30] b: ArrayLike[Int32Type] = Array(data) print(b) # define a function with the generics def sample_udf(array: ArrayLike[Int32Type]) -> ArrayLike[Int32Type]: return arraysig = inspect.signature(sample_udf) input_types = sig.parameters.values() annotations = [val.annotation for val in input_types] annotation = annotations[0] inner_type = annotation.__args__[0] outer_type = annotation.__origin__ expr_arg = inner_type == Int32Type assert(expr_arg) expr_outer = outer_type == ArrayLike assert(expr_outer) {code} > [Python] Extracting Type information from Python Objects > -------------------------------------------------------- > > Key: ARROW-15765 > URL: https://issues.apache.org/jira/browse/ARROW-15765 > Project: Apache Arrow > Issue Type: Improvement > Components: C++, Python > Reporter: Vibhatha Lakmal Abeykoon > Assignee: Vibhatha Lakmal Abeykoon > Priority: Major > > When creating user defined functions or similar exercises where we want to > extract the Arrow data types from the type hints, the existing Python API > have some limitations. > An example case is as follows; > {code:java} > def function(array1: pa.Int64Array, arrya2: pa.Int64Array) -> pa.Int64Array: > return pc.call_function("add", [array1, array2]) > {code} > We want to extract the fact that array1 is an `pa.Array` of `pa.Int32Type`. > At the moment there doesn't exist a straightforward manner to get this done. > So the idea is to expose this feature to Python. -- This message was sent by Atlassian Jira (v8.20.1#820001)