Debian Codegraph @

tests/introspection/test_literal_values.py

raw
1 import sys
2 import typing as t
3 from textwrap import dedent
4 from typing import Any
5
6 import pytest
7 import typing_extensions as t_e
8
9 from typing_inspection import typing_objects
10 from typing_inspection.introspection import get_literal_values
11
12 TypingAlias = t_e.TypeAliasType('TypingAlias', t.Literal[1, 'a'])
13
14
15 @pytest.mark.parametrize(
16 ['annotation', 'expected'],
17 [
18 (t.Literal[1, 'a'], [1, 'a']),
19 (t.Literal[1, 'a'], [1, 'a']),
20 (t.Literal[1, TypingAlias], [1, TypingAlias]),
21 ],
22 )
23 def test_literal_values_skip_aliases_no_type_check(annotation: Any, expected: list[Any]) -> None:
24 result = get_literal_values(annotation, type_check=False, unpack_type_aliases='skip')
25 assert list(result) == expected
26
27
28 def test_literal_values_skip_aliases_type_check() -> None:
29 with pytest.raises(TypeError):
30 list(get_literal_values(t.Literal[1, TypingAlias], type_check=True, unpack_type_aliases='skip'))
31
32
33 def test_literal_values_type_check() -> None:
34 literal = t.Literal[1, True, False, b'', '', None, typing_objects.NoneType] # noqa: PYI061
35 expected = [1, True, False, b'', '', None]
36 assert list(get_literal_values(literal, type_check=True, unpack_type_aliases='skip')) == expected
37 assert list(get_literal_values(literal, type_check=True, unpack_type_aliases='eager')) == expected
38
39 with pytest.raises(TypeError):
40 list(get_literal_values(t.Literal[1.0], type_check=True, unpack_type_aliases='skip'))
41
42 with pytest.raises(TypeError):
43 list(get_literal_values(t.Literal[1.0], type_check=True, unpack_type_aliases='eager'))
44
45
46 def test_literal_values_unpack_type_aliases() -> None:
47 TestType0 = t_e.TypeAliasType('TestType0', t_e.Literal['a'])
48 TestType1 = t_e.TypeAliasType('TestType1', t_e.Literal[TestType0, 'b'])
49 TestType2 = t_e.TypeAliasType('TestType2', t.Literal[TestType1, 'c'])
50
51 expected = ['a', 'b', 'c', 'd']
52
53 assert list(get_literal_values(t.Literal[TestType2, 'd'], unpack_type_aliases='eager')) == expected
54
55
56 @pytest.mark.skipif(sys.version_info < (3, 12), reason='Requires new `type` statement syntax.')
57 def test_literal_values_unpack_type_aliases_undefined(create_module) -> None:
58 code = dedent("""
59 from typing import Literal
60
61 type TestType0 = Literal[Undefined, 'a']
62 type TestType1 = Literal[TestType0, 'b']
63 """)
64
65 module = create_module(code)
66 expected = [module.TestType0, 'b', 'c']
67
68 assert list(get_literal_values(t.Literal[module.TestType1, 'c'], unpack_type_aliases='lenient')) == expected
69
70 with pytest.raises(NameError):
71 list(get_literal_values(t.Literal[module.TestType1, 'c'], unpack_type_aliases='eager'))
72
73 with pytest.raises(TypeError):
74 # As `TestType0` can't be unpacked, it isn't a valid literal value:
75 list(get_literal_values(t.Literal[module.TestType1, 'c'], type_check=True, unpack_type_aliases='lenient'))
76
77
78 def test_literal_values_unhashable_type() -> None:
79 assert list(get_literal_values(t_e.Literal[[1, 'a']])) == [[1, 'a']]
80
81
82 @pytest.mark.skipif(
83 sys.version_info >= (3, 13, 15),
84 reason='Unashable arguments are deduplicated (https://github.com/python/cpython/pull/153914)',
85 )
86 def test_duplicate_literal_values_unhashable_type() -> None:
87 assert list(get_literal_values(t_e.Literal[[1, 'a'], [1, 'a']])) == [[1, 'a'], [1, 'a']] # noqa: PYI062