Skip to content

Commit 2e99068

Browse files
committed
SG-31340 Deprecation of Python 2 (#295)
* remove references to py2 and skip tests * small fix * revert change * revert change * revert change * revert change * remove badge * use assertRaisesRegex
1 parent b54c286 commit 2e99068

File tree

8 files changed

+11
-119
lines changed

8 files changed

+11
-119
lines changed

README.md

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![VFX Platform](https://img.shields.io/badge/vfxplatform-2020-blue.svg)](http://www.vfxplatform.com/)
2-
[![Python 2.7 3.7](https://img.shields.io/badge/python-2.7%20%7C%203.7-blue.svg)](https://www.python.org/)
2+
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/)
33
[![Reference Documentation](http://img.shields.io/badge/doc-reference-blue.svg)](http://developer.shotgridsoftware.com/python-api)
44
[![Build Status](https://dev.azure.com/shotgun-ecosystem/Python%20API/_apis/build/status/shotgunsoftware.python-api?branchName=master)](https://dev.azure.com/shotgun-ecosystem/Python%20API/_build/latest?definitionId=108&branchName=master)
55
[![Coverage Status](https://coveralls.io/repos/github/shotgunsoftware/python-api/badge.svg?branch=master)](https://coveralls.io/github/shotgunsoftware/python-api?branch=master)
@@ -37,63 +37,6 @@ The API comes with a copy of the `httplib2` inside the `shotgun_api3/lib` folder
3737

3838
where `vX.Y.Z` is a release found on `httplib2`'s [release page](https://github.com/httplib2/httplib2/releases).
3939

40-
## Maintaining Python 2 and 3 compatibility
41-
42-
python-api should remain compatible with both Python 2, and 3. To make this easier, we use [six](https://six.readthedocs.io/). When adding code that works with types that have changed between Python 2 and 3, notably strings and files, it's advisable to use the `six` types for casting and comparisons. Be sure to follow Python 2 and 3 compatible conventions in code, especially when raising or capturing exceptions and printing. While we don't use `future`, [this page](https://python-future.org/compatible_idioms.html) contains a fairly comprehensive list of Python 2/3 compatibility sticking points to look out for.
43-
44-
Additionally, the [python-modernize](https://python-modernize.readthedocs.io/en/latest/) tool can be helpful when updating Python 2 code for Python 3 compatibility.
45-
46-
### Examples:
47-
48-
#### Comparisons against changed types:
49-
50-
Python 2:
51-
52-
```
53-
if isinstance(my_variable, str):
54-
```
55-
56-
Python 2/3:
57-
58-
```
59-
if isinstance(my_variable, six.string_types):
60-
```
61-
62-
#### Catching exceptions
63-
64-
Python 2:
65-
66-
```
67-
except SomeExceptionType, e:
68-
print "I like to swallow exceptions!"
69-
```
70-
71-
Python 2/3:
72-
73-
```
74-
from __future__ import print_function
75-
except SomeExceptionType as e:
76-
print("I like to swallow exceptions!")
77-
```
78-
79-
#### Print statements
80-
81-
Python 2:
82-
83-
```
84-
print "My spoon is too big!"
85-
```
86-
87-
Python 2/3:
88-
89-
```
90-
from __future__ import print_function
91-
print("My spoon is too big!")
92-
```
93-
94-
95-
Additionally, when testing locally, tests should be run for both python 2 and python 3 to ensure changes won't break cross-compatibility.
96-
9740
## Tests
9841

9942
Integration and unit tests are provided.

azure-pipelines-templates/run-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ jobs:
4444
strategy:
4545
matrix:
4646
# We support these two versions of Python.
47-
Python27:
48-
python.version: '2.7'
4947
Python37:
5048
python.version: '3.7'
5149
Python39:

tests/base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
from shotgun_api3.shotgun import ServerCapabilities
1111
from shotgun_api3.lib import six
1212
from shotgun_api3.lib.six.moves import urllib
13-
14-
if six.PY2:
15-
from shotgun_api3.lib.six.moves.configparser import SafeConfigParser as ConfigParser
16-
else:
17-
from shotgun_api3.lib.six.moves.configparser import ConfigParser
18-
13+
from shotgun_api3.lib.six.moves.configparser import ConfigParser
1914

2015
try:
2116
# Attempt to import skip from unittest. Since this was added in Python 2.7

tests/mock.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@
3838
# may not have inspect
3939
inspect = None
4040

41-
try:
42-
BaseException
43-
except NameError:
44-
# Python 2.4 compatibility
45-
BaseException = Exception
46-
4741
try:
4842
from functools import wraps
4943
except ImportError:

tests/test_api.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,7 @@ def _id_in_result(self, entity_type, filters, expected_id):
12061206
for particular filters.
12071207
"""
12081208
results = self.sg.find(entity_type, filters)
1209-
# can't use 'any' in python 2.4
1210-
for result in results:
1211-
if result['id'] == expected_id:
1212-
return True
1213-
return False
1209+
return any(result['id'] == expected_id for result in results)
12141210

12151211
# TODO test all applicable data types for 'in'
12161212
# 'currency' => [BigDecimal, Float, NilClass],

tests/test_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ def test_parse_records(self):
564564
system = platform.system().lower()
565565
if system == 'darwin':
566566
local_path_field = "local_path_mac"
567-
# python 2.4 returns 'Microsoft'
568567
elif system in ['windows', 'microsoft']:
569568
local_path_field = "local_path_windows"
570569
elif system == 'linux':

tests/test_mockgun.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,6 @@
5353
)
5454

5555

56-
# FIXME: This should probably be refactored into a base class for
57-
# all test bases
58-
class TestBaseWithExceptionTests(unittest.TestCase):
59-
"""
60-
Implements a Python 2.4 compatible assertRaisesRegexp like method. This
61-
was introduced in Python 2.7.
62-
"""
63-
def assertRaisesRegexp(self, exception_type, re_msg, func):
64-
try:
65-
func()
66-
except exception_type as exception:
67-
matches = re.findall(re_msg, str(exception))
68-
if not matches:
69-
self.fail("Expected exception to match '%s', got '%s' instead." % (
70-
re_msg, str(exception)
71-
))
72-
except Exception as ex:
73-
self.fail("Expected exception of type %s, got %s" % (exception_type, type(ex)))
74-
else:
75-
self.fail("Expected %s was not raised." % exception_type)
76-
77-
7856
class TestMockgunModuleInterface(unittest.TestCase):
7957
"""
8058
mockgun.py was turned into a module. Ensure we haven't broken the interface.
@@ -93,7 +71,7 @@ def test_interface_intact(self):
9371
mockgun.Shotgun
9472

9573

96-
class TestValidateFilterSyntax(TestBaseWithExceptionTests):
74+
class TestValidateFilterSyntax(unittest.TestCase):
9775
"""
9876
Tests filter syntax support.
9977
"""
@@ -127,7 +105,7 @@ def test_filter_array_or_dict(self):
127105
)
128106

129107
# We can't have not dict/list values for filters however.
130-
self.assertRaisesRegexp(
108+
self.assertRaisesRegex(
131109
ShotgunError,
132110
"Filters can only be lists or dictionaries, not int.",
133111
lambda: self._mockgun.find(
@@ -137,7 +115,7 @@ def test_filter_array_or_dict(self):
137115
)
138116

139117

140-
class TestEntityFieldComparison(TestBaseWithExceptionTests):
118+
class TestEntityFieldComparison(unittest.TestCase):
141119
"""
142120
Checks if entity fields comparison work.
143121
"""
@@ -191,7 +169,7 @@ def test_find_entity_with_none_link(self):
191169
self.assertEqual(items[0]["id"], self._project_link["id"])
192170

193171

194-
class TestTextFieldOperators(TestBaseWithExceptionTests):
172+
class TestTextFieldOperators(unittest.TestCase):
195173
"""
196174
Checks if text field comparison work.
197175
"""
@@ -210,7 +188,7 @@ def test_operator_contains(self):
210188
self.assertTrue(item)
211189

212190

213-
class TestMultiEntityFieldComparison(TestBaseWithExceptionTests):
191+
class TestMultiEntityFieldComparison(unittest.TestCase):
214192
"""
215193
Ensures multi entity field comparison work.
216194
"""
@@ -293,7 +271,7 @@ def test_find_with_none(self):
293271
self.assertTrue(len(item["users"]) > 0)
294272

295273

296-
class TestFilterOperator(TestBaseWithExceptionTests):
274+
class TestFilterOperator(unittest.TestCase):
297275
"""
298276
Unit tests for the filter_operator filter syntax.
299277
"""
@@ -408,7 +386,7 @@ def test_nested_filter_operators(self):
408386

409387
def test_invalid_operator(self):
410388

411-
self.assertRaisesRegexp(
389+
self.assertRaisesRegex(
412390
ShotgunError,
413391
"Unknown filter_operator type: bad",
414392
lambda: self._mockgun.find(
@@ -421,7 +399,7 @@ def test_invalid_operator(self):
421399
])
422400
)
423401

424-
self.assertRaisesRegexp(
402+
self.assertRaisesRegex(
425403
ShotgunError,
426404
"Bad filter operator, requires keys 'filter_operator' and 'filters',",
427405
lambda: self._mockgun.find(

tests/test_unit.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,5 @@ def _test_mimetypes_import(self, platform, major, minor, patch_number, result, m
506506
mock.platform = platform
507507
self.assertEqual(_is_mimetypes_broken(), result)
508508

509-
def test_correct_mimetypes_imported(self):
510-
"""
511-
Makes sure fix is imported for only for Python 2.7.0 to 2.7.7 on win32
512-
"""
513-
self._test_mimetypes_import("win32", 2, 6, 9, False)
514-
for patch_number in range(0, 10): # 0 to 9 inclusively
515-
self._test_mimetypes_import("win32", 2, 7, patch_number, True)
516-
self._test_mimetypes_import("win32", 2, 7, 10, False)
517-
self._test_mimetypes_import("win32", 3, 0, 0, False)
518-
self._test_mimetypes_import("darwin", 2, 7, 0, False)
519-
520509
if __name__ == '__main__':
521510
unittest.main()

0 commit comments

Comments
 (0)