dataenforce icon indicating copy to clipboard operation
dataenforce copied to clipboard

Add support for NewType, typing.List, typing.Dict type hinting

Open martijnlentink opened this issue 5 years ago • 5 comments

A NewType definition will fail the inspect.isclass check. Retrieving and using the __supertype__ will potentially fix this.

Example.

from typing import NewType, Dict
UserId = NewType("UserId", int)

DUserDataset = Dataset["user": UserId, "properties": Dict[str, str]]

martijnlentink avatar Jan 07 '21 13:01 martijnlentink

Hi @martijnlentink , Thank you for the pull request, great additions. Unfortunately I can't make it work. I tried Python 3.6, 3.7, 3.8, and on all it seems NewType is not something you can apply isinstance on.

Here is the unit test that I wrote, does it work for you?

def test_validate_newtype():
    from typing import NewType

    UserId = NewType('UserId', int)

    df = pd.DataFrame(dict(a=[UserId(1), UserId(2), UserId(3)], b=["a","b","c"]))

    @validate
    def process(data: Dataset["a": UserId, "b": object]):
        pass

    process(df)

CedricFR avatar Jan 11 '21 12:01 CedricFR

Hmm, you are right. I should've mentioned that I did not have time to test it yet 🙈. Maybe change the isinstance check to hasattr(stop_type, "__supertype__"). What do you think?

martijnlentink avatar Jan 11 '21 12:01 martijnlentink

I haven't found any other way that works, so I would say yes let's go with this

CedricFR avatar Jan 11 '21 13:01 CedricFR

Okay, done. Let me know if you want me to squash my commits 👍

martijnlentink avatar Jan 12 '21 08:01 martijnlentink

Are we able to merge this to production?

martijnlentink avatar Mar 23 '21 11:03 martijnlentink