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

Support config functions with trailing comma parameters #764

Merged
merged 1 commit into from Sep 25, 2020

Conversation

AdamGleave
Copy link
Contributor

For config functions with large number of arguments, it is often necessary to format the arguments over multilines. In this case, it is common (and enforced by some code formatters, like black) that the last argument has a trailing comma. Minimal example:

#!/usr/bin/env python3

import sacred

ex = sacred.Experiment("test")

@ex.config
def default():
    x = 1
    y = 1

@ex.config
def f(
    x,
    y,
):
    z = x + y

@ex.automain
def main():
    pass

Running this in master sacred produces:

$ python3 test.py print_config
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    @ex.config
  File "/home/adam/dev/sacred/sacred/ingredient.py", line 162, in config
    self.configurations.append(ConfigScope(function))
  File "/home/adam/dev/sacred/sacred/config/config_scope.py", line 25, in __init__
    self._body_code = get_function_body_code(func)
  File "/home/adam/dev/sacred/sacred/config/config_scope.py", line 146, in get_function_body_code
    func_body, line_offset = get_function_body(func)
  File "/home/adam/dev/sacred/sacred/config/config_scope.py", line 104, in get_function_body
    assert defs
AssertionError

With the changes in the PR, this produces expected behavior:

python3 test.py print_config
INFO - test - Running command 'print_config'
INFO - test - Started
Configuration (modified, added, typechanged, doc):
  seed = 505103377                   # the random seed for this experiment
  x = 1
  y = 1
  z = 2
INFO - test - Completed after 0:00:00

@thequilo
Copy link
Collaborator

I assume that the failing checks are not caused by your changes

@thequilo thequilo merged commit c13ab88 into IDSIA:master Sep 25, 2020
@AdamGleave AdamGleave deleted the trailing-comma-configs branch October 8, 2020 15:22
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.

None yet

2 participants