File: test_pycode_ast.py

package info (click to toggle)
sphinx 8.2.3-12
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 26,960 kB
  • sloc: python: 105,864; javascript: 6,474; perl: 449; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (73 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Test pycode.ast"""

from __future__ import annotations

import ast

import pytest

from sphinx.pycode.ast import unparse as ast_unparse


@pytest.mark.parametrize(
    ('source', 'expected'),
    [
        ('a + b', 'a + b'),                        # Add
        ('a and b', 'a and b'),                    # And
        ('os.path', 'os.path'),                    # Attribute
        ('1 * 2', '1 * 2'),                        # BinOp
        ('a & b', 'a & b'),                        # BitAnd
        ('a | b', 'a | b'),                        # BitOr
        ('a ^ b', 'a ^ b'),                        # BitXor
        ('a and b and c', 'a and b and c'),        # BoolOp
        ("b'bytes'", "b'bytes'"),                  # Bytes
        ('object()', 'object()'),                  # Call
        ('1234', '1234'),                          # Constant, Num
        ("{'key1': 'value1', 'key2': 'value2'}",
         "{'key1': 'value1', 'key2': 'value2'}"),  # Dict
        ('a / b', 'a / b'),                        # Div
        ('...', '...'),                            # Ellipsis
        ('a // b', 'a // b'),                      # FloorDiv
        ('Tuple[int, int]', 'Tuple[int, int]'),    # Index, Subscript
        ('~1', '~1'),                              # Invert
        ('lambda x, y: x + y',
         'lambda x, y: ...'),                      # Lambda
        ('[1, 2, 3]', '[1, 2, 3]'),                # List
        ('a << b', 'a << b'),                      # LShift
        ('a @ b', 'a @ b'),                        # MatMult
        ('a % b', 'a % b'),                        # Mod
        ('a * b', 'a * b'),                        # Mult
        ('sys', 'sys'),                            # Name, NameConstant
        ('not a', 'not a'),                        # Not
        ('a or b', 'a or b'),                      # Or
        ('a**b', 'a**b'),                          # Pow
        ('a >> b', 'a >> b'),                      # RShift
        ('{1, 2, 3}', '{1, 2, 3}'),                # Set
        ('a - b', 'a - b'),                        # Sub
        ("'str'", "'str'"),                        # Str
        ('+a', '+a'),                              # UAdd
        ('-1', '-1'),                              # UnaryOp
        ('-a', '-a'),                              # USub
        ('(1, 2, 3)', '(1, 2, 3)'),                # Tuple
        ('()', '()'),                              # Tuple (empty)
        ('(1,)', '(1,)'),                          # Tuple (single item)
        ('lambda x=0, /, y=1, *args, z, **kwargs: x + y + z',
         'lambda x=0, /, y=1, *args, z, **kwargs: ...'),  # posonlyargs
        ('0x1234', '0x1234'),                      # Constant
        ('1_000_000', '1_000_000'),                # Constant
        ('Tuple[:,:]', 'Tuple[:, :]'),             # Index, Subscript, 2x Slice
        ('Tuple[1:2]', 'Tuple[1:2]'),              # Index, Subscript, Slice(no-step)
        ('Tuple[1:2:3]', 'Tuple[1:2:3]'),          # Index, Subscript, Slice
        ('x[:, np.newaxis, :, :]',
         'x[:, np.newaxis, :, :]'),                # Index, Subscript, numpy extended syntax
        ('y[:, 1:3][np.array([0, 2, 4]), :]',
         'y[:, 1:3][np.array([0, 2, 4]), :]'),     # Index, 2x Subscript, numpy extended syntax
    ],
)  # fmt: skip
def test_unparse(source, expected):
    module = ast.parse(source)
    assert ast_unparse(module.body[0].value, source) == expected


def test_unparse_None() -> None:
    assert ast_unparse(None) is None