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

Option to reset/clear gin register #115

Open
Conchylicultor opened this issue Apr 6, 2021 · 4 comments
Open

Option to reset/clear gin register #115

Conchylicultor opened this issue Apr 6, 2021 · 4 comments

Comments

@Conchylicultor
Copy link
Member

Conchylicultor commented Apr 6, 2021

Currently, if a @gin.configurable is imported twice (e.g. through importlib.reload()), it raises an error.

This can be fixed by gin.enter_interactive_mode(), but interactive mode does not clear the previous scopes, which is potentially error prone as all importlib.reload() might interfere with one another. This makes it difficult to keep track of the current registered configurable state.

I would like to be able to clear to reset gin to it's initial state, so I am confident my colab will behave similarly to python script, without having to restart the colab runtime.

@Conchylicultor Conchylicultor changed the title Option to reset gin to it's initial state Option to reset/clear gin register Apr 6, 2021
@sguada
Copy link
Collaborator

sguada commented May 10, 2021

You can clear gin registrations with gin.clear_config() and use clear_config(clear_constants=True) to also clear all gin.constants

@Conchylicultor
Copy link
Member Author

Yes, but this does not solve the issue gin.clear_config() reset the parsed config, but not the registered @gin.configurable

@gin.configurable
class A:
  pass

gin.clear_config(clear_constants=True)

@gin.configurable  # Error: A already registered
class A:
  pass

I guess I'm asking for gin.clear_config(clear_registered=True)

The use-case is colab use:

gin.clear_config(clear_registered=True)
with adhoc_import:
  import my_project
  reload_module(my_project)

The second time this gets executed, this will raise an error. As explained above, gin.enter_interactive_mode() feels error-prone

@Conchylicultor
Copy link
Member Author

Conchylicultor commented May 17, 2021

Another example were this appear is in unittest:

@pytest.mark.parametrize('default', [None, 123])
def test_default_factory_explicit(default):
  gin.clear_config()

  @gin.configurable
  @dataclasses.dataclass
  class Config:
    x: Any = default

Despite gin.clear_config(), the Config class is created twice, thus raising a:

ValueError: A configurable matching 'Config' already exists.

To allow re-registration of configurables in an interactive environment, use:

gin.enter_interactive_mode()

Using gin.enter_interactive_mode() inside a test feel very wrong. It would be much better to have a gin.clear_config(clear_registered=True).

@sguada
Copy link
Collaborator

sguada commented May 18, 2021

This 334547d should fix this issue, and allow to register the same configurable twice, as far is the same class with the same name.

Can you try to pull the latest version and try again?

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

2 participants