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 instantiation from class methods #498

Merged

Conversation

dhpollack
Copy link
Contributor

  • instantiate an object from a class method of that object
  • add tests for this
  • simplify get_static_method function

Signed-off-by: David Pollack d.pollack@solvemate.com

Motivation

I wanted to use hydra with the huggingface transfromers library and they extensively use a class method named from_pretrained.

Currently one could do this by using the get_static_method and then manually converting the params key and passing them to the gotten method. However that's not very straight forward or intuitive and this doesn't add much logic to the utility function.

Have you read the Contributing Guidelines on pull requests?

Yes - passed tests in test_utils.py and flake8

Test Plan

I added a test that tests the basic case.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 18, 2020
Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the PR.

See my comments.

hydra/utils.py Outdated Show resolved Hide resolved
tests/test_utils.py Outdated Show resolved Hide resolved
@dhpollack dhpollack force-pushed the dhp/instantiate_from_class_method branch from bd36485 to 1439acc Compare March 21, 2020 13:53
Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting to look good.

hydra/utils.py Outdated Show resolved Hide resolved
hydra/utils.py Outdated Show resolved Hide resolved
tests/test_utils.py Show resolved Hide resolved
website/docs/patterns/objects.md Outdated Show resolved Hide resolved
@omry
Copy link
Collaborator

omry commented Mar 21, 2020

As for the failing CI:
see the developer guide for info on how to run the tests and the lint locally.

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Mar 23, 2020

This pull request introduces 1 alert when merging 52e99cc into 67a525b - view on LGTM.com

new alerts:

  • 1 for Except block handles 'BaseException'

hydra/utils.py Outdated Show resolved Hide resolved
hydra/utils.py Outdated Show resolved Hide resolved
hydra/utils.py Outdated Show resolved Hide resolved
hydra/utils.py Outdated Show resolved Hide resolved
website/docs/patterns/objects.md Outdated Show resolved Hide resolved
@omry
Copy link
Collaborator

omry commented Mar 26, 2020

ping

@dhpollack
Copy link
Contributor Author

The test failues look like they are due to the upgrade in omegaconf. Do you prefer a git rebase or a git merge on the master? I tested both on branches locally. I would assume rebasing, but I figured I'd ask.

@omry
Copy link
Collaborator

omry commented Mar 26, 2020

I prefer rebase.

David Pollack and others added 6 commits March 26, 2020 21:01
* instantiate an object from a class method of that object
* add tests for this
* simplify get_static_method function

Signed-off-by: David Pollack <d.pollack@solvemate.com>
* works with class, classmethod, or staticmethod
* still requires the user to specify the module
* simplifies importing of all types into a single method

Signed-off-by: David Pollack <david@da3.net>
Signed-off-by: David Pollack <david@da3.net>
this is a reimplementation of the pydoc.locate function.  That function
would attempt to load a module from beginning to end and if any link in
the chain did not load properly then it would fail.  The reimplemented
function searches for the module to load from the end of the path to the
beginning.

Additionally, this reimplementation will raise exceptions instead of
returning None if the path cannot be located.

Signed-off-by: David Pollack <david@da3.net>
@dhpollack dhpollack force-pushed the dhp/instantiate_from_class_method branch from cbe1a65 to ae26554 Compare March 26, 2020 20:01
Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. A few more comments about the docs and the tests.

hydra/utils.py Outdated Show resolved Hide resolved
website/docs/patterns/objects.md Outdated Show resolved Hide resolved
tests/test_utils.py Outdated Show resolved Hide resolved
@dhpollack dhpollack force-pushed the dhp/instantiate_from_class_method branch from ae26554 to 6d57655 Compare March 28, 2020 22:07
Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome. almost there.

hydra/utils.py Outdated Show resolved Hide resolved
website/docs/patterns/objects.md Outdated Show resolved Hide resolved
@dhpollack
Copy link
Contributor Author

Alright, I think that I solved the mypy issues by using _instantiate_class for classes and _call_callable for functions and methods. I also created a bunch of aliases for the old naming scheme and renamed functions as well. I also added a few extra tests for builtins and nested methods.

As for the documentation, let me know what you think. I tried to keep it as compact as possible, but may be doing too many things with the same class.

P.S. the win37 failure looks unrelated to this PR. Looks like the conda servers glitched.

Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to split hydra.utils into hydra.utils for public API and some other internal class with non public API. but this can be a followup diff.

Made one last suggestion.

website/docs/patterns/objects.md Outdated Show resolved Hide resolved
Co-Authored-By: Omry Yadan <omry@fb.com>
@omry
Copy link
Collaborator

omry commented Mar 29, 2020

As for the docs, they look good here but I would like to see them in the website.
Did you start the website locally to test that they look okay?

@dhpollack
Copy link
Contributor Author

I didn't but I'll check it tomorrow

Split the current objects docs into two sections.  Moved the call
section to the top and the db example to the bottom.  Renamed the header
to reflect the ability to call functions and methods.
@omry
Copy link
Collaborator

omry commented Mar 29, 2020

Awesome. edited the doc a bit and added the issue #505 for API cleanup before 1.0.

@omry
Copy link
Collaborator

omry commented Mar 30, 2020

Ohh, forgot.
Can you add a news fragment file?
see the developer guide for details.

@omry omry merged commit a5bd89a into facebookresearch:master Mar 31, 2020
@omry
Copy link
Collaborator

omry commented Mar 31, 2020

Awesome, thanks David!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants