Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds explanatory error message to check decorators and tests #86

Merged
merged 3 commits into from
Aug 15, 2019

Conversation

mastersplinter
Copy link
Collaborator

This PR:

  • adds a detailed explanation when the user specifies an invalid index to a check decorator
  • this is particularly useful in a long/complex pipeline undergoing refactoring

In this example:

import pandas as pd
from pandera import Column, Float, DataFrameSchema, check_input, check_output

df_schema = DataFrameSchema(
        {"a":        Column(Float, nullable=True),
         "b":        Column(Float, nullable=True),
         })

df1 = pd.DataFrame({'a':[1.,2.], 'b':[1.,2.]})
df2 = pd.DataFrame({'a':[1.,2.], 'b':[1.,2.]})

@check_input(df_schema, 2)
@check_output(df_schema)
def testing_func(a,b):
    out = a.append(b)
    return out

testing_func(df1,df2)

before PR:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-ddac0478d816> in <module>
     16     return out
     17 
---> 18 testing_func(df1,df2)

/pandera_dev/pandera/pandera.py in _wrapper(fn, instance, args, kwargs)
   1004         args = list(args)
   1005         if isinstance(obj_getter, int):
-> 1006             args[obj_getter] = schema.validate(args[obj_getter])
   1007         elif isinstance(obj_getter, str):
   1008             if obj_getter in kwargs:

IndexError: list index out of range

after the PR the error is:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/pandera_dev/pandera/pandera.py in _wrapper(fn, instance, args, kwargs)
   1006             try:
-> 1007                 args[obj_getter] = schema.validate(args[obj_getter])
   1008             except IndexError as e:

IndexError: list index out of range

During handling of the above exception, another exception occurred:

SchemaError                               Traceback (most recent call last)
<ipython-input-2-ddac0478d816> in <module>
     16     return out
     17 
---> 18 testing_func(df1,df2)

/pandera_dev/pandera/pandera.py in _wrapper(fn, instance, args, kwargs)
   1016                          len(_get_fn_argnames(fn)),
   1017                          max(0, len(_get_fn_argnames(fn))-1),
-> 1018                          e
   1019                          )
   1020                         )

SchemaError: error in check decorator of function 'testing_func': the index '2' was supplied to the check but this function accepts '2' arguments, so the maximum index is '1'. The full error is: 'list index out of range'

@mastersplinter mastersplinter added the enhancement New feature or request label Aug 14, 2019
@mastersplinter
Copy link
Collaborator Author

@cosmicBboy this is now ready for re-review, apologies for the new PR, I squashed the history to ensure it was tidy for the merge and git won't open a PR when the branch has been force-pushed.

Copy link
Collaborator

@cosmicBboy cosmicBboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved! left one suggestion

pandera/pandera.py Outdated Show resolved Hide resolved
@codecov-io
Copy link

Codecov Report

Merging #86 into master will increase coverage by 0.12%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #86      +/-   ##
==========================================
+ Coverage   92.83%   92.96%   +0.12%     
==========================================
  Files           2        2              
  Lines         335      341       +6     
==========================================
+ Hits          311      317       +6     
  Misses         24       24
Impacted Files Coverage Δ
pandera/pandera.py 92.92% <100%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4d38a4d...5b864b1. Read the comment docs.

@mastersplinter mastersplinter merged commit c1a3616 into master Aug 15, 2019
@mastersplinter mastersplinter deleted the feature/better_wrapper_error branch August 15, 2019 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants