Skip to content

Commit 87309ae

Browse files
committed
Modernize the code for Python 3.9
with the help of pyupgrade https://github.com/asottile/pyupgrade
1 parent 381f2af commit 87309ae

File tree

10 files changed

+36
-46
lines changed

10 files changed

+36
-46
lines changed

rest_framework_gis/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44

55
def get_version():
6-
version = '%s.%s' % (VERSION[0], VERSION[1])
6+
version = f'{VERSION[0]}.{VERSION[1]}'
77
if VERSION[2]:
8-
version = '%s.%s' % (version, VERSION[2])
8+
version = f'{version}.{VERSION[2]}'
99
if VERSION[3:] == ('alpha', 0):
1010
version = '%s pre-alpha' % version
1111
else:
1212
if VERSION[3] != 'final':
13-
version = '%s %s' % (version, VERSION[3])
13+
version = f'{version} {VERSION[3]}'
1414
return version

rest_framework_gis/fields.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def to_internal_value(self, value):
7272
)
7373
)
7474
except (ValueError, TypeError, GDALException) as e:
75-
raise ValidationError(
76-
_('Unable to convert to python object: {}'.format(str(e)))
77-
)
75+
raise ValidationError(_(f'Unable to convert to python object: {str(e)}'))
7876

7977
def validate_empty_values(self, data):
8078
if data == '':

rest_framework_gis/filters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_filter_bbox(self, request):
4646
p1x, p1y, p2x, p2y = (float(n) for n in bbox_string.split(','))
4747
except ValueError:
4848
raise ParseError(
49-
'Invalid bbox string supplied for parameter {0}'.format(self.bbox_param)
49+
f'Invalid bbox string supplied for parameter {self.bbox_param}'
5050
)
5151

5252
x = Polygon.from_bbox((p1x, p1y, p2x, p2y))
@@ -66,7 +66,7 @@ def filter_queryset(self, request, queryset, view):
6666
bbox = self.get_filter_bbox(request)
6767
if not bbox:
6868
return queryset
69-
return queryset.filter(Q(**{'%s__%s' % (filter_field, geoDjango_filter): bbox}))
69+
return queryset.filter(Q(**{f'{filter_field}__{geoDjango_filter}': bbox}))
7070

7171
def get_schema_operation_parameters(self, view):
7272
return [
@@ -127,7 +127,7 @@ def get_filter_bbox(self, request):
127127
z, x, y = (int(n) for n in tile_string.split('/'))
128128
except ValueError:
129129
raise ParseError(
130-
'Invalid tile string supplied for parameter {0}'.format(self.tile_param)
130+
f'Invalid tile string supplied for parameter {self.tile_param}'
131131
)
132132

133133
bbox = Polygon.from_bbox(tile_edges(x, y, z))
@@ -158,7 +158,7 @@ def get_filter_point(self, request, **kwargs):
158158
(x, y) = (float(n) for n in point_string.split(','))
159159
except ValueError:
160160
raise ParseError(
161-
'Invalid geometry string supplied for parameter {0}'.format(
161+
'Invalid geometry string supplied for parameter {}'.format(
162162
self.point_param
163163
)
164164
)
@@ -212,7 +212,7 @@ def filter_queryset(self, request, queryset, view):
212212
dist = float(dist_string)
213213
except ValueError:
214214
raise ParseError(
215-
'Invalid distance string supplied for parameter {0}'.format(
215+
'Invalid distance string supplied for parameter {}'.format(
216216
self.dist_param
217217
)
218218
)
@@ -222,7 +222,7 @@ def filter_queryset(self, request, queryset, view):
222222
dist = self.dist_to_deg(dist, point[1])
223223

224224
return queryset.filter(
225-
Q(**{'%s__%s' % (filter_field, geoDjango_filter): (point, dist)})
225+
Q(**{f'{filter_field}__{geoDjango_filter}': (point, dist)})
226226
)
227227

228228
def get_schema_operation_parameters(self, view):

rest_framework_gis/schema.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def _map_geo_field(self, serializer, geo_field_name):
101101
try:
102102
return self.GEO_FIELD_TO_SCHEMA[geo_field.__class__]
103103
except KeyError:
104-
warnings.warn(
105-
"Geometry generation for {field} is not supported.".format(field=field)
106-
)
104+
warnings.warn(f"Geometry generation for {field} is not supported.")
107105
return {}
108106

109107
def map_field(self, field):

rest_framework_gis/serializers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def __init__(self, *args, **kwargs):
8181
def check_excludes(field_name, field_role):
8282
"""make sure the field is not excluded"""
8383
if hasattr(meta, 'exclude') and field_name in meta.exclude:
84-
raise ImproperlyConfigured(
85-
"You cannot exclude your '{0}'.".format(field_role)
86-
)
84+
raise ImproperlyConfigured(f"You cannot exclude your '{field_role}'.")
8785

8886
def add_to_fields(field_name):
8987
"""Make sure the field is included in the fields"""

tests/django_restframework_gis_tests/test_bbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_list(self):
5353
elif fid == 2:
5454
self.assertEqual(feature['bbox'], self.bl2.bbox_geometry.extent)
5555
else:
56-
self.fail("Unexpected id: {0}".format(fid))
56+
self.fail(f"Unexpected id: {fid}")
5757
BoxedLocation.objects.all().delete()
5858

5959
def test_post_location_list_geojson(self):

tests/django_restframework_gis_tests/test_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
class BaseTestCase(TestCase):
117117
@staticmethod
118118
def get_instance(data_dict):
119-
class Model(object):
119+
class Model:
120120
def __init__(self, geojson_dict):
121121
self.geometry = GEOSGeometry(json.dumps(geojson_dict))
122122

tests/django_restframework_gis_tests/test_filters.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@ def test_DistanceToPointFilter_filtering(self):
274274
distance = 5000 # meters
275275
point_on_alcatraz = [-122.4222, 37.82667]
276276

277-
url_params = '?dist=%0.4f&point=hello&format=json' % (distance,)
277+
url_params = f'?dist={distance:0.4f}&point=hello&format=json'
278278
response = self.client.get(
279-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
279+
f'{self.location_within_distance_of_point_list_url}{url_params}'
280280
)
281281
self.assertEqual(response.status_code, 400)
282282

283-
url_params = '?dist=%0.4f&point=%0.4f,%0.4f&format=json' % (
283+
url_params = '?dist={:0.4f}&point={:0.4f},{:0.4f}&format=json'.format(
284284
distance,
285285
point_on_alcatraz[0],
286286
point_on_alcatraz[1],
@@ -299,21 +299,21 @@ def test_DistanceToPointFilter_filtering(self):
299299

300300
# Make sure we only get back the ones within the distance
301301
response = self.client.get(
302-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
302+
f'{self.location_within_distance_of_point_list_url}{url_params}'
303303
)
304304
self.assertEqual(len(response.data['features']), 1)
305305
for result in response.data['features']:
306306
self.assertEqual(result['properties']['name'], treasure_island.name)
307307

308308
# Make sure we get back all the ones within the distance
309309
distance = 7000
310-
url_params = '?dist=%0.4f&point=%0.4f,%0.4f&format=json' % (
310+
url_params = '?dist={:0.4f}&point={:0.4f},{:0.4f}&format=json'.format(
311311
distance,
312312
point_on_alcatraz[0],
313313
point_on_alcatraz[1],
314314
)
315315
response = self.client.get(
316-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
316+
f'{self.location_within_distance_of_point_list_url}{url_params}'
317317
)
318318
self.assertEqual(len(response.data['features']), 2)
319319
for result in response.data['features']:
@@ -323,7 +323,7 @@ def test_DistanceToPointFilter_filtering(self):
323323

324324
# Make sure we only get back the ones within the distance
325325
degrees = 0.05
326-
url_params = '?dist=%0.4f&point=%0.4f,%0.4f&format=json' % (
326+
url_params = '?dist={:0.4f}&point={:0.4f},{:0.4f}&format=json'.format(
327327
degrees,
328328
point_on_alcatraz[0],
329329
point_on_alcatraz[1],
@@ -348,7 +348,7 @@ def test_DistanceToPointOrderingFilter_filtering(self):
348348

349349
url_params = '?point=hello&format=json'
350350
response = self.client.get(
351-
'%s%s' % (self.location_order_distance_to_point, url_params)
351+
f'{self.location_order_distance_to_point}{url_params}'
352352
)
353353
self.assertEqual(response.status_code, 400)
354354

@@ -381,7 +381,7 @@ def test_DistanceToPointOrderingFilter_filtering(self):
381381

382382
url_params = '?point=%i,%i&format=json' % (point[0], point[1])
383383
response = self.client.get(
384-
'%s%s' % (self.location_order_distance_to_point, url_params)
384+
f'{self.location_order_distance_to_point}{url_params}'
385385
)
386386
self.assertEqual(len(response.data['features']), 8)
387387
self.assertEqual(
@@ -400,7 +400,7 @@ def test_DistanceToPointOrderingFilter_filtering(self):
400400

401401
url_params = '?point=%i,%i&order=desc&format=json' % (point[0], point[1])
402402
response = self.client.get(
403-
'%s%s' % (self.location_order_distance_to_point, url_params)
403+
f'{self.location_order_distance_to_point}{url_params}'
404404
)
405405
self.assertEqual(len(response.data['features']), 8)
406406
self.assertEqual(
@@ -445,11 +445,9 @@ def test_GeometryField_filtering(self):
445445
except AttributeError:
446446
quoted_param = urllib.parse.quote(point_inside_ggpark_geojson)
447447

448-
url_params = "?contains_properly=%s" % (quoted_param,)
448+
url_params = f"?contains_properly={quoted_param}"
449449

450-
response = self.client.get(
451-
'{0}{1}'.format(self.geojson_contained_in_geometry, url_params)
452-
)
450+
response = self.client.get(f'{self.geojson_contained_in_geometry}{url_params}')
453451
self.assertEqual(len(response.data), 1)
454452

455453
geometry_response = GEOSGeometry(json.dumps(response.data[0]['geometry']))
@@ -511,7 +509,7 @@ def test_TileFilter_ValueError(self):
511509
def test_DistanceToPointFilter_filtering_none(self):
512510
url_params = '?dist=5000&point=&format=json'
513511
response = self.client.get(
514-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
512+
f'{self.location_within_distance_of_point_list_url}{url_params}'
515513
)
516514
self.assertDictEqual(
517515
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -522,7 +520,7 @@ def test_DistanceToPointFilter_filter_field_none(self):
522520
GeojsonLocationWithinDistanceOfPointList.distance_filter_field = None
523521
url_params = '?dist=5000&point=&format=json'
524522
response = self.client.get(
525-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
523+
f'{self.location_within_distance_of_point_list_url}{url_params}'
526524
)
527525
self.assertDictEqual(
528526
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -532,7 +530,7 @@ def test_DistanceToPointFilter_filter_field_none(self):
532530
def test_DistanceToPointFilter_ValueError_point(self):
533531
url_params = '?dist=500.0&point=hello&format=json'
534532
response = self.client.get(
535-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
533+
f'{self.location_within_distance_of_point_list_url}{url_params}'
536534
)
537535
self.assertEqual(
538536
response.data['detail'],
@@ -542,7 +540,7 @@ def test_DistanceToPointFilter_ValueError_point(self):
542540
def test_DistanceToPointFilter_ValueError_distance(self):
543541
url_params = '?dist=wrong&point=12.0,42.0&format=json'
544542
response = self.client.get(
545-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
543+
f'{self.location_within_distance_of_point_list_url}{url_params}'
546544
)
547545
self.assertEqual(
548546
response.data['detail'],
@@ -552,7 +550,7 @@ def test_DistanceToPointFilter_ValueError_distance(self):
552550
def test_DistanceToPointOrderingFilter_filtering_none(self):
553551
url_params = '?point=&format=json'
554552
response = self.client.get(
555-
'%s%s' % (self.location_order_distance_to_point, url_params)
553+
f'{self.location_order_distance_to_point}{url_params}'
556554
)
557555
self.assertDictEqual(
558556
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -565,7 +563,7 @@ def test_DistanceToPointOrderingFilter_ordering_filter_field_none(self):
565563
GeojsonLocationOrderDistanceToPointList.distance_ordering_filter_field = None
566564
url_params = '?point=&format=json'
567565
response = self.client.get(
568-
'%s%s' % (self.location_order_distance_to_point, url_params)
566+
f'{self.location_order_distance_to_point}{url_params}'
569567
)
570568
self.assertDictEqual(
571569
response.data, {'type': 'FeatureCollection', 'features': []}

tests/django_restframework_gis_tests/test_performance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Meta:
4949
with Timer() as t:
5050
JSONRenderer().render(serializer.data)
5151
# print results
52-
msg = 'GeoJSON rendering of {0} objects ' 'completed in {1}'.format(
52+
msg = 'GeoJSON rendering of {} objects ' 'completed in {}'.format(
5353
self.NUMBER_OF_LOCATIONS, t.elapsed
5454
)
55-
print('\n\033[95m{0}\033[0m'.format(msg))
55+
print(f'\n\033[95m{msg}\033[0m')

tests/django_restframework_gis_tests/tests.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def test_geometry_empty_representation(self):
662662
)
663663
for geom_type in geom_types:
664664
with self.subTest(geom_type=geom_type):
665-
value = f.to_representation(GEOSGeometry('{} EMPTY'.format(geom_type)))
665+
value = f.to_representation(GEOSGeometry(f'{geom_type} EMPTY'))
666666
self.assertIsNotNone(value)
667667
if geom_type == 'LINEARRING':
668668
geom_type = 'LINESTRING'
@@ -699,9 +699,7 @@ def test_geojson_pagination(self):
699699
response = self.client.get(self.geojson_location_list_url)
700700
self.assertEqual(response.data['type'], 'FeatureCollection')
701701
self.assertEqual(len(response.data['features']), 2)
702-
response = self.client.get(
703-
'{0}?page_size=1'.format(self.geojson_location_list_url)
704-
)
702+
response = self.client.get(f'{self.geojson_location_list_url}?page_size=1')
705703
self.assertEqual(response.data['type'], 'FeatureCollection')
706704
self.assertEqual(len(response.data['features']), 1)
707705
self.assertIn('next', response.data)

0 commit comments

Comments
 (0)