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

Inconsistent ParseResults from parse actions that return other types #401

Open
djpohly opened this issue May 23, 2022 · 0 comments
Open

Comments

@djpohly
Copy link
Contributor

djpohly commented May 23, 2022

The explanation of parse action handlers in #309 gives three cases for the return type:

  • None: use the existing results object, potentially modified by the action
  • ParseResults: replaces the existing results object
  • Anything else: replaces the existing results object, wrapped in a ParseResults

In the "anything else" category, many types work as described. Notice in each case that there is a single element in the tokens list, which is identical to results["letters"]:

>>> Empty()("name").set_parse_action(lambda: "123").parse_string("")
ParseResults(['123'], {'name': '123'})
>>> Empty()("name").set_parse_action(lambda: {1,2,3}).parse_string("")
ParseResults([{1, 2, 3}], {'name': {1, 2, 3}})

If the action returns a tuple, the token list is correct, but the names dict is wrong:

>>> Empty()("name").set_parse_action(lambda: (1,2,3)).parse_string("")
ParseResults([(1, 2, 3)], {'name': 1})
  should be: [(1, 2, 3)], {'name': (1, 2, 3)}

If the action returns a list, both the tokens list and names dict are wrong:

>>> Empty()("name").set_parse_action(lambda: [1,2,3]).parse_string("")
ParseResults([1, 2, 3], {'name': 1})
  should be: [[1, 2, 3]], {'name': [1, 2, 3]}

You can even get some very unexpected behavior from other types:

>>> Empty()("name").set_parse_action(lambda: collections.defaultdict(str)).parse_string("")
ParseResults([defaultdict(<class 'str'>, {0: ''})], {'name': ''})
  should be: [defaultdict(<class 'str'>, {})], {'name': defaultdict(<class 'str'>, {})}
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

1 participant