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

Allow config scopes with type annotations. #868

Merged
merged 7 commits into from
Sep 9, 2022

Conversation

vnmabus
Copy link
Contributor

@vnmabus vnmabus commented Apr 30, 2022

Allows the usage of type hints in config scopes. Closes #818.

Notes:

  • A new test is added for this behaviour.
  • Instead of fixing the regexp, it uses ast and tokenize modules. With ast we find the first line with code in the function body. As comments are removed in the AST representation, we then generate the tokens of the function until that line is reached, in order to keep the previous lines that only have comments and whitespace.

@thequilo
Copy link
Collaborator

Hi @vnmabus! I seem to have overlooked your PR. I like the idea to use the ast module to parse the config scopes, especially because bugs related to the config scope parsing keep popping up here and there, and we currently have to modify the regexes every time things are added to the language. But the current implementation doesn't work. Are you planning to fix the issues?

@vnmabus
Copy link
Contributor Author

vnmabus commented Jun 27, 2022

Hum, it worked in my PC. I can't see the errors in Azure (maybe because the runs have been removed). Can you relaunch the tests?

@thequilo
Copy link
Collaborator

When I try to run the testcases, I get the error E NameError: name 'generate_tokens' is not defined

@thequilo
Copy link
Collaborator

GitHub doesn't let me re-run the checks. I clicked on the re-run button and it just disappeared without re-running the checks. The easiest is probably to push an empty commit, which should trigger the checks again.

Conflicts:
	.gitignore
	sacred/config/config_scope.py
@vnmabus
Copy link
Contributor Author

vnmabus commented Aug 11, 2022

It seems that it is working only in Python 3.8 and newer because of this: https://docs.python.org/3/whatsnew/3.8.html#tokenize .
I could attempt to manually add a newline to have it working in Python 3.7. However, if we are following https://numpy.org/neps/nep-0029-deprecation_policy.html we should remove support for Python 3.7 anyways.

@thequilo
Copy link
Collaborator

@vnmabus Agreed, if it makes your code significantly simpler, we can remove support for Python 3.7

@vnmabus
Copy link
Contributor Author

vnmabus commented Aug 15, 2022

@vnmabus Agreed, if it makes your code significantly simpler, we can remove support for Python 3.7

I don't think it would be too hard to fix it either. But if you are planning to upgrade the minimum requirements soon I won't need to do it.

@thequilo
Copy link
Collaborator

thequilo commented Sep 5, 2022

I don't plan on upgrading the minimum requirements explicitly in a separate PR. The recent updates always happened as a byproduct of other changes, like this one.

@vnmabus
Copy link
Contributor Author

vnmabus commented Sep 5, 2022

Well, probably it is cleaner to modify the files related with the minimum version (setup.py/pyproject.toml, tests, etc) in a separate PR. What do you think?

@thequilo
Copy link
Collaborator

thequilo commented Sep 5, 2022

Well, you are right. Are you going to open one?

@vnmabus
Copy link
Contributor Author

vnmabus commented Sep 5, 2022

Well, you are right. Are you going to open one?

I opened #887.

@thequilo
Copy link
Collaborator

thequilo commented Sep 6, 2022

I found one issue:

def fn(): a = 2 # valid python code
get_function_body(fn)

gives

('def fn(): a = 2 # valid python code\n', 1)

The old variant threw an exception here. This now results in:

from sacred.config.config_scope import ConfigScope

@ConfigScope
def fn(): a = 2

fn()
{'fn': <function __main__.fn()>}

It should either work as expected or fail with a good error message

@vnmabus
Copy link
Contributor Author

vnmabus commented Sep 6, 2022

It should either work as expected or fail with a good error message

Good catch!! I decided to make it work. I had to change the expected results of two tests, as now the comment after the function signature is also preserved (and the newlines too).

@thequilo
Copy link
Collaborator

thequilo commented Sep 8, 2022

I didn't find other issues, so I think it's ready to merge!

@thequilo thequilo merged commit 940ac81 into IDSIA:master Sep 9, 2022
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.

Sacred dislikes return type hint in config
3 participants