unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library
https://www.union.ai/pandera
MIT License
3.27k stars 305 forks source link

Explicitly reexport in base init #718

Closed rchui closed 2 years ago

rchui commented 2 years ago

Describe the bug When mypy is run with the setting (implicit_reexport = False), mypy will fail when pandera is used as the documentation recommends.

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# main.py
import pandera as pa

class Schema(pa.SchemaModel):  # <-- error
  ...

And can be type checked with

mypy --strict main.py

main.py:4: error: Name 'pa.SchemaModel' is not defined
main.py:4: error: Class cannot subclass 'SchemaModel' (has type 'Any')
Found 2 errors in 1 file (checked 1 source file)

Expected behavior

The pandera base init file (pandera/__init__.py) uses implicit re-exports which means that instead we must import pandera classes instead like:

from pandera.model import SchemaModel
from pandera.model_components import Field

If the expectation is that if pandera is imported like import pandera as pa, then top level classes and function should be explicitly re-exported.

This would have no impact on users who don't use mypy and those who do allow implicit re-exports. But for those who want stricter type safety, it would allow the library to be used as intended.

Desktop (please complete the following information):

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

cosmicBboy commented 2 years ago

Hi @rchui thanks for filing this issue! This should have been addressed in https://github.com/pandera-dev/pandera/pull/686.

I reproduced the issue on 0.8.0 tag, and it isn't a problem on master.

I'll be releasing a bugfix release by next week (a pre-2022 holiday 🎁 !)

Also as a head up, 0.8.1 will be making the mypy static-linting support optional with a pip install pandera[mypy] extra and mypy plugin. It turns out the pandas-stubs dependency raises a fair number of false positives so we decided to make mypy support experimental.

As a heads up here's the updated docs: https://pandera--701.org.readthedocs.build/en/701/mypy_integration.html#mypy-integration.

In the mean time you can try out master and see if it addresses the issue.

rchui commented 2 years ago

Thank you. I'm looking forward to this. Closing this issue.