Skip to content

Conversation

@thechargedneutron
Copy link
Contributor

Reference Issue

Fixes #9454

What does this implement/fix? Explain your changes.

I added a new directory named modifiedunittest which backports msg.

Any other comments?

  1. I have not removed the extra codes of the pasted module, will be done upon validation of my task.
  2. This should work with Python 2.6 as well. If it fails, necessary improvement will be done.
    Thanks

@thechargedneutron
Copy link
Contributor Author

After having a look at the error log, which states ImportError, what all changes do I need to make in order to recognise the package I used.
One thing I observed was to add config.add_subpackage('modifiedunittest') in sklearn.utils.setup.py . Any other location where I must add something?

return isinstance(expected, type) and issubclass(expected, basetype)


class _AssertRaisesContext(_AssertRaisesBaseContext):
Copy link
Member

Choose a reason for hiding this comment

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

I think we basically just want this (perhaps with some changes for compatibility), and nothing else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll remove all the unnecessary packages once I am sure the whole works. I am getting ImportError on writing import sklearn.utils.modifiedunittest . Please help me in importing it correctly. I'll remove the extra code here.

@thechargedneutron
Copy link
Contributor Author

So, now I realise that we don't need to import the modules and all. We can just copy paste the codes in testing.py itself since you pointed out that there's only a few things to be added. Is this a good idea or importing from other file is better.

@thechargedneutron
Copy link
Contributor Author

I guess now we have the minimum amount of code in sklearn.utils.testing.py. The extra module added is removed. Hope this works fine.
Sorry for so many commits.

@jnothman
Copy link
Member

jnothman commented Aug 13, 2017 via email

@thechargedneutron
Copy link
Contributor Author

@jnothman Does it work or there's something more to be done here?
The codecov patch test fails. But I don't think we need the test function of the functions added by me. I need your opinion in this.

"assert_approx_equal", "SkipTest"]


def _is_subtype(expected, basetype):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it would be better to put this in a separate file like _unittest_backport.py or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll make a new file for this.

@amueller
Copy link
Member

I think we should have a small test for this.

@thechargedneutron
Copy link
Contributor Author

Is the backport implementation working? I wanted to know this. :)
I will add tests as soon as possible.

@thechargedneutron
Copy link
Contributor Author

I think I need some help in making the test functions.
In python's source code, I found the following code:
Is this the appropriate test case for _formatMessage function which is there in class TestCase_new ?
Kindly help as I am not sure about the test functions.

class TestLongMessage(unittest.TestCase):
    def setUp(self):
        class TestableTestFalse(unittest.TestCase):
            longMessage = False
            failureException = self.failureException

            def testTest(self):
                pass

        class TestableTestTrue(unittest.TestCase):
            longMessage = True
            failureException = self.failureException

            def testTest(self):
                pass

        self.testableTrue = TestableTestTrue('testTest')
        self.testableFalse = TestableTestFalse('testTest')

    def test_formatMsg(self):
        self.assertEqual(self.testableFalse._formatMessage(None, "foo"), "foo")
        self.assertEqual(self.testableFalse._formatMessage("foo", "bar"), "foo")

        self.assertEqual(self.testableTrue._formatMessage(None, "foo"), "foo")
        self.assertEqual(self.testableTrue._formatMessage("foo", "bar"), "bar : foo")

        # This blows up if _formatMessage uses string concatenation
        self.testableTrue._formatMessage(object(), 'foo')

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Thanks.

I don't think TestLongMessage is testing assertRaises.

@@ -0,0 +1,173 @@
import re
Copy link
Member

Choose a reason for hiding this comment

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

This should have attribution to where it is backported from (and the copyright notice)

return True


class TestCase_new(object):
Copy link
Member

Choose a reason for hiding this comment

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

If you inherit from unittest.TestCase you can use this alone as the dummy, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something like this? class TestCase_new(unittest.TestCase):. And then we can remove the redundant functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This somehow gives error. It says ValueError: no such test method in <class 'TestCase_new'>: runTest . I looked for the error in StackOverflow and found this: https://stackoverflow.com/questions/2090479/valueerror-no-such-test-method-in-class-myapp-tests-sessiontestcase-runtes but could not make use of it. Can you suggest me how to achieve this?

@jnothman
Copy link
Member

to test you want something like:

def test_assert_raises_msg():
    with assert_raises_regex(AssertionError, 'Hello world'):
        with assert_raises(ValueError, msg='Hello world'):
            # Nothing is raised. Assertion should fail.
            pass

@thechargedneutron
Copy link
Contributor Author

@jnothman Okay, so I was under the impression that I have to add tests for the functions in _unittest_backport.py and not for assert_raises . If only I add tests for assert_raises_with_msg, the codecov/patch will still fail, isn't it?

@jnothman
Copy link
Member

jnothman commented Aug 16, 2017 via email

return True


class TestCase_new(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

you could just call the new class TestCase. Certainly, it shouldn't have an underscore in a class name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll do that. somehow I am not able to use a single dummy variable, initializing unittest.Test case('init') inside new TestCase gives an error saying the 'init' function take 2 arguments, 4 given. Please look into it

@thechargedneutron
Copy link
Contributor Author

So, I have made all the changes suggested so far, added test_assert_raises_with_msg , added copyright in _unittest_backport.py and also imported unittest module in my backported class.
Kindly review the changes and suggest if anything else is required.

@amueller amueller changed the title [WIP] Backports msg in assert_raises and assert_raises_regex [MRG] Backports msg in assert_raises and assert_raises_regex Aug 16, 2017
@amueller
Copy link
Member

LGTM

@jnothman
Copy link
Member

LGTM, thanks!

@jnothman jnothman merged commit 2b9405f into scikit-learn:master Aug 16, 2017
@thechargedneutron
Copy link
Contributor Author

Thanks a lot, this was a very good learning experience, will continue to contribute :)

@jnothman
Copy link
Member

jnothman commented Aug 17, 2017 via email

paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
…learn#9536)

* Added modifiedunittest

* Backports msg in assertRaises and assertRaisesRegexp

* Import statement corrected

* Corrected import statement

* Added module name in utils.setup.py

* Removed Extra modules

* Reordered class

* _is_subtype added

* Missing import added

* _formatMessage added

* missing variables added

* Remove PEP8 failures

* Removed safe_repr

* _unittest_backport.py added

* Import statement corrected

* Added copyright

* Syntax Error removed

* Error removed

* runTest function added

* Tests added

* __init__ added

* Import added
AishwaryaRK pushed a commit to AishwaryaRK/scikit-learn that referenced this pull request Aug 29, 2017
…learn#9536)

* Added modifiedunittest

* Backports msg in assertRaises and assertRaisesRegexp

* Import statement corrected

* Corrected import statement

* Added module name in utils.setup.py

* Removed Extra modules

* Reordered class

* _is_subtype added

* Missing import added

* _formatMessage added

* missing variables added

* Remove PEP8 failures

* Removed safe_repr

* _unittest_backport.py added

* Import statement corrected

* Added copyright

* Syntax Error removed

* Error removed

* runTest function added

* Tests added

* __init__ added

* Import added
@thechargedneutron thechargedneutron deleted the Secondary branch October 17, 2017 02:28
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
…learn#9536)

* Added modifiedunittest

* Backports msg in assertRaises and assertRaisesRegexp

* Import statement corrected

* Corrected import statement

* Added module name in utils.setup.py

* Removed Extra modules

* Reordered class

* _is_subtype added

* Missing import added

* _formatMessage added

* missing variables added

* Remove PEP8 failures

* Removed safe_repr

* _unittest_backport.py added

* Import statement corrected

* Added copyright

* Syntax Error removed

* Error removed

* runTest function added

* Tests added

* __init__ added

* Import added
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
…learn#9536)

* Added modifiedunittest

* Backports msg in assertRaises and assertRaisesRegexp

* Import statement corrected

* Corrected import statement

* Added module name in utils.setup.py

* Removed Extra modules

* Reordered class

* _is_subtype added

* Missing import added

* _formatMessage added

* missing variables added

* Remove PEP8 failures

* Removed safe_repr

* _unittest_backport.py added

* Import statement corrected

* Added copyright

* Syntax Error removed

* Error removed

* runTest function added

* Tests added

* __init__ added

* Import added
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.

backport msg in assert_raises and assert_raises_regex

3 participants