Skip to content

Commit

Permalink
Fix error in test suite under Python 3.6.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgranade committed Jul 9, 2020
1 parent 54cb36e commit 0d78ca7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions dataenforce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import inspect
from functools import wraps
import pandas as pd
from typing import _TypingEmpty, _tp_cache, Generic,get_type_hints
from typing import _TypingEmpty, _tp_cache, Generic, get_type_hints
import numpy as np
try:
from typing import GenericMeta # Python 3.6
except ImportError: # Python 3.7
class GenericMeta(type): pass
from typing import GenericMeta # Python 3.6
except ImportError: # Python 3.7
class GenericMeta(type):
pass


def validate(f):
signature = inspect.signature(f)
Expand Down Expand Up @@ -50,6 +52,7 @@ def wrapper(*args, **kwargs):

return wrapper


def _get_columns_dtypes(p):
columns = set()
dtypes = {}
Expand All @@ -74,6 +77,7 @@ def _get_columns_dtypes(p):

return columns, dtypes


class DatasetMeta(GenericMeta):
"""Metaclass for Dataset (internal)."""

Expand Down Expand Up @@ -101,6 +105,7 @@ def __getitem__(self, parameters):

return meta


class Dataset(pd.DataFrame, extra=Generic, metaclass=DatasetMeta):
"""Defines type Dataset to serve as column name & type enforcement for pandas DataFrames"""
__slots__ = ()
Expand Down
7 changes: 5 additions & 2 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ def process(data: Dataset[...]):
with pytest.raises(TypeError):
process(False)


def test_validate_dtypes():
df = pd.DataFrame(dict(a=[1,2,3], b=[4.1,5.1,6.1], c=["a", "b", "c"], d=[datetime.now().replace(hour=x) for x in [7,8,9]]))
df = pd.DataFrame(dict(a=[1, 2, 3], b=[4.1, 5.1, 6.1], c=["a", "b", "c"], d=[datetime.now().replace(hour=x) for x in [7, 8, 9]]))

@validate
def process1(data: Dataset["a": int, "b": np.float, "c": object, "d": np.datetime64]):
def process1(data: Dataset["a": np.int64, "b": np.float, "c": object, "d": np.datetime64]):
pass

@validate
def process2(data: Dataset["a": float, "b", ...]):
pass

@validate
def process3(data: Dataset["a": np.datetime64, ...]):
pass
Expand Down

0 comments on commit 0d78ca7

Please sign in to comment.