fix(typing): Resolve all mypy & pyright errors for _arrow#2007
fix(typing): Resolve all mypy & pyright errors for _arrow#2007dangotbanned merged 63 commits intomainfrom
mypy & pyright errors for _arrow#2007Conversation
Will close #1961 Still have ~50 errors for `mypy` to review
|
@MarcoGorelli Almost out of the rabbit hole on this! I've found some more places where (#1657 (comment)) would be pretty helpful: narwhals/narwhals/_arrow/series.py Lines 126 to 160 in 1d24dec narwhals/narwhals/_arrow/series.py Lines 549 to 552 in 1d24dec Essentially anywhere that So for My brain has fully melted working on this, hope the above made sense 🫠 |
|
thanks for working on this is it necessary to make |
@MarcoGorelli 100% needed to resolve the issues I'm afraid 😔 Without the Having read through a lot of the code (but not using It seems to be available in our min version (https://arrow.apache.org/docs/11.0/python/generated/pyarrow.dataset.Expression.html) For some context of the kinds of errors, (#1961 (comment)) |
|
Started at like 150-300 errors Down to 32 errors 😁!!!!!!!!!!! Update 1Now 23 errors (https://github.com/narwhals-dev/narwhals/actions/runs/13315369207/job/37187931923?pr=2007) Update 2Update 3Now with 17 errors (https://github.com/narwhals-dev/narwhals/actions/runs/13317234868/job/37194138357?pr=2007) Update 4Down to 10 errors (https://github.com/narwhals-dev/narwhals/actions/runs/13328559038/job/37227189197?pr=2007) All remaining errors are in Update 5Only 1 error left (https://github.com/narwhals-dev/narwhals/actions/runs/13328999125/job/37228573119?pr=2007) Update 6
|
…) [attr-defined] `__iter__` doesn't seem to be defined in the docs or stubs https://arrow.apache.org/docs/python/generated/pyarrow.Table.html
- The stubs are overly strict for `Table.from_arrays` - It can accept `Sequence[Array[T] | ChunkedArray[T]]`
…s" cannot be "object" [type-var] - Had 2 of these - Used a different path that preserved the generic type
| Incomplete: TypeAlias = Any # pragma: no cover | ||
| """ | ||
| Marker for working code that fails on the stubs. | ||
|
|
||
| Common issues: | ||
| - Annotated for `Array`, but not `ChunkedArray` | ||
| - Relies on typing information that the stubs don't provide statically | ||
| - Missing attributes | ||
| - Incorrect return types | ||
| - Inconsistent use of generic/concrete types | ||
| - `_clone_signature` used on signatures that are not identical | ||
| """ |
There was a problem hiding this comment.
Note
See for more context https://github.com/python/typeshed/blob/a410f251ee535950eb4440ad41ae4c00c87e6a67/stdlib/_typeshed/__init__.pyi#L45-L50
I've been sprinkling these in with a comment when all else fails, e.g:
narwhals/narwhals/_arrow/series.py
Lines 486 to 490 in 1b537e7
If the stub issues get resolved in the future, this will be a lot easier to fix than just using Any directly
Want to start a thread, but havent changed the code here yet
`pyright` doesn't need this, `mypy` infers this as `str` - which is too wide > narwhals/_arrow/namespace.py:372: error: No overload variant of "binary_join_element_wise" matches argument types "Generator[ChunkedArray[StringScalar], None, None]", "str" [call-overload]> narwhals/_arrow/namespace.py:372: note: Possible overload variants:
narwhals/_arrow/utils.py
Outdated
| backend_version: tuple[int, ...], | ||
| ) -> Any: | ||
| length: int, other: ArrowSeries, backend_version: tuple[int, ...] | ||
| ) -> pa.BooleanArray: |
There was a problem hiding this comment.
🤔 this doesn't have to be boolean, right? plenty of binary operations don't return booleans?
…ev/narwhals into typing-major-fixing-1
…ev/narwhals into typing-major-fixing-1
…als into typing-major-fixing-1
|
@MarcoGorelli I'm gonna leave you to it for now to avoid conflicts, but could you give me a shout when everything is ready please? I'm getting warnings from |
|
sure, I think those pyright issues should be addressed now (and it would be really good to get pyright running in CI too) downstream test failures should all be unrelated to this PR |
MarcoGorelli
left a comment
There was a problem hiding this comment.
happy to get this in anyway, thanks @dangotbanned , really appreciate it!
Thanks @MarcoGorelli, both Editdouble cat was unintentional but I like it |
@MarcoGorelli IIRC |
| def total_milliseconds(self: Self) -> ArrowSeries: | ||
| ser: ArrowSeries = self._compliant_series | ||
| arr = ser._native_series | ||
| unit = ser._type.unit | ||
| unit = ser._type.unit # type: ignore[attr-defined] | ||
| unit_to_milli_factor = { | ||
| "s": 1e3, # seconds | ||
| "ms": 1, # milli |
There was a problem hiding this comment.
A bit dissapointed this change reintroduced the need for the #type: ignores that were fixed
Note
Just an observation
There was a problem hiding this comment.
For future reference, this commit has a solution using the "public" API 44560d1
| def is_pyarrow_chunked_array( | ||
| ser: Any | ArrowChunkedArray, | ||
| ) -> TypeIs[ArrowChunkedArray]: |
There was a problem hiding this comment.
Could you revert this to keeping the TypeVar?
I don't see a need to erase it, if it can be known here - especially since this is public
|
Going to go ahead and merge this as I think it's a clear improvement, and we can address some smaller items like #2007 (comment) and #2007 (comment) as follow-ups Thanks again Dan! |
Pretty sure `pyarrow-stubs` is going to need an upstream fix for the imports #2007 (comment)
Discovered during narwhals-dev/narwhals#2007 I worked around this with this, but seemed simple enough to upstream: ```py def _type(self: pa.ChunkedArray[pa.Scalar[_DataType_CoT]]) -> _DataType_CoT: if TYPE_CHECKING: return self[0].type return self.type ```
I noted this in #2007 (comment) Seems `preview` detects this, as I was expecting when I disabled the `pyright` diagnostic



What type of PR is this? (check all applicable)
Related issues
mypypassing withpyarrow-stubsinstalled #1961Checklist
If you have comments or can explain your changes, please do so below
Planning to finish getting the, thenmypyerrors to 0tidying things up