Repository: cosmicBboy/pandera

Issue:
handle `self` argument when using check_input in class method when str obj_getter is supplied
Check is the wrapped function is a class method:

https://stackoverflow.com/questions/5963729/check-if-a-function-is-a-method-of-some-object

if so, ignore the first argument.

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=h1) Report
> Merging [#64](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=desc) into [master](https://codecov.io/gh/cosmicBboy/pandera/commit/3c96cb1ef97dc818ec047f5745a3341d5d70993e?src=pr&el=desc) will **increase** coverage by `0.51%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/cosmicBboy/pandera/pull/64/graphs/tree.svg?width=650&token=OXz3BoBZMN&height=150&src=pr)](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #64      +/-   ##
=========================================
+ Coverage   89.69%   90.2%   +0.51%     
=========================================
  Files           1       1              
  Lines         291     296       +5     
=========================================
+ Hits          261     267       +6     
+ Misses         30      29       -1
```


| [Impacted Files](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandera/pandera.py](https://codecov.io/gh/cosmicBboy/pandera/pull/64/diff?src=pr&el=tree#diff-cGFuZGVyYS9wYW5kZXJhLnB5) | `90.2% <100%> (+0.51%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=footer). Last update [3c96cb1...a54c5c4](https://codecov.io/gh/cosmicBboy/pandera/pull/64?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pandera/pandera.py b/pandera/pandera.py
index b11eaa4..c191f6c 100644
--- a/pandera/pandera.py
+++ b/pandera/pandera.py
@@ -684,9 +684,8 @@ class Column(SeriesSchemaBase):
             then assumes one of the valid pandas string values:
             http://pandas.pydata.org/pandas-docs/stable/basics.html#dtypes
         :type pandas_dtype: str|PandasDtype
-        :param checks: If element_wise is True, then callable signature should
-            be:
-            x -> x where x is a scalar element in the column. Otherwise,
+        :param checks: if element_wise is True, then callable signature should
+            be: x -> bool where x is a scalar element in the column. Otherwise,
             x is assumed to be a pandas.Series object.
         :type checks: callable
         :param nullable: Whether or not column can contain null values.
@@ -741,6 +740,18 @@ class Column(SeriesSchemaBase):
         return "<Schema Column: '%s' type=%s>" % (self._name, dtype)
 
 
+def _get_fn_argnames(fn):
+    if sys.version_info.major >= 3:
+        arg_spec_args = inspect.getfullargspec(fn).args
+    else:
+        arg_spec_args = inspect.getargspec(fn).args
+
+    if inspect.ismethod(fn) and arg_spec_args[0] == "self":
+        # don't include "self" argument
+        arg_spec_args = arg_spec_args[1:]
+    return arg_spec_args
+
+
 def check_input(schema, obj_getter=None):
     """Validate function argument when function is called.
 
@@ -770,10 +781,7 @@ def check_input(schema, obj_getter=None):
             if obj_getter in kwargs:
                 kwargs[obj_getter] = schema.validate(kwargs[obj_getter])
             else:
-                if sys.version_info.major >= 3:
-                    arg_spec_args = inspect.getfullargspec(fn).args
-                else:
-                    arg_spec_args = inspect.getargspec(fn).args
+                arg_spec_args = _get_fn_argnames(fn)
                 args_dict = OrderedDict(
                     zip(arg_spec_args, args))
                 args_dict[obj_getter] = schema.validate(args_dict[obj_getter])


Test patch:
diff --git a/tests/test_pandera.py b/tests/test_pandera.py
index d8f5aeb..063ac50 100644
--- a/tests/test_pandera.py
+++ b/tests/test_pandera.py
@@ -272,6 +272,50 @@ def test_check_function_decorator_transform():
     assert "column2" in result2[1]
 
 
+def test_check_input_method_decorators():
+    in_schema = DataFrameSchema({"column1": Column(String)})
+    out_schema = DataFrameSchema({"column2": Column(Int)})
+    dataframe = pd.DataFrame({"column1": ["a", "b", "c"]})
+
+    def _transform_helper(df):
+        return df.assign(column2=[1, 2, 3])
+
+    class TransformerClass(object):
+
+        @check_input(in_schema)
+        @check_output(out_schema)
+        def transform_first_arg(self, df):
+            return _transform_helper(df)
+
+        @check_input(in_schema, 0)
+        @check_output(out_schema)
+        def transform_first_arg_with_list_getter(self, df):
+            return _transform_helper(df)
+
+        @check_input(in_schema, 1)
+        @check_output(out_schema)
+        def transform_secord_arg_with_list_getter(self, x, df):
+            return _transform_helper(df)
+
+        @check_input(in_schema, "df")
+        @check_output(out_schema)
+        def transform_secord_arg_with_dict_getter(self, x, df):
+            return _transform_helper(df)
+
+    def _assert_expectation(result_df):
+        assert isinstance(result_df, pd.DataFrame)
+        assert "column2" in result_df.columns
+
+    transformer = TransformerClass()
+    _assert_expectation(transformer.transform_first_arg(dataframe))
+    _assert_expectation(
+        transformer.transform_first_arg_with_list_getter(dataframe))
+    _assert_expectation(
+        transformer.transform_secord_arg_with_list_getter(None, dataframe))
+    _assert_expectation(
+        transformer.transform_secord_arg_with_dict_getter(None, dataframe))
+
+
 def test_string_dtypes():
     # TODO: add tests for all datatypes
     schema = DataFrameSchema(