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

Passing pre_save_hook as importstring #753

Closed
dleen opened this issue Mar 24, 2022 · 2 comments · Fixed by #754
Closed

Passing pre_save_hook as importstring #753

dleen opened this issue Mar 24, 2022 · 2 comments · Fixed by #754
Labels

Comments

@dleen
Copy link
Contributor

dleen commented Mar 24, 2022

Description

Passing a pre_save_hook as an importstring e.g.

--FileContentsManager.pre_save_hook='mymodule.foo_function`

fails with the following error when launching the Jupyter server:

Troubleshoot Output
    @validate("pre_save_hook")
    def _validate_pre_save_hook(self, proposal):
        value = proposal["value"]
        if isinstance(value, str):
            value = import_item(self.pre_save_hook)
        if not callable(value):
            raise TraitError("pre_save_hook must be callable")
        if self.pre_save_hook is not None:
            warnings.warn(
>               f"Overriding existing pre_save_hook ({self.pre_save_hook.__name__}) with a new one ({value.__name__}).",
                stacklevel=2,
            )
E           AttributeError: 'str' object has no attribute '__name__'

If you edit jupyter_server_config.py to import the function, then setting the value is fine.

Reproduce

Pass pre_save_hook or post_save_hook as a importstring.

Expected behavior

Passing as a string or function works equally well.

Discussion

The problem seems to be because the code assumes that self.pre_save_hook is always a function and has the __name__ dunder.

@dleen dleen added the bug label Mar 24, 2022
@welcome
Copy link

welcome bot commented Mar 24, 2022

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
welcome
You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! 👋

Welcome to the Jupyter community! 🎉

dleen added a commit to dleen/jupyter_server that referenced this issue Mar 24, 2022
Try access the __name__ attribute on the function form of the hook, and
if it doesn't exist then use the string as the name.

Closes jupyter-server#753
@davidbrochart
Copy link
Contributor

Thanks for opening an issue @dleen, I commented on the PR.

dleen added a commit to dleen/jupyter_server that referenced this issue Mar 24, 2022
Only treat a hook as a replacement if the existing hook is callable. Otherwise
treat it as a new hook.

Closes jupyter-server#753
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants