Skip to content

Annotate the pmcid arrays as ndarray, not Sequence[int] - #62

Merged
adelavega merged 1 commit into
mainfrom
fix/vectorization-return-type
Sep 25, 2026
Merged

adelavega merged 1 commit into
mainfrom
fix/vectorization-return-type

Conversation

@adelavega

Copy link
Copy Markdown
Collaborator

make test_mypy currently fails on main:

src/pubget/_vectorization.py:200: error: Incompatible return value type
  (got "tuple[ndarray[tuple[Any, ...], dtype[Any]], dict[str, Any], Any]",
   expected "tuple[Sequence[int], dict[str, Any], Any]")  [return-value]

_extract_word_counts returns np.concatenate(...), an ndarray, while its
signature promises Sequence[int]. Older numpy stubs allowed an ndarray to
satisfy Sequence[int]; from numpy 2.1 the shape-parameterised
ndarray[tuple[Any, ...], dtype[Any]] no longer does. That is why the failure
appeared without any change to this file — the py313-latest-deps leg of the
matrix now resolves a numpy new enough to reject it, while py310-min-deps
still passes.

_vectorize_articles has the same mistake one level down: it returns
articles["pmcid"].values under the same Sequence[int] annotation. mypy cannot
currently see it, because DataFrame.values is Any without pandas stubs, but it
would surface as soon as those are installed. Both are fixed here.

Annotations only — no behaviour change.

Verification

Reproduced the CI failure locally in a clean venv with numpy 2.5.3 and mypy 2.3.1,
then confirmed the fix under both extremes:

numpy before after
2.5.3 the error above Success: no issues found
1.25.2 clean clean

The module still imports and the annotation resolves to a real numpy.ndarray
type at runtime.

🤖 Generated with Claude Code

`make test_mypy` fails on main under numpy >= 2.1:

  _vectorization.py:200: error: Incompatible return value type
    (got "tuple[ndarray[tuple[Any, ...], dtype[Any]], dict[str, Any], Any]",
     expected "tuple[Sequence[int], dict[str, Any], Any]")

`_extract_word_counts` returns `np.concatenate(...)`, which is an ndarray, while
the signature promises `Sequence[int]`. Older numpy stubs let an ndarray satisfy
Sequence[int]; from 2.1 the shape-parameterised `ndarray[tuple[Any, ...],
dtype[Any]]` no longer does, so the CI matrix's py313-latest-deps job started
failing while py310-min-deps still passes.

`_vectorize_articles` has the same mistake one level down -- it returns
`articles["pmcid"].values` under the same `Sequence[int]` annotation. mypy cannot
see it because DataFrame.values is Any without pandas stubs, so it would surface
the moment those are added. Fixed both.

Annotations only; no behaviour change.

Verified under both extremes: with numpy 2.5.3, mypy goes from the error above to
"Success: no issues found"; with numpy 1.25.2 the file is clean before and after.
The module still imports, and the annotation resolves to a real ndarray type.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@adelavega
adelavega merged commit 1127666 into main Sep 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant