Skip to content

Commit 28aa8e1

Browse files
committed
Modernize the code for Python 3.9
with the help of pyupgrade https://github.com/asottile/pyupgrade
1 parent 69cabda commit 28aa8e1

File tree

10 files changed

+36
-46
lines changed

10 files changed

+36
-46
lines changed

rest_framework_gis/__init__.py

+3-3
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

+1-3
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

+6-6
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

+1-3
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

+1-3
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

+1-1
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

+1-1
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

+18-20
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ def test_DistanceToPointFilter_filtering(self):
273273
distance = 5000 # meters
274274
point_on_alcatraz = [-122.4222, 37.82667]
275275

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

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

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

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

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

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

@@ -380,7 +380,7 @@ def test_DistanceToPointOrderingFilter_filtering(self):
380380

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

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

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

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

454452
geometry_response = GEOSGeometry(json.dumps(response.data[0]['geometry']))
@@ -510,7 +508,7 @@ def test_TileFilter_ValueError(self):
510508
def test_DistanceToPointFilter_filtering_none(self):
511509
url_params = '?dist=5000&point=&format=json'
512510
response = self.client.get(
513-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
511+
f'{self.location_within_distance_of_point_list_url}{url_params}'
514512
)
515513
self.assertDictEqual(
516514
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -521,7 +519,7 @@ def test_DistanceToPointFilter_filter_field_none(self):
521519
GeojsonLocationWithinDistanceOfPointList.distance_filter_field = None
522520
url_params = '?dist=5000&point=&format=json'
523521
response = self.client.get(
524-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
522+
f'{self.location_within_distance_of_point_list_url}{url_params}'
525523
)
526524
self.assertDictEqual(
527525
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -531,7 +529,7 @@ def test_DistanceToPointFilter_filter_field_none(self):
531529
def test_DistanceToPointFilter_ValueError_point(self):
532530
url_params = '?dist=500.0&point=hello&format=json'
533531
response = self.client.get(
534-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
532+
f'{self.location_within_distance_of_point_list_url}{url_params}'
535533
)
536534
self.assertEqual(
537535
response.data['detail'],
@@ -541,7 +539,7 @@ def test_DistanceToPointFilter_ValueError_point(self):
541539
def test_DistanceToPointFilter_ValueError_distance(self):
542540
url_params = '?dist=wrong&point=12.0,42.0&format=json'
543541
response = self.client.get(
544-
'%s%s' % (self.location_within_distance_of_point_list_url, url_params)
542+
f'{self.location_within_distance_of_point_list_url}{url_params}'
545543
)
546544
self.assertEqual(
547545
response.data['detail'],
@@ -551,7 +549,7 @@ def test_DistanceToPointFilter_ValueError_distance(self):
551549
def test_DistanceToPointOrderingFilter_filtering_none(self):
552550
url_params = '?point=&format=json'
553551
response = self.client.get(
554-
'%s%s' % (self.location_order_distance_to_point, url_params)
552+
f'{self.location_order_distance_to_point}{url_params}'
555553
)
556554
self.assertDictEqual(
557555
response.data, {'type': 'FeatureCollection', 'features': []}
@@ -564,7 +562,7 @@ def test_DistanceToPointOrderingFilter_ordering_filter_field_none(self):
564562
GeojsonLocationOrderDistanceToPointList.distance_ordering_filter_field = None
565563
url_params = '?point=&format=json'
566564
response = self.client.get(
567-
'%s%s' % (self.location_order_distance_to_point, url_params)
565+
f'{self.location_order_distance_to_point}{url_params}'
568566
)
569567
self.assertDictEqual(
570568
response.data, {'type': 'FeatureCollection', 'features': []}

tests/django_restframework_gis_tests/test_performance.py

+2-2
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def test_geometry_empty_representation(self):
660660
)
661661
for geom_type in geom_types:
662662
with self.subTest(geom_type=geom_type):
663-
value = f.to_representation(GEOSGeometry('{} EMPTY'.format(geom_type)))
663+
value = f.to_representation(GEOSGeometry(f'{geom_type} EMPTY'))
664664
self.assertIsNotNone(value)
665665
if geom_type == 'LINEARRING':
666666
geom_type = 'LINESTRING'
@@ -697,9 +697,7 @@ def test_geojson_pagination(self):
697697
response = self.client.get(self.geojson_location_list_url)
698698
self.assertEqual(response.data['type'], 'FeatureCollection')
699699
self.assertEqual(len(response.data['features']), 2)
700-
response = self.client.get(
701-
'{0}?page_size=1'.format(self.geojson_location_list_url)
702-
)
700+
response = self.client.get(f'{self.geojson_location_list_url}?page_size=1')
703701
self.assertEqual(response.data['type'], 'FeatureCollection')
704702
self.assertEqual(len(response.data['features']), 1)
705703
self.assertIn('next', response.data)

0 commit comments

Comments
 (0)