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

Invalid DSL when using assignment with function call #544

Open
BrentBlanckaert opened this issue Oct 2, 2024 · 2 comments
Open

Invalid DSL when using assignment with function call #544

BrentBlanckaert opened this issue Oct 2, 2024 · 2 comments

Comments

@BrentBlanckaert
Copy link
Collaborator

When trying to make an exercise with a statement that has an assignment using a function call (as in the example below), You will get an invalid DSL error with no extra info. I was able to track a part of the problem down to here. In this case the type would FunctionType.FUNCTION.

- tab: sortWords
  contexts:
    - testcases:
      - statement: 'result = countWords(["Gobber", "Hiccup", "Astrid", "Stoick", "Toothless"], "httyd.txt")'
      - expression: 'sortWords(result)'
        return: ["Hiccup", "Toothless", "Stoick", "Astrid", "Gobber"]
@pdawyndt
Copy link
Contributor

pdawyndt commented Oct 3, 2024

Since TESTed has to infer the type of the variable, we may need to add type information about the return value of the function. No idea how to specify that information in the DSL, so I'll ask @niknetniko.

Could you try doing it this way, so with an explicit int type conversion (guessing that the function should return a dict):

- tab: sortWords
  contexts:
    - testcases:
      - statement: 'result = dict(countWords(["Gobber", "Hiccup", "Astrid", "Stoick", "Toothless"], "httyd.txt"))'
      - expression: 'sortWords(result)'
        return: ["Hiccup", "Toothless", "Stoick", "Astrid", "Gobber"]

@niknetniko
Copy link
Member

niknetniko commented Oct 3, 2024

One way is to use Python type annotations:

- tab: sortWords
  contexts:
    - testcases:
    - statement: 'result: map = count_words(["Gobber", "Hiccup", "Astrid", "Stoick", "Toothless"], "httyd.txt")'
                      # ^^^^^ add this
    - expression: 'sort_words(result)'
  return: ["Hiccup", "Toothless", "Stoick", "Astrid", "Gobber"]

Also note I have used snake case for the function names; otherwise TESTed should print warnings.

When I run the original test suite locally, I do get an error which says what is wrong, so not sure why did not get that:

Traceback (most recent call last):
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/ast_translator.py", line 354, in parse_string
    return _translate_to_ast(tree, is_return)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/ast_translator.py", line 333, in _translate_to_ast
    return _convert_statement(statement_or_expression)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/ast_translator.py", line 312, in _convert_statement
    return _convert_assignment(node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/ast_translator.py", line 147, in _convert_assignment
    raise InvalidDslError(
tested.dsl.ast_translator.InvalidDslError: Could not deduce the type of variable result: add a type annotation.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/niko/Ontwikkeling/universal-judge/tested/manual.py", line 57, in <module>
    run(config, sys.stdout)
  File "/home/niko/Ontwikkeling/universal-judge/tested/main.py", line 33, in run
    suite = parse_dsl(textual_suite)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 761, in parse_dsl
    return _convert_dsl(dsl_object)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 742, in _convert_dsl
    tabs = _convert_dsl_list(tab_list, context, _convert_tab)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 715, in _convert_dsl_list
    objects.append(converter(dsl_object, context))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 682, in _convert_tab
    contexts = _convert_dsl_list(tab["contexts"], context, _convert_context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 715, in _convert_dsl_list
    objects.append(converter(dsl_object, context))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 663, in _convert_context
    testcases = _convert_dsl_list(raw_testcases, dsl_context, _convert_testcase)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 715, in _convert_dsl_list
    objects.append(converter(dsl_object, context))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/translate_parser.py", line 586, in _convert_testcase
    the_input = parse_string(expr_stmt)
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niko/Ontwikkeling/universal-judge/tested/dsl/ast_translator.py", line 356, in parse_string
    raise InvalidDslError("Invalid DSL") from e
tested.dsl.ast_translator.InvalidDslError: Invalid DSL

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

No branches or pull requests

3 participants