ENH: Add typing tests according to the issue 45252
State: closed

Typing test stubs originally authored by Joanna Sendorek, Zbigniew Królikowski and pandas_stubs contributors: https://github.com/VirtusLab/pandas-stubs/graphs/contributors

Adapted to pandas needs by Torsten Wörtwein

Issue reference:
https://github.com/pandas-dev/pandas/issues/45252

**Summary of files and changes:**

- Included a number of _test_ files under `pandas/tests/typing/valid` that contain functions testing different areas of pandas API. There are no assertions there - their only aim to check argument and declarations that should be allowed according to the documentation and popular use-cases.
- Included a dedicated `README` under `pandas/tests/typing/REAMDE.md` that describes the purpose of origin of those files.
- Included a file called `STUBS_LICENSE` under `LICENSES`. It contain the Copyright and license (MIT) of the original project.
- Modified `pyproject.toml` to enable mypy and pyright on the directory.

**Related and out-of-scope aspects**

- **How to handle negative tests**: That's out-of-scope. Simplest way would be to place those tests in a directory not checked by pytest and use `# type: ignore[<reason>]`.
- **How to use these new typing test when we have more (public) pyi files https://github.com/pandas-dev/pandas/pull/45561#issuecomment-1020280857**: Mypy or pyright should be able to automatically pick-up the .pyi files when they'll be present.
- **How to maintain the new type tests (ideally could de-duplicate work between pandas and VirtusLab)**: As for keeping up-to-date on our side I've created two issues https://github.com/VirtusLab/pandas-stubs/issues/146 https://github.com/VirtusLab/pandas-stubs/issues/147 to tackle that. At the very least we can make it easier to move the code. At best we can partially automate it. In terms of de-duplicating work - all work in pandas_stubs except for small fixes is written out in issues and it will most likely be the same for pandas. We'll need to count on contributor diligence.

- [x] closes #45252
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit) for how to run them
- [ ] whatsnew entry - (_doesn't have a functional impact_)


Labels:
name: Typing
description: type annotations, mypy/pyright type checking
name: Stale

Comments:
Except for running isort you will need to skip a few tests depending on required dependencies to make the CI greener :) (or wait first for consensus before going forward)

`df.plot.hist()`  in  pandas/tests/typing/valid/test_frame.py:534 requires matplotlib
`df4: pd.DataFrame = pd.DataFrame({"a": [1], "b": [1]}).style.pipe(foo)` in pandas/tests/typing/valid/test_frame.py:733 seems to require jinja2

The easiest is probably to skip the entire test function with the existing decorator:

```py
from pandas.util import _test_decorators as td

@td.skip_if_no("matplotlib")
def test_bar():
    ...
```

Probably wouldn't hurt to also have a small readme in the typing/valid folder to mention where these files came from and state the main contributors. Probably would also need someone with expertise with licenses (Pandas is BSD-3 but the stubs are MIT).
@twoertwein It's impossible to make both `isort` and `black` pass at the same time. They expect different order of imports. I was forced to push without the pre-commit hooks. Hopefully it works correctly on the CI.

EDIT: No it doesn't. I'll continue working on that. There might be a workaround.

I've added a small README but haven't mentioned the contributors - pandas repository seems devoid of such and I've deemed to continue with this approach.

@twoertwein Only available option was to configure isort to black. This might be a bit controversial.
> @twoertwein Only available option was to configure isort to black. This might be a bit controversial.

I think isort is already configured to be compatible with black:
https://github.com/pandas-dev/pandas/blob/42a26ac539ac9a194f6e7da0897ffc938b7d7e76/pyproject.toml#L143
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
@Dr-Irv we should close this now and open these changes against https://github.com/pandas-dev/pandas-stubs in the first instance?
> @Dr-Irv we should close this now and open these changes against https://github.com/pandas-dev/pandas-stubs in the first instance?

We can close it.  The current `pandas-stubs` has an evolved version of the tests that are in this PR.  No need to copy anything over.