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

Added the test for the import error. #547

Merged
merged 7 commits into from
Aug 1, 2019

Conversation

gabrieldemarmiesse
Copy link
Collaborator

@gabrieldemarmiesse gabrieldemarmiesse commented Jul 31, 2019

We have an issue with error messages for optional dependencies. For exemple, trying to use the sql observer without sql installed throws the following error:

tests/test_observers/test_sql_observer_not_installed.py:11 (test_importerror_sql)
ex = <sacred.experiment.Experiment object at 0x7f5495d9f4a8>

    @pytest.mark.skipif(has_sqlalchemy, reason='We are testing the import error.')
    def test_importerror_sql(ex):
        with pytest.raises(ImportError):
>           ex.observers.append(SqlObserver.create('some_uri'))

test_sql_observer_not_installed.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'sacred.observers.sql.SqlObserver'>, url = 'some_uri', echo = False
priority = 40

    @classmethod
    def create(cls, url, echo=False, priority=DEFAULT_SQL_PRIORITY):
>       engine = sa.create_engine(url, echo=echo)
E       NameError: name 'sa' is not defined

../../sacred/observers/sql.py:23: NameError

Not very helpful. See here the travis log for the test I added.

This PR makes sure that we have a proper error message.

I had to enable the test for the observers in the "setup" build. Otherwise the added test will never run.

With the new setup, the error message is:

Traceback (most recent call last):
  File "/mnt/c/Users/gdemarmi/Desktop/projects/sacred/trying_stuff.py", line 9, in <module>
    ex.observers.append(SqlObserver.create('some_uri'))
  File "/mnt/c/Users/gdemarmi/Desktop/projects/sacred/sacred/observers/sql.py", line 19, in create
    from sqlalchemy.orm import sessionmaker, scoped_session
ModuleNotFoundError: No module named 'sqlalchemy'

The majority of the change is just a copy past between sql.py and sql_bases.py.

@coveralls
Copy link

coveralls commented Jul 31, 2019

Coverage Status

Coverage decreased (-0.2%) to 85.305% when pulling a0e801f on gabrieldemarmiesse:better_import_message into 7054628 on IDSIA:master.

@@ -20,6 +16,8 @@
class SqlObserver(RunObserver):
@classmethod
def create(cls, url, echo=False, priority=DEFAULT_SQL_PRIORITY):
from sqlalchemy.orm import sessionmaker, scoped_session
from .sql_bases import sa
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any reason you are import sqlalchemy here from sql_bases instead of directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, there is a very good reason. It's because I wasn't attentive, haha.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Haha ok. PR looks good, I will merge once the tests pass after the change.

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

3 participants