Skip to content

Commit

Permalink
- Fix coding convention
Browse files Browse the repository at this point in the history
  • Loading branch information
maycuatroi committed Oct 13, 2024
1 parent 4a85faf commit 45345d7
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
14 changes: 13 additions & 1 deletion src/latexify/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ def extract_function_name_or_none(node: ast.Call) -> str | None:
return None


def ast_function_def(*args, **kwargs) -> ast.FunctionDef:
def create_function_def(*args, **kwargs) -> ast.FunctionDef:
"""Creates a FunctionDef node.
This function generates an `ast.FunctionDef` node, optionally removing
the `type_params` keyword argument for Python versions below 3.12.
Args:
*args: Positional arguments for `ast.FunctionDef`.
**kwargs: Keyword arguments for `ast.FunctionDef`.
Returns:
ast.FunctionDef: The generated FunctionDef node.
"""
if sys.version_info.minor < 12:
kwargs.pop("type_params", None)
return ast.FunctionDef(*args, **kwargs)
59 changes: 38 additions & 21 deletions src/latexify/ast_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any

import pytest
import sys

from latexify import ast_utils, test_utils

Expand Down Expand Up @@ -34,27 +35,6 @@ def test_make_attribute() -> None:
)


@pytest.mark.parametrize(
"value,expected",
[
(None, ast.Constant(value=None)),
(False, ast.Constant(value=False)),
(True, ast.Constant(value=True)),
(..., ast.Constant(value=Ellipsis)),
(123, ast.Constant(value=123)),
(4.5, ast.Constant(value=4.5)),
(6 + 7j, ast.Constant(value=6 + 7j)),
("foo", ast.Constant(value="foo")),
(b"bar", ast.Constant(value=b"bar")),
],
)
def test_make_constant_legacy(value: Any, expected: ast.Constant) -> None:
test_utils.assert_ast_equal(
observed=ast_utils.make_constant(value),
expected=expected,
)


@pytest.mark.parametrize(
"value,expected",
[
Expand Down Expand Up @@ -218,3 +198,40 @@ def test_extract_int_invalid() -> None:
)
def test_extract_function_name_or_none(value: ast.Call, expected: str | None) -> None:
assert ast_utils.extract_function_name_or_none(value) == expected


def test_create_function_def() -> None:
expected_args = ast.arguments(
posonlyargs=[],
args=[ast.arg(arg="x")],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
)

kwargs = {
"name": "test_func",
"args": expected_args,
"body": [ast.Return(value=ast.Name(id="x", ctx=ast.Load()))],
"decorator_list": [],
"returns": None,
"type_comment": None,
"lineno": 1,
"col_offset": 0,
"end_lineno": 2,
"end_col_offset": 0,
}
if sys.version_info.minor >= 12:
kwargs["type_params"] = []

func_def = ast_utils.create_function_def(**kwargs)
assert isinstance(func_def, ast.FunctionDef)
assert func_def.name == "test_func"

assert func_def.args.posonlyargs == expected_args.posonlyargs
assert func_def.args.args == expected_args.args
assert func_def.args.kwonlyargs == expected_args.kwonlyargs
assert func_def.args.kw_defaults == expected_args.kw_defaults
assert func_def.args.defaults == expected_args.defaults
2 changes: 1 addition & 1 deletion src/latexify/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def f(x):

expected = ast.Module(
body=[
ast_function_def(
ast_utils.create_function_def(
name="f",
args=ast.arguments(
posonlyargs=[],
Expand Down
4 changes: 2 additions & 2 deletions src/latexify/transformers/assignment_reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any

from latexify import exceptions
from latexify.ast_utils import ast_function_def
from latexify import ast_utils


class AssignmentReducer(ast.NodeTransformer):
Expand Down Expand Up @@ -68,7 +68,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
# Pop stack
self._assignments = parent_assignments
type_params = getattr(node, "type_params", [])
return ast_function_def(
return ast_utils.create_function_def(
name=node.name,
args=node.args,
body=[return_transformed],
Expand Down
2 changes: 1 addition & 1 deletion src/latexify/transformers/assignment_reducer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _make_ast(body: list[ast.stmt]) -> ast.Module:
"""
return ast.Module(
body=[
ast_utils.ast_function_def(
ast_utils.create_function_def(
name="f",
args=ast.arguments(
args=[ast.arg(arg="x")],
Expand Down
2 changes: 1 addition & 1 deletion src/latexify/transformers/docstring_remover_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def f():
tree = parser.parse_function(f).body[0]
assert isinstance(tree, ast.FunctionDef)

expected = ast_utils.ast_function_def(
expected = ast_utils.create_function_def(
name="f",
body=[
ast.Assign(
Expand Down
4 changes: 2 additions & 2 deletions src/latexify/transformers/identifier_replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import keyword
from typing import cast

from latexify.ast_utils import ast_function_def
from latexify import ast_utils


class IdentifierReplacer(ast.NodeTransformer):
Expand Down Expand Up @@ -60,7 +60,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
defaults=visited.args.defaults,
)
type_params = getattr(visited, "type_params", [])
return ast_function_def(
return ast_utils.create_function_def(
name=self._mapping.get(visited.name, visited.name),
args=args,
body=visited.body,
Expand Down
7 changes: 3 additions & 4 deletions src/latexify/transformers/identifier_replacer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import pytest

from latexify import test_utils
from latexify.ast_utils import ast_function_def
from latexify import ast_utils, test_utils
from latexify.transformers.identifier_replacer import IdentifierReplacer


Expand Down Expand Up @@ -41,7 +40,7 @@ def test_functiondef_with_posonlyargs() -> None:
# @d
# def f(x=a, /, y=b, *, z=c):
# pass
source = ast_function_def(
source = ast_utils.create_function_def(
name="f",
args=ast.arguments(
posonlyargs=[ast.arg(arg="x")],
Expand All @@ -60,7 +59,7 @@ def test_functiondef_with_posonlyargs() -> None:
type_params=[],
)

expected = ast_function_def(
expected = ast_utils.create_function_def(
name="F",
args=ast.arguments(
posonlyargs=[ast.arg(arg="X")],
Expand Down

0 comments on commit 45345d7

Please sign in to comment.