Repository navigation
Conversation
|
|
|
Thanks for continuing to push this forward Rok! I think this PR is a great next step and is quite clear to review. I have checked out the PR locally and started with running type checkers ( The reason for these warnings and errors in I propose to push the changes directly if we agree the additions makes sense. Meanwhile I will continue with running the |
|
I have run I have also run mypy's stubtest with Besides the reports mentioned above which will be fixed in subsequent PRs, stubtest did found one bug in pyarrow: Note missing "L" in "Decimal". I will work on the fix for this now and would suggest to add it as part of this PR. Can commit directly, if that is ok. |
|
I've run Running on editable dev build 27, with pyright==1.1.414 in my conda env, I also hit errors |
That makes sense and please go for it. I think we should aim for
Huh. I've never noticed these were missing. Given we're not really using them as types I'd skip them if we can.
Huh. Let's do the fix as separate PR, but also include a fix here? (And we'll just rebase over the actual fix on main)
I'm not sure we want (can) bring in ruff as a CI dependency. However we should autoformat before we merge, so we don't do it later and change history for no good reason. So I propose one of you runs the ruff formatter and commits the change into this branch. |
I think
Sounds good. Will open an issue and a PR and add a fix here also. |
Yeah, they are internal and exposed through |
Ok, I'll do that reformatting in a bit. |
|
I have started with reviewing pyarrow tests with running click for output$ mypy --check-untyped-defs --warn-unused-ignores pyarrow/tests/test_array.py
pyarrow/tests/test_array.py:21: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:22: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:25: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:80: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:790: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:986: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:1300: error: List item 3 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:1302: error: List item 1 has incompatible type "None"; expected "list[tuple[bytes, int]]" [list-item]
pyarrow/tests/test_array.py:1313: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:1320: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:1324: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:1348: error: List item 0 has incompatible type "None"; expected "list[tuple[bytes, int]]" [list-item]
pyarrow/tests/test_array.py:1348: error: List item 1 has incompatible type "None"; expected "list[tuple[bytes, int]]" [list-item]
pyarrow/tests/test_array.py:1362: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:2513: error: Need type annotation for "buffers" (hint: "buffers: list[<type>] = ...") [var-annotated]
pyarrow/tests/test_array.py:2539: error: "object" has no attribute "dtype" [attr-defined]
pyarrow/tests/test_array.py:2541: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:2542: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:2543: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:2885: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:2917: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:2938: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:2939: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:3183: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:3299: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:3720: error: Incompatible types in assignment (expression has type "None", variable has type "ndarray[tuple[Any, ...], dtype[Any]]") [assignment]
pyarrow/tests/test_array.py:3729: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:3740: error: Value of type "object" is not indexable [index]
pyarrow/tests/test_array.py:3817: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:4004: error: Incompatible types in assignment (expression has type "MyArrayInvalid", variable has type "MyArray") [assignment]
pyarrow/tests/test_array.py:4017: error: Incompatible types in assignment (expression has type "MyArray2", variable has type "MyArray") [assignment]
pyarrow/tests/test_array.py:4031: error: Incompatible types in assignment (expression has type "MyArray3", variable has type "MyArray") [assignment]
pyarrow/tests/test_array.py:4095: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:4099: error: Unused "type: ignore" comment [unused-ignore]
pyarrow/tests/test_array.py:4315: error: List item 3 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:4319: error: List item 5 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:4324: error: List item 2 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:4324: error: List item 4 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:4395: error: List item 1 has incompatible type "None"; expected "int" [list-item]
pyarrow/tests/test_array.py:4407: error: List item 0 has incompatible type "None"; expected "int" [list-item]
Found 40 errors in 1 file (checked 1 source file)Type checkers are currently configured to skip I propose to temporarily add the |
|
Another thing: I can't run |
|
Oh, I was too fast here. Unfortunately Would it make sense to remove I will leave this for discussion and start with reading the stubs and comparing them with the APIs, starting with |
|
Ignore statements not being needed is more likely due to changes to typecheckers than due to changes in stubs. Or they might be needed once we import other parts of the library. Also note camel cased (e.g. reportArgumentType) ignores are likely coming from pyright. So if mypy thinks it's a redundant ignore you might want to check pyright too. |
Let's rather remove |
|
OK, agree, when reviewing I plan to do that later and am focusing on reading the stubs currently. |
|
Ehhh... I've fallen into this interesting rabbit hole - There are over 1.5k mypy errors % mypy --check-untyped-defs --warn-unused-ignores pyarrow/tests/test_convert_builtin.py
pyarrow/tests/test_convert_builtin.py:78: error: Module not callable [operator]
pyarrow/tests/test_convert_builtin.py:79: error: Module not callable [operator]
...
pyarrow/tests/test_convert_builtin.py:168: error: Module not callable [operator]
...
Found 321 errors in 1 file (checked 1 source file)
% mypy --check-untyped-defs --warn-unused-ignores pyarrow/tests/test_*.py
Found 1866 errors in 35 files (checked 44 source files)
% ... | grep -c "Module not callable"
1562test_convert_builtin.py:168 : pa.array(... A stub So I propose we reintroduce subfolder |
|
Just to note: with commit eda3425 stubs |
|
@tadeja I suppose the |
|
@jorisvandenbossche oh, yes, I think |
|
@tadeja @rok I have checked typed.pyi and decided to push a commit (4b97c69) after all =) Instead of suggestions I will post the reasoning behind the changes here:
Future TODO:
I have run
|
Co-authored-by: Joris Van den Bossche <[email protected]>
| class ListArray(BaseListArray): | ||
| @staticmethod | ||
| def from_arrays( | ||
| offsets: Int32Array | Iterable[int | None] | np.ndarray, |
There was a problem hiding this comment.
I think here the same applies as the BooleanArray -> Array discussion we had for the mask argument. IMO we have to annotate this with a generic Array, to avoid people to have to cast their pyarrow array objects (since those will often not be inferred to the exact sub-type)
(and the same for all the Int32Array/Int64Array usage below)
There was a problem hiding this comment.
(and I don't know what the good solution is long term. Ideally you would be able to express something like "this argument can be Array, but if you know its subtype, it has to be Int64Array and not something else", but no idea if that is possible)
There was a problem hiding this comment.
Actually, I also quickly checked the code for the exact runtime behaviour, and eg in ListArray.from_arrays, we do _offsets = asarray(offsets, type='int32') on the input argument for offsets. So strictly speaking, even Int32Array is not correct if we had perfect inference of Array subtypes. Because it is perfectly fine to pass an int64 array, and it will be cast to int32 at runtime.
There was a problem hiding this comment.
In this case we could just make a wide union then. Pyarrow-stubs uses overloads https://github.com/zen-xu/pyarrow-stubs/blob/d92217fe71ff56e86c0125175b488b0d486a3a3e/pyarrow-stubs/__lib_pxi/array.pyi#L2433-L2512. Could we express this with generics?
There was a problem hiding this comment.
The overloads are for the values (to try to infer the values type of the ListArray), not for offsets, I think?
And with a wide union, do you mean include both like Array | Int32Array | ..? (my naive assumption about typing logic is that this would be useless compared to just Array)
There was a problem hiding this comment.
The overloads are for the
values(to try to infer the values type of the ListArray), not foroffsets, I think?
Ah yes, indeed.
And with a wide union, do you mean include both like
Array | Int32Array | ..? (my naive assumption about typing logic is that this would be useless compared to justArray)
By wide union I mean as wide a type that we can have that's not Array. pyarrow-stubs uses Int32Array | list[int] which seems too narrow.
There was a problem hiding this comment.
Ah, that meaning of wide ;) Understood now.
Indeed both parts in Int32Array | list[int] is too narrow, I think this should be something like Array | Iterable[int] (and even the int in Iterable might give problems if you pass eg a numpy array? We should add tests for those)
There was a problem hiding this comment.
Pushed bc94a18, please check if it makes sense.
There was a problem hiding this comment.
Plus I did additional 66d8661 to narrow the list from_arrays masks from Mask to Array | None as runtime accepts only boolean Array (and not ndarray, also not ChunkedArray)
Add type annotation stubs for compute, _compute, _compute_docstring, and _acero modules. These are the stubs of apacheGH-49201 as they were, at their original paths. The type checker accommodations that apacheGH-49201 made in compute.py, acero.py and the PyArrow tests are left out, as the type checkers no longer run on pyarrow/ (see apacheGH-55264).
Move the stubs to the flat pyarrow-stubs/ layout and drop the generics: plain classes and unions instead of TypeVars and Array[Scalar[...]]. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types still name the kind of array where the function determines it. The scalar classes are imported from pyarrow.__lib.scalar, as placeholders (Any) until scalar.pyi is annotated. - Fix the signatures that stubtest reports against the runtime, e.g. the arguments without defaults, parameter names and positional-only dunders. Remove CumulativeSumOptions, Function.kernels and the reflected Expression operators, which don't exist or raise TypeError at runtime, and the stub-only Expression methods for the tests. - Rename _compute_docstring.pyi to _compute_docstrings.pyi to match the runtime module, and remove _perform_join and _filter_table from _acero.pyi, which are defined in acero.py. - Add the functions and options added since: hypot, modulo, remainder, inverse_permutation, scatter, search_sorted and their options, and per-key null placement in sort keys. - Type check pyarrow.compute and pyarrow._compute instead of treating them as Any, and add type annotation tests for them.
Add type annotation stubs for compute, _compute, _compute_docstring, and _acero modules. These are the stubs of apacheGH-49201 as they were, at their original paths. The type checker accommodations that apacheGH-49201 made in compute.py, acero.py and the PyArrow tests are left out, as the type checkers no longer run on pyarrow/ (see apacheGH-55264).
Move the stubs to the flat pyarrow-stubs/ layout and drop the generics: plain classes and unions instead of TypeVars and Array[Scalar[...]]. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types still name the kind of array where the function determines it. The scalar classes are imported from pyarrow.__lib.scalar, as placeholders (Any) until scalar.pyi is annotated. - Fix the signatures that stubtest reports against the runtime, e.g. the arguments without defaults, parameter names and positional-only dunders. Remove CumulativeSumOptions, Function.kernels and the reflected Expression operators, which don't exist or raise TypeError at runtime, and the stub-only Expression methods for the tests. - Rename _compute_docstring.pyi to _compute_docstrings.pyi to match the runtime module, and remove _perform_join and _filter_table from _acero.pyi, which are defined in acero.py. - Add the functions and options added since: hypot, modulo, remainder, inverse_permutation, scatter, search_sorted and their options, and per-key null placement in sort keys. - Type check pyarrow.compute and pyarrow._compute instead of treating them as Any, and add type annotation tests for them.
…al types Squash of apache#55264 (5a026a0) on top of main, to check the stacked compute stubs against it. Not for merging.
Add type annotation stubs for compute, _compute, _compute_docstring, and _acero modules. These are the stubs of apacheGH-49201 as they were, at their original paths. The type checker accommodations that apacheGH-49201 made in compute.py, acero.py and the PyArrow tests are left out, as the type checkers no longer run on pyarrow/ (see apacheGH-55264).
Move the stubs to the flat pyarrow-stubs/ layout and drop the generics: plain classes and unions instead of TypeVars and Array[Scalar[...]]. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types still name the kind of array where the function determines it. The scalar classes are imported from pyarrow.__lib.scalar, as placeholders (Any) until scalar.pyi is annotated. - Fix the signatures that stubtest reports against the runtime, e.g. the arguments without defaults, parameter names and positional-only dunders. Remove CumulativeSumOptions, Function.kernels and the reflected Expression operators, which don't exist or raise TypeError at runtime, and the stub-only Expression methods for the tests. - Rename _compute_docstring.pyi to _compute_docstrings.pyi to match the runtime module, and remove _perform_join and _filter_table from _acero.pyi, which are defined in acero.py. - Add the functions and options added since: hypot, modulo, remainder, inverse_permutation, scatter, search_sorted and their options, and per-key null placement in sort keys. - Type check pyarrow.compute and pyarrow._compute instead of treating them as Any, and add type annotation tests for them.
pa.array() is annotated as returning the base Array, so parameters typed as Int32Array, Int8Array or BooleanArray forced users to cast. Annotate them as Array, and widen run ends to what the runtime accepts. DictionaryArray.from_arrays never accepts an Arrow array as mask, so drop BooleanArray there.
Annotate pyarrow.compute, pyarrow._compute, pyarrow._acero and pyarrow._compute_docstrings, following the approach of apacheGH-55264: plain classes and unions instead of generics, in the flat pyarrow-stubs/ layout. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types name the kind of array where the function determines it. The scalar classes are placeholders (Any) until scalar.pyi is annotated. - The signatures match the runtime, as checked by stubtest. - pyarrow.compute and pyarrow._compute are type checked instead of being treated as Any. - Add type annotation tests for the compute and Acero stubs.
…al types Squash of apache#55264 (bc94a18) on top of main. The compute stubs in the next commit build on it; this commit goes away once that pull request is merged.
Annotate pyarrow.compute, pyarrow._compute, pyarrow._acero and pyarrow._compute_docstrings, following the approach of apacheGH-55264: plain classes and unions instead of generics, in the flat pyarrow-stubs/ layout. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types name the kind of array where the function determines it. The scalar classes are placeholders (Any) until scalar.pyi is annotated. - The signatures match the runtime, as checked by stubtest. - pyarrow.compute and pyarrow._compute are type checked instead of being treated as Any. - Add type annotation tests for the compute and Acero stubs.
Annotate pyarrow.compute, pyarrow._compute, pyarrow._acero and pyarrow._compute_docstrings, following the approach of apacheGH-55264: plain classes and unions instead of generics, in the flat pyarrow-stubs/ layout. - Array arguments accept any Array or ChunkedArray, since pa.array() and most of the API return plain Array instances. The return types name the kind of array where the function determines it. The scalar classes are placeholders (Any) until scalar.pyi is annotated. - The signatures match the runtime, as checked by stubtest. - pyarrow.compute and pyarrow._compute are type checked instead of being treated as Any. - Add type annotation tests for the compute and Acero stubs.
Rationale for this change
This is the next step in adding type annotations to PyArrow (#32609). It merges #49103 (internal type system stubs) and #49193 (core data structures) because the two depend on each other and can't be type checked separately.
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?
Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by: