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

Fix Python 3 warnings #783

Merged
merged 2 commits into from
Sep 24, 2020
Merged

Conversation

seando-adsk
Copy link
Collaborator

Fix Python 3 warnings (also works in Python 2).
See https://docs.python.org/3.3/library/unittest.html#deprecated-aliases

@seando-adsk seando-adsk added pxr Related to Pixar plugin unit test Related to unit tests (both python or c++) labels Sep 18, 2020
Copy link
Contributor

@mattyjams mattyjams left a comment

Choose a reason for hiding this comment

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

I haven't tried pulling this change into our environment, but I'm not sure that the new function names are available in our current version of Python (2.7.16):

> python
Python 2.7.16 (default, Mar  9 2020, 17:47:06) 
[GCC 6.3.1 20170216 (Red Hat 6.3.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from unittest import TestCase
>>> 
>>> TestCase.assertRegex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'TestCase' has no attribute 'assertRegex'
>>> TestCase.assertRegexpMatches
<unbound method TestCase.assertRegexpMatches>
>>> 
>>> TestCase.assertRaisesRegex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'TestCase' has no attribute 'assertRaisesRegex'
>>> TestCase.assertRaisesRegexp
<unbound method TestCase.assertRaisesRegexp>

I do see the new names and the deprecated old names in our version of Python 3 though (3.6.8):

python3
Python 3.6.8 (default, Sep 26 2019, 11:57:09) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from unittest import TestCase
>>> 
>>> TestCase.assertRegex
<function TestCase.assertRegex at 0x7fca4c5f0510>
>>> TestCase.assertRegexpMatches
<function TestCase._deprecate.<locals>.deprecated_func at 0x7fca4c5f0ae8>
>>> 
>>> TestCase.assertRaisesRegex
<function TestCase.assertRaisesRegex at 0x7fca4c5f0400>
>>> TestCase.assertRaisesRegexp
<function TestCase._deprecate.<locals>.deprecated_func at 0x7fca4c5f0a60>

Is it possible your Python 2 test run wasn't actually running against Python 2?

Maybe we could do a sys.version check in setUpClass() and monkey patch in the old functions with the new names if we're running against Python 2?

@seando-adsk
Copy link
Collaborator Author

@mattyjams You're right sorry about that. I see the same thing with my Python2 version. I don't know what I did - maybe I only ran the tests in python3.

These are changes I was asked to make by another developer on the Maya team. I was told they were python2 compatible, but I'm questioning that now. I'll get back to you.

@seando-adsk
Copy link
Collaborator Author

@mattyjams It turns out these functions have only been deprecated since 3.2 (https://docs.python.org/3.3/library/unittest.html#deprecated-aliases):
Deprecated since version 3.2: assertRegexpMatches and assertRaisesRegexp have been renamed to assertRegex() and assertRaisesRegex()

Do you still want me to fix this using your monkey patch suggestion or do you have another suggestion?

@mattyjams
Copy link
Contributor

@mattyjams It turns out these functions have only been deprecated since 3.2 (https://docs.python.org/3.3/library/unittest.html#deprecated-aliases):
Deprecated since version 3.2: assertRegexpMatches and assertRaisesRegexp have been renamed to assertRegex() and assertRaisesRegex()

Do you still want me to fix this using your monkey patch suggestion or do you have another suggestion?

@seando-adsk: That was just the first idea that came to mind, but I'm open to another solution if you have one.

To make it a little more concrete, this is more or less what I was thinking:

    @classmethod
    def setUpClass(cls):
        ...
        if sys.version_info.major < 3 or sys.version_info.minor < 2:
            cls.assertRegex = cls.assertRegexpMatches
            cls.assertRaisesRegex = cls.assertRaisesRegexp
        ...

@seando-adsk
Copy link
Collaborator Author

@mattyjams I used your suggestion and ran the tests in both python2 (2.7.11) and python3 (3.7.7). They both passed now. Thanks for your help.

Copy link
Contributor

@mattyjams mattyjams left a comment

Choose a reason for hiding this comment

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

Cool, thanks @seando-adsk! Glad that worked out. Looks good to me!

@seando-adsk seando-adsk added the ready-for-merge Development process is finished, PR is ready for merge label Sep 24, 2020
@kxl-adsk kxl-adsk merged commit 99a0238 into dev Sep 24, 2020
@kxl-adsk kxl-adsk deleted the donnels/fix_python3_deprecated_functions branch September 24, 2020 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pxr Related to Pixar plugin ready-for-merge Development process is finished, PR is ready for merge unit test Related to unit tests (both python or c++)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants