ENH: Add set of tests to type check the public APIs
See original GitHub issueFrom discussion on pandas typing meeting on January 7, 2022 (notes at https://docs.google.com/document/d/1tGbTiYORHiSPgVMXawiweGJlBw5dOkVJLY-licoBmBU/)
Follow up to discussion here: https://github.com/pandas-dev/pandas/issues/28142#issuecomment-991946409
Goal is to have a set of tests that make sure that users of the pandas API writing correct code work with whatever type stubs we publish.
Example of an approach that is suggested was developed within the pandas-stubs project based on various code snippets which are then tested with mypy
: https://github.com/VirtusLab/pandas-stubs/tree/master/tests/snippets
@zkrolikowski-vl and @joannasendorek from that project have offered to help (nudge, nudge)
Ideally we would add this testing framework to the CI before starting to bring over the stubs.
Issue Analytics
- State:
- Created 2 years ago
- Comments:47 (46 by maintainers)
I think we are not talking about using the
pandas-stubs
to test pandas source. I think the idea is that we make sure that changes topandas-stubs
work with both released versions of pandas, and in-development versions of pandas, and, conversely, that changes topandas
in development don’t break the stubs. The stubs are tested withmypy
,pyright
, but alsopytest
, so you could imagine a change inpandas
breaking apytest
test.I don’t think that you can have some stubs in a
pandas-stubs
package, and the rest in the source code. Not sure the type checkers will manage that.So the only issue here is that we will probably add tests for typing to test if a type is too wide, and put a
#type: ignore
comment on them, but the code will then execute with pytest, and raise an exception, which means if pytest is run on it, we have to expect that exception. For example:So when we type
Series.corr()
to havemethod
be aUnion[Literal["pearson", "spearman", "kendall"], Callable]
, then you want the typing test to test thatfoobar
is invalid formethod
. To do that you’d do something likeso that the type checker was happy, but then
pytest
will fail until you surround the code withpytest.raises
types of things.Maybe that means we should have a
pandas/tests/typing/valid
section - which contains valid code that is tested withpytest
and the type checker, and apandas/tests/typing/invalid
section where we don’t runpytest
. Then in the files in theinvalid
directory, have something in there that just skips the tests.