Пример #1
0
def test_optional():
    s = spec_from(HasOptional)

    assert assert_spec(s, {'k': 123}) == {'k': 123}
    assert assert_spec(s, {'k': None}) == {'k': None}

    check_spec_error(s, {'k': "not an int"}, "not an int")
Пример #2
0
def test_generic_typevars_unconstrained_bound():
    s = spec_from(BoundGeneric)

    d = assert_spec(s, {'t': 123, 'v': 'string'})

    assert d == {'t': 123, 'v': 'string'}

    check_spec_error(s, {'t': "not an int", 'v': 'string'}, "not an int")
Пример #3
0
def test_lists_of_forward_references():
    s = spec_from(HasListsOfForwardReference)

    d = assert_spec(s, {'k': [{'k': []}]})

    assert d == {'k': [{'k': []}]}

    check_spec_error(s, {'k': ["not a NeedsForwardReference"]}, "not a NeedsForwardReference")
Пример #4
0
def test_forward_references():
    s = spec_from(HasForwardReference)

    d = assert_spec(s, {'k': {'k': None}})

    assert d == {'k': {'k': None}}

    check_spec_error(s, {'k': "not a NeedsForwardReference"}, "not a NeedsForwardReference")
Пример #5
0
def test_list():
    s = spec_from(HasList)

    d = assert_spec(s, {'k': [123, 456]})

    assert d == {'k': [123, 456]}

    check_spec_error(s, {'k': ["not an int"]}, "not an int")
Пример #6
0
def test_primitives():
    s = spec_from(JustPrimitive)

    d = assert_spec(s, {'k': 123})

    assert d == {'k': 123}

    check_spec_error(s, {'k': "not an int"}, "not an int")
Пример #7
0
def test_classvar_should_never_appear():
    s = spec_from(HasClassVar)

    d = assert_spec(s, {})

    assert d == {}

    check_spec_error(s, {'a': 123}, "ClassVar")
    check_spec_error(s, {'a': "wrong type doesn't matter"}, "ClassVar")
Пример #8
0
def test_non_generic_class_with_typevar_annotations():
    s = spec_from(NonGenericWithTypevars)

    d = assert_spec(s, {'a': 123, 'b': 456})

    assert d == {'a': 123, 'b': 456}

    # Need to ensure all annotations marked with unbound generic V
    # are of the same type
    int_T = 123
    str_T = "mooooo"
    check_spec_error(s, {'a': int_T, 'b': str_T}, str_T)
Пример #9
0
def test_generic_typevars_unconstrained_unbound():
    s = spec_from(UnboundGeneric)

    d = assert_spec(s, {'t': 123, 'v': "V type", 'another_v': "V type"})

    assert d == {'t': 123, 'v': "V type", 'another_v': "V type"}

    # Should not conform if all annotations marked with unbound generic V
    # are not of the same type
    int_V = 123
    str_V = "mooooo"
    check_spec_error(s, {'t': 123, 'v': int_V, 'another_v': str_V}, str_V)
Пример #10
0
def test_any():
    s = spec_from(HasAny)

    d = assert_spec(s, {'a': "Whatever"})

    assert d == {'a': "Whatever"}