fix(inspect): emit parametrized hints for bare container types - #6237
fix(inspect): emit parametrized hints for bare container types#6237arimu1 wants to merge 3 commits into
Conversation
Add STANDALONE_TYPE_HINT for PyList, PyDict, and PyTuple so Bound/Py forms emit list[Any], dict[Any, Any], and tuple[Any, ...] in generated stubs while keeping TYPE_HINT unparametrized for composition. Fixes PyO3#6236
- Rename newsfragments/6236.fixed.md → 6237.fixed.md (PR number) - Rename #type_hint= → #standalone_type_hint= in pyobject macros - Fix dict.rs import order; assert Bound/Py INPUT/OUTPUT_TYPE in tests
The introspection change makes bare `dict`, `list` and `tuple` emit parametrized hints, so the checked-in stubs no longer matched what `nox -s test-introspection` generates.
|
Thank you for this MR. I would tend to prefer we keep the bare container types in the stubs: this mypy lint is to nudge people to actually set a parameter to the collection. In PyO3, it likely implies adding a manual type hint in the signature e.g. |
|
Thanks @arimu1 for working so quickly on this and thanks @Tpt for already looking into it! As I created #6236 which this PR is about, let me give you some motivation of why I think it would be beneficial to have this behavior. Ideally, I would want PyO3 to always emit typestubs that are fully You described a workaround where one could manually set a If I have an interface that strictly expect a list of integers, instead of receiving a In my specific example that occurs a handful of times across the project, I actually want to consume a bare I may come from a bit of an opinionated perspective, but ideally I would want PyO3 to always emit typestubs that are |
|
Btw., it seems that a bunch of stubs in the |
|
@jonasdedden good catch — I reproduced your run exactly, and it turns out it's a useful measurement of this PR. Same
So this PR clears 14 of the 16
Both look tractable and I'm happy to do either as a follow-up, but I'd rather not widen this PR while the underlying question is still open. @Tpt on that question — the argument that persuades me isn't "silence the lint", it's consistency with what PyO3 already does. # pytests/stubs/pyfunctions.pyi on main
def many_keyword_arguments(*, ant: Any | None = None, bear: Any | None = None, ...)Nobody expects that to nudge the user into hand-writing a signature — it's just the honest rendering of a parameter whose Rust type carries no more information. Put differently: That said, it's your call and I'm not going to argue it past this. If you'd still prefer bare containers, I'm happy to close this and instead document the |
Summary
Fixes #6236.
experimental-inspectpreviously emitted barelist,dict, andtuplefor standaloneBound<'_, PyList>/PyDict/PyTuple(andPy<...>equivalents), which failsmypy --strictunderdisallow_any_generics.This adds
STANDALONE_TYPE_HINTonPyTypeInfo/PyTypeCheck, used for directBound/Pycontainer types, while keepingTYPE_HINTunparametrized for composition (e.g.Vec<T>still renders aslist[T]).PyListlist[Any]PyDictdict[Any, Any]PyTupletuple[Any, ...]Test plan
PYO3_PYTHON=python3.12 cargo test --features experimental-inspect --lib container_standalone_type_hintsPYO3_PYTHON=python3.12 cargo test --features experimental-inspect --lib container_type_hintsPYO3_PYTHON=python3.12 cargo build --features experimental-inspect