Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parsing of function in CodeInput using ast #86

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

agoscinski
Copy link
Collaborator

@agoscinski agoscinski commented Nov 30, 2024

The problem with inspect is that ignores the actual formatting provided by the user and that it sometimes stores directly the related objects (e.g. type annotation x: int stores the type int instead of the string int which makes it hard to pass as string later on). With ast this should be resolved. See example

def mock_function_1(x: int, y: int):
    """
    This is an example function.
    It adds two numbers.
    """
    if x > 0:
        return x + y
    else:
        return y

import ast, inspect
module = ast.parse(inspect.getsource(mock_function_1))
ast.dump(module)

#Out: "Module(body=[FunctionDef(name='mock_function_1', args=arguments(posonlyargs=[], args=[arg(arg='x', annotation=Name(id='int', ctx=Load())), arg(arg='y', annotation=Name(id='int', ctx=Load()))], kwonlyargs=[], kw_defaults=[], defaults=[]), body=[Expr(value=Constant(value='\\n    This is an example function.\\n    It adds two numbers.\\n    ')), If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Gt()], comparators=[Constant(value=0)]), body=[Return(value=BinOp(left=Name(id='x', ctx=Load()), op=Add(), right=Name(id='y', ctx=Load())))], orelse=[Return(value=Name(id='y', ctx=Load()))])], decorator_list=[])], type_ignores=[])"

📚 Documentation preview 📚: https://scicode-widgets--86.org.readthedocs.build/en/86/

@agoscinski agoscinski force-pushed the refactor-function-parsing branch from 9cb0b9b to 6b16fdc Compare December 11, 2024 07:09
@agoscinski
Copy link
Collaborator Author

Sadly the ast module by design ignores comment which makes it not applicable for this approach

@agoscinski agoscinski changed the title Refactor parsing of function in CodeInput Refactor parsing of function in CodeInput using ast Dec 11, 2024
@agoscinski agoscinski changed the title Refactor parsing of function in CodeInput using ast Refactor parsing of function in CodeInput using ast Dec 11, 2024
@agoscinski
Copy link
Collaborator Author

For reference this is how get_function_body could have been changed, but we cannot take this approach since it removes comments that are essential for the function body

    @staticmethod
    def get_function_body(function: types.FunctionType) -> str:
        function_source, function_definition = CodeInput._get_function_source_and_def(
            function
        )

        ast_functon_body = function_definition.body[-1]

        source_function_body = ast.get_source_segment(
            function_source, ast_functon_body, padded=True
        )
        breakpoint()
        if source_function_body is None:
            raise ValueError("Could not extract function body.")
        source_function_body = textwrap.dedent(source_function_body)
        return source_function_body

@agoscinski agoscinski force-pushed the refactor-function-parsing branch from 6b16fdc to 5eff5a3 Compare December 11, 2024 11:11
With `ast` we have to use less regex tricks and can rely that the
function is properly parsed.

Docstring is now also considering a linebreak at the beginning.

Annotations of input arguments are now supported.

Default arguments are now supported. We even support expressions as
default arguments e.g lambda functions.

Arbitrary keyword arguments now supported.
@agoscinski agoscinski force-pushed the refactor-function-parsing branch from 5eff5a3 to c38e445 Compare December 11, 2024 11:28
@agoscinski agoscinski marked this pull request as ready for review December 11, 2024 11:28
@agoscinski
Copy link
Collaborator Author

agoscinski commented Dec 11, 2024

We still fix the formatting for parsing the parameters but it this avoids including regex expressions that are harder to maintain.

@agoscinski agoscinski merged commit 0f65564 into main Dec 11, 2024
3 checks passed
@agoscinski agoscinski deleted the refactor-function-parsing branch December 11, 2024 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant