Type Stubs for subpackages don't work with basedpyright
I am not fully versed with this projects ins and outs, but I am encountering an issue with the type stubs when using basedpyright.
Specifically, the type checker seems to default to using the __init__.py files as opposed to the .pyi files when importing submodules.
I noted that there was a change in how type stubs were generated in 0.23.1:
Type stubs are precise (https://github.com/huggingface/tokenizers/pull/1928, https://github.com/huggingface/tokenizers/pull/1997) — methods that returned
Anynow return real types;mypy --strictmay surface previously-hidden errors. Stub layout also moved fromtokenizers/<sub>/__init__.pyitotokenizers/<sub>.pyi. This breaks the surface of some of the processors likeRobertaProcessign's__init__.
I think it's this stub layout that basedpyright is getting caught up on.
When I do something as simple as:
from tokenizers.models import BPE
It gets the type information from the generated __init__.py file, in this case:
# Generated content DO NOT EDIT
from .. import models
BPE = models.BPE
Model = models.Model
Unigram = models.Unigram
WordLevel = models.WordLevel
WordPiece = models.WordPiece
Rather than models.pyi. I was able to workaround this by copying models.pyi to my own type stubs renamed as __init__.pyi, but that isn't ideal.
Apologies, but I've not read much into PEP-561 and type stubbing in general so I don't know what the best solution to this is. Perhaps creating a stub __init__.pyi file?
I can reproduce this, and it isn't specific to basedpyright: pyright (so Pylance in VS Code) and mypy also see no types for any submodule in 0.23.1 and 0.23.2.
from tokenizers.models import BPE
from tokenizers.processors import RobertaProcessing
reveal_type(BPE)
reveal_type(BPE(vocab={"a": 0}, merges=[]).token_to_id)
reveal_type(RobertaProcessing)
| tokenizers | pyright 1.1.414 | mypy 2.4.0 |
|---|---|---|
0.22.1 (models/__init__.pyi) |
type[BPE] |
no py.typed in that version |
0.23.1, 0.23.2 (models.pyi) |
Unknown, Unknown, Unknown |
Any, Any, Any |
0.23.2 with <sub>.pyi moved to <sub>/__init__.pyi |
type[BPE], (token: str) -> (int | None), type[RobertaProcessing] |
full signatures |
The wheel ships both a models/ package and a sibling models.pyi. Type checkers resolve tokenizers.models to the package, like the interpreter does, and its generated __init__.py only re-exports names from the compiled extension (from .. import models), so models.pyi is never read. The same holds for decoders, normalizers, pre_tokenizers and processors. trainers is the exception that shows the fix: the 0.23.2 wheel also ships trainers/__init__.pyi, and from tokenizers.trainers import BpeTrainer resolves to type[BpeTrainer]. Moving the other five stubs to <sub>/__init__.pyi, as the reporter suggested, fixes them too.
On main this no longer applies, because 1.0 removes these submodules (1.0.0-rc.2 has no tokenizers.models). But transformers pins tokenizers>=0.23.1,<0.24.0, so 0.23.x is what most users have today. Would you take a fix for a 0.23.3? I'm happy to open a PR against a branch from v0.23.2 that writes the stubs to <sub>/__init__.pyi and adds a check that a type checker resolves tokenizers.models.BPE, if that's useful.
ty both for reporting!
Should be fixed now in v0.23.3 of the python package Feel free to re-open the issue if it's still not working for you, thanks