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

pylint complains assertRaisesRegex is deprecated #1946

Closed
smoser opened this issue Mar 16, 2018 · 3 comments
Closed

pylint complains assertRaisesRegex is deprecated #1946

smoser opened this issue Mar 16, 2018 · 3 comments
Labels
Astroid Related to astroid Bug 🪲

Comments

@smoser
Copy link

smoser commented Mar 16, 2018

Steps to reproduce

We have unit tests that we need to work in python 2.6 all the way to 3.6.

Older unittest2.TestCase (as seen in CentOS 6) do not have an
assertRaisesRegex method. They only have the now-deprecated
assertRaisesRegexp.

We need our unit tests to work there and on newer python (3.6),
so we just fill the method in as seen in the test case.

This causes pylint to complain that assertRaisesRegex is deprecated.

One thing to note is that we can work around this by using getattr:

  if not hasattr(TestCase, 'assertRaisesRegex'):
      TestCase.assertRaisesRegex = getattr(TestCase, 'assertRaisesRegexp')
  1. Create mytest.py

     $ cat mytest.py
     from unittest2 import TestCase
    
     if not hasattr(TestCase, 'assertRaisesRegex'):
         TestCase.assertRaisesRegex = TestCase.assertRaisesRegexp
    
     class TestMe(TestCase):
         """Test Regex"""
    
         def test_my_regex(self):
             """Use assertRaisesRegex."""
             value = "9a"
             with self.assertRaisesRegex(ValueError, r"invalid.*%s" % value):
                 int(value)
    
  2. Run pylint on file

Current behavior

  $ python3 -m pylint --rcfile=/dev/null mytest.py
  Using config file /dev/null
  ************* Module mytest
  W: 25,13: Using deprecated method assertRaisesRegex() (deprecated-method)

Note that it actually complains on the use of assertRaisesRegex (not assertRaisesRegexp)
and the warning comes not from the hackery but the use of assertRaisesRegex.

Expected behavior

No errors on assertRaisesRegex are expected.

pylint --version output

  $ python3 -m pylint --rcfile=/dev/null --version
  Using config file /dev/null
  __main__.py 1.8.2, 
  astroid 1.6.1
  Python 3.6.4+ (default, Feb 12 2018, 08:25:03) 
  [GCC 7.3.0]
smoser added a commit to canonical/cloud-init that referenced this issue Mar 16, 2018
Older unittest2.TestCase (as seen in CentOS 6) do not have an
assertRaisesRegex method. They only have the now-deprecated
assertRaisesRegexp.

We need our unit tests to work there and on newer python (3.6).
Simply making assertRaisesRegex = assertRaisesRegexp makes pylint
complain as described in pylint-dev/pylint#1946 .

What was here before this commit was actually broken.  This commit
makes assertRaisesRegex functional in CentOS 6 and works around
the invalid Deprecated warning from pylint.

To prove this, we use assertRaisesRegex in a unit test which will
be exectued in py27, py3 and py26.
@brycepg
Copy link
Contributor

brycepg commented Mar 17, 2018

For some reason the inference of the attribute assertRaisesRegex is inferring to [<BoundMethod assertRaisesRegex of unittest2.case.TestCase at 0x131180310620816, <BoundMethod deprecated_func of unittest2.case.TestCase._deprecate at 0x131180310652744, <BoundMethod deprecated_func of unittest.case.TestCase._deprecate at 0x131180310653584, <BoundMethod assertRaisesRegex of unittest.case.TestCase at 0x131180310651064]
Which contains the a "deprecate" decorator that pylint makes note of. Some debugging needs to be done in astroid as to why the inference system is doing this.

@AWhetter AWhetter added the Astroid Related to astroid label May 10, 2018
@wulfgarpro
Copy link

+1

@PCManticore
Copy link
Contributor

This should be fixed in the master branch now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid Bug 🪲
Projects
None yet
Development

No branches or pull requests

5 participants