예제 #1
0
def test_typing_invalid():

    for ttype in [
        typing.List,
        typing.List[str],
        typing.Optional[int],
        typing.Set,
        typing.Set[int],
        typing.Dict,
        typing.Dict[int, str],
        typing.Tuple,
        typing.Tuple[int, int],
    ]:
        with pytest.raises(DagsterInvariantViolationError):
            check_dagster_type(ttype, None)
예제 #2
0
def test_check_dagster_type():
    res = check_dagster_type(Any, 'bar')
    assert res.success

    res = check_dagster_type(Any, None)
    assert res.success

    res = check_dagster_type(str, 'bar')
    assert res.success

    res = check_dagster_type(str, 1)
    assert not res.success
    assert 'Value "1" of python type "int" must be a string.' in res.description

    res = check_dagster_type(String, 'bar')
    assert res.success

    res = check_dagster_type(list, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[Any], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[str], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(Any, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[Any, Any], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, str], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, int], {'foo': 'bar'})
    assert not res.success
    assert 'Value "bar" of python type "str" must be a int.' in res.description

    res = check_dagster_type(tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[Any, Any], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar', 'baz'))
    assert not res.success
    assert (
        'Tuple with key TypedPythonTupleString.String requires 2 entries, received 3 values'
    ) in res.description

    res = check_dagster_type(Set, {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[Any], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[str], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[int], {'foo'})
    assert not res.success
    assert 'Value "foo" of python type "str" must be a int.' in res.description

    res = check_dagster_type(Optional[str], 'str')
    assert res.success

    res = check_dagster_type(Optional[str], None)
    assert res.success

    class Foo(object):
        pass

    class Bar(object):
        pass

    res = check_dagster_type(Foo, Foo())
    assert res.success

    res = check_dagster_type(Foo, Bar())
    assert not res.success
    assert re.match(
        re.escape('Value of type <class \'dagster_tests.utils_tests.test_test_utils')
        + '('
        + re.escape('.test_check_dagster_type.<locals>')
        + ')?'
        + re.escape(
            '.Bar\'> failed type check for Dagster type Implicit[Foo], expected value to be '
            'of Python type Foo.'
        ),
        res.description,
    )

    @dagster_type
    class Baz(object):
        pass

    res = check_dagster_type(Baz, Baz())
    assert res.success

    @dagster_type(type_check=lambda _: TypeCheck(success=True))
    class Quux(object):
        pass

    res = check_dagster_type(Quux, Quux())
    assert res.success

    @dagster_type(type_check=lambda _: TypeCheck(success=False))
    class Corge(object):
        pass

    res = check_dagster_type(Corge, Corge())
    assert not res.success

    @dagster_type(type_check=lambda _: TypeCheck(True, 'a_check', []))
    class Garble(object):
        pass

    res = check_dagster_type(Garble, Garble())
    assert isinstance(res, TypeCheck)
    assert res.success
    assert res.description == 'a_check'

    @dagster_type(type_check=lambda _: True)
    class Bboo(object):
        pass

    res = check_dagster_type(Bboo, Bboo())
    assert isinstance(res, TypeCheck)
    assert res.success

    @dagster_type(type_check=lambda _: False)
    class Bboott(object):
        pass

    res = check_dagster_type(Bboott, Bboott())
    assert isinstance(res, TypeCheck)
    assert not res.success
예제 #3
0
def test_check_dagster_type():
    res = check_dagster_type(Any, 'bar')
    assert res is None

    res = check_dagster_type(Any, None)
    assert res is None

    res = check_dagster_type(str, 'bar')
    assert res is None

    with pytest.raises(
            Failure,
            match=re.escape(
                'Value "1" of python type "int" must be a string.')):
        res = check_dagster_type(str, 1)

    res = check_dagster_type(String, 'bar')
    assert res is None

    res = check_dagster_type(list, ['foo', 'bar'])
    assert res is None

    res = check_dagster_type(List, ['foo', 'bar'])
    assert res is None

    res = check_dagster_type(List[Any], ['foo', 'bar'])
    assert res is None

    res = check_dagster_type(List[str], ['foo', 'bar'])
    assert res is None

    res = check_dagster_type(Any, {'foo': 'bar'})
    assert res is None

    res = check_dagster_type(dict, {'foo': 'bar'})
    assert res is None

    res = check_dagster_type(Dict, {'foo': 'bar'})
    assert res is None

    res = check_dagster_type(Dict[Any, Any], {'foo': 'bar'})
    assert res is None

    res = check_dagster_type(Dict[str, str], {'foo': 'bar'})
    assert res is None

    with pytest.raises(Failure,
                       match=re.escape(
                           'Value "bar" of python type "str" must be a int.')):
        check_dagster_type(Dict[str, int], {'foo': 'bar'})

    res = check_dagster_type(tuple, ('foo', 'bar'))
    assert res is None

    res = check_dagster_type(Tuple, ('foo', 'bar'))
    assert res is None

    res = check_dagster_type(Tuple[Any, Any], ('foo', 'bar'))
    assert res is None

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res is None

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res is None

    with pytest.raises(
            Failure,
            match=re.escape(
                'Tuple with key TypedPythonTupleString.String requires 2 entries. Received tuple '
                'with 3 values'),
    ):
        res = check_dagster_type(Tuple[str, str], ('foo', 'bar', 'baz'))

    res = check_dagster_type(Set, {'foo', 'bar'})
    assert res is None

    res = check_dagster_type(Set[Any], {'foo', 'bar'})
    assert res is None

    res = check_dagster_type(Set[str], {'foo', 'bar'})
    assert res is None

    with pytest.raises(Failure,
                       match=re.escape(
                           'Value "foo" of python type "str" must be a int.')):
        res = check_dagster_type(Set[int], {'foo'})

    res = check_dagster_type(Optional[str], 'str')
    assert res is None

    res = check_dagster_type(Optional[str], None)
    assert res is None

    class Foo(object):
        pass

    class Bar(object):
        pass

    res = check_dagster_type(Foo, Foo())
    assert res is None

    with pytest.raises(
            Failure,
            match=re.escape(
                'Value of type <class \'dagster_tests.utils_tests.test_test_utils'
            ) + '(' + re.escape('.test_check_dagster_type.<locals>') + ')?' +
            re.escape(
                '.Bar\'> failed type check for Dagster type Implicit[Foo], expected value to be '
                'of Python type Foo.'),
    ):
        res = check_dagster_type(Foo, Bar())

    @dagster_type
    class Baz(object):
        pass

    res = check_dagster_type(Baz, Baz())
    assert res is None

    @dagster_type(type_check=lambda _: None)
    class Quux(object):
        pass

    res = check_dagster_type(Quux, Quux())
    assert res is None

    def raise_failure(_):
        raise Failure('failed')

    @dagster_type(type_check=raise_failure)
    class Corge(object):
        pass

    with pytest.raises(Failure, match='failed'):
        res = check_dagster_type(Corge, Corge())

    @dagster_type(typecheck_metadata_fn=lambda _: TypeCheck('a_check', []))
    class Garble(object):
        pass

    res = check_dagster_type(Garble, Garble())
    assert isinstance(res, TypeCheck)
    assert res.description == 'a_check'
예제 #4
0
def test_check_dagster_type():
    res = check_dagster_type(Any, 'bar')
    assert res.success

    res = check_dagster_type(Any, None)
    assert res.success

    res = check_dagster_type(str, 'bar')
    assert res.success

    res = check_dagster_type(str, 1)
    assert not res.success
    assert 'Value "1" of python type "int" must be a string.' in res.description

    res = check_dagster_type(String, 'bar')
    assert res.success

    res = check_dagster_type(list, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[Any], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[str], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(Any, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[Any, Any], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, str], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, int], {'foo': 'bar'})
    assert not res.success
    assert 'Value "bar" of python type "str" must be a int.' in res.description

    res = check_dagster_type(tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[Any, Any], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar', 'baz'))
    assert not res.success
    assert (
        'Tuple with key TypedPythonTupleString.String requires 2 entries, received 3 values'
    ) in res.description

    res = check_dagster_type(Set, {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[Any], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[str], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[int], {'foo'})
    assert not res.success
    assert 'Value "foo" of python type "str" must be a int.' in res.description

    res = check_dagster_type(Optional[str], 'str')
    assert res.success

    res = check_dagster_type(Optional[str], None)
    assert res.success

    class Foo(object):
        pass

    class Bar(object):
        pass

    res = check_dagster_type(Foo, Foo())
    assert res.success

    res = check_dagster_type(Foo, Bar())
    assert not res.success
    assert re.match(
        re.escape(
            'Value of type <class \'dagster_tests.utils_tests.test_test_utils')
        + '(' + re.escape('.test_check_dagster_type.<locals>') + ')?' +
        re.escape(
            '.Bar\'> failed type check for Dagster type Implicit[Foo], expected value to be '
            'of Python type Foo.'),
        res.description,
    )

    @dagster_type
    class Baz(object):
        pass

    res = check_dagster_type(Baz, Baz())
    assert res.success
예제 #5
0
def test_check_dagster_type():
    res = check_dagster_type(Any, 'bar')
    assert res.success

    res = check_dagster_type(Any, None)
    assert res.success

    res = check_dagster_type(str, 'bar')
    assert res.success

    res = check_dagster_type(str, 1)
    assert not res.success
    assert 'Value "1" of python type "int" must be a string.' in res.description

    res = check_dagster_type(String, 'bar')
    assert res.success

    res = check_dagster_type(list, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List, ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[Any], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(List[str], ['foo', 'bar'])
    assert res.success

    res = check_dagster_type(Any, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict, {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[Any, Any], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, str], {'foo': 'bar'})
    assert res.success

    res = check_dagster_type(Dict[str, int], {'foo': 'bar'})
    assert not res.success
    assert 'Value "bar" of python type "str" must be a int.' in res.description

    res = check_dagster_type(tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple, ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[Any, Any], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar'))
    assert res.success

    res = check_dagster_type(Tuple[str, str], ('foo', 'bar', 'baz'))
    assert not res.success
    assert (
        'Tuple with key TypedPythonTupleString.String requires 2 entries, received 3 values'
    ) in res.description

    res = check_dagster_type(Set, {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[Any], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[str], {'foo', 'bar'})
    assert res.success

    res = check_dagster_type(Set[int], {'foo'})
    assert not res.success
    assert 'Value "foo" of python type "str" must be a int.' in res.description

    res = check_dagster_type(Optional[str], 'str')
    assert res.success

    res = check_dagster_type(Optional[str], None)
    assert res.success

    @usable_as_dagster_type
    class Baz(object):
        pass

    res = check_dagster_type(Baz, Baz())
    assert res.success