feature-engine / feature_engine

Feature engineering package with sklearn like functionality
https://feature-engine.trainindata.com/
BSD 3-Clause "New" or "Revised" License
1.8k stars 304 forks source link

Add py.typed file #710

Closed david-cortes closed 7 months ago

david-cortes commented 8 months ago

This PR adds a py.typed file for the package, which signals to mypy that the library contains types that can be checked during checks/tests in user code.

This is useful for example to use classes from feature_engine in type hints and check that the types match when executing that code.

codecov[bot] commented 8 months ago

Codecov Report

Merging #710 (f2d1147) into main (fa17c33) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #710   +/-   ##
=======================================
  Coverage   98.03%   98.03%           
=======================================
  Files         100      100           
  Lines        3877     3877           
  Branches      761      761           
=======================================
  Hits         3801     3801           
  Misses         28       28           
  Partials       48       48           

:mega: Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

solegalli commented 7 months ago

Hey @david-cortes

Thanks for the PR. I didn't know about this functionality. Is this for users of Feature-engine? or for feature-engine developers? Do you have by any chance a code snippet where this would be useful? or a link?

Thanks!

david-cortes commented 7 months ago

Hey @david-cortes

Thanks for the PR. I didn't know about this functionality. Is this for users of Feature-engine? or for feature-engine developers? Do you have by any chance a code snippet where this would be useful? or a link?

Thanks!

This is for users of the package who wish to run mypy on code that uses it.

For example, suppose you create a file test_transformers.py like this:

from feature_engine.preprocessing import MatchVariables

def get_transformer() -> MatchVariables:
    return MatchVariables()

def test_get_transformer():
    tr = get_transformer()
    assert True

Then running it with pytest test_transformers.py will succeed as expected.

But running it with pytest --mypy test_transformers.py will error out:

======================================== FAILURES =========================================
__________________________________ test_transformers.py ___________________________________
1: error: Skipping analyzing "feature_engine.preprocessing": module is installed, but missing library stubs or py.typed marker  [import-untyped]
1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
______________________________________ test session _______________________________________
mypy exited with status 1.
========================================== mypy ===========================================
Found 1 error in 1 file (checked 1 source file)
================================= short test summary info =================================
FAILED test_transformers.py::mypy
FAILED test_transformers.py::mypy-status
=============================== 2 failed, 1 passed in 1.14s ===============================

Adding this file allows checking the type hints that involve classes from this package.

solegalli commented 7 months ago

Super thank you! That's very helpful :)