From fb4da7c768d3ef4161dfb77302c891315ce37d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Sun, 3 Apr 2022 20:19:41 +0200 Subject: [PATCH] satisfy linter and type checker in many places --- src/c3nav/api/urls.py | 2 +- src/c3nav/editor/views/edit.py | 2 +- src/c3nav/editor/wrappers.py | 2 +- src/c3nav/mapdata/models/access.py | 2 +- src/c3nav/mapdata/models/geometry/level.py | 2 ++ src/c3nav/mapdata/render/engines/openscad.py | 3 ++- src/c3nav/mapdata/render/renderer.py | 2 +- src/c3nav/mapdata/utils/cache/indexed.py | 4 +++- src/c3nav/mapdata/utils/cache/stats.py | 21 ++++++++++---------- src/c3nav/mapdata/utils/mesh.py | 1 + src/c3nav/mapdata/utils/mpl.py | 2 +- src/c3nav/mapdata/views.py | 2 +- src/c3nav/routing/locator.py | 2 +- src/c3nav/routing/route.py | 9 ++++++--- src/c3nav/site/converters.py | 1 + src/c3nav/site/forms.py | 7 ++++--- 16 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/c3nav/api/urls.py b/src/c3nav/api/urls.py index ba84b22d..bf5e1e2c 100644 --- a/src/c3nav/api/urls.py +++ b/src/c3nav/api/urls.py @@ -68,7 +68,7 @@ class APIRoot(GenericAPIView): @cached_property def urls(self): include_editor = can_access_editor(self.request) - urls = OrderedDict() + urls: dict[str, dict[str, str] | str] = OrderedDict() for urlpattern in router.urls: if not include_editor and inspect.getmodule(urlpattern.callback).__name__.startswith('c3nav.editor.'): continue diff --git a/src/c3nav/editor/views/edit.py b/src/c3nav/editor/views/edit.py index dfa8f49d..df048b12 100644 --- a/src/c3nav/editor/views/edit.py +++ b/src/c3nav/editor/views/edit.py @@ -643,7 +643,7 @@ def graph_edit(request, level=None, space=None): 'obj_title': node.title }) - permissions = AccessPermission.get_for_request(request) | set([None]) + permissions = AccessPermission.get_for_request(request) | {None} edge_settings_form = GraphEdgeSettingsForm(instance=GraphEdge(), request=request, data=request.POST) graph_action_form = GraphEditorActionForm(request=request, allow_clicked_position=create_nodes, data=request.POST) diff --git a/src/c3nav/editor/wrappers.py b/src/c3nav/editor/wrappers.py index b3799dee..bb4238af 100644 --- a/src/c3nav/editor/wrappers.py +++ b/src/c3nav/editor/wrappers.py @@ -475,7 +475,7 @@ class BaseQueryWrapper(BaseWrapper): if not segments: # if the check is just 'pk' or the name or the name of the primary key, return the mathing object if is_created_pk(filter_value): - return Q(pk__in=()), set([filter_value]) + return Q(pk__in=()), {filter_value} if filter_value is None or int(filter_value) in self._changeset.deleted_existing.get(model, ()): return Q(pk__in=()), set() return q, set() diff --git a/src/c3nav/mapdata/models/access.py b/src/c3nav/mapdata/models/access.py index 431bd346..70ee0832 100644 --- a/src/c3nav/mapdata/models/access.py +++ b/src/c3nav/mapdata/models/access.py @@ -213,7 +213,7 @@ class AccessPermission(models.Model): access_restriction_ids = set(permissions.keys()) expire_date = min((e for e in permissions.values() if e), default=timezone.now()+timedelta(seconds=120)) - cache.set(cache_key, access_restriction_ids, max(0, (expire_date-timezone.now()).total_seconds())) + cache.set(cache_key, access_restriction_ids, max(0.0, (expire_date-timezone.now()).total_seconds())) return set(access_restriction_ids) @classmethod diff --git a/src/c3nav/mapdata/models/geometry/level.py b/src/c3nav/mapdata/models/geometry/level.py index 430e34e1..2ea7b106 100644 --- a/src/c3nav/mapdata/models/geometry/level.py +++ b/src/c3nav/mapdata/models/geometry/level.py @@ -343,8 +343,10 @@ class AltitudeArea(LevelGeometryMixin, models.Model): csgraph[area.tmpid, connected_tmpid] = True repeat = True + while repeat: repeat = False + # noinspection PyTupleAssignmentBalance distances, predecessors = dijkstra(csgraph, directed=False, return_predecessors=True, unweighted=True) np_areas_with_altitude = np.array(areas_with_altitude, dtype=np.uint32) relevant_distances = distances[np_areas_with_altitude[:, None], np_areas_with_altitude] diff --git a/src/c3nav/mapdata/render/engines/openscad.py b/src/c3nav/mapdata/render/engines/openscad.py index 58ebb59e..cc86bdf9 100644 --- a/src/c3nav/mapdata/render/engines/openscad.py +++ b/src/c3nav/mapdata/render/engines/openscad.py @@ -19,7 +19,7 @@ class AbstractOpenScadElem(ABC): raise NotADirectoryError -class AbstractOpenScadBlock(AbstractOpenScadElem, UserList): +class AbstractOpenScadBlock(AbstractOpenScadElem, UserList, ABC): def render_children(self): return '\n'.join(child.render() for child in self.data if child is not None) @@ -290,6 +290,7 @@ class OpenSCADEngine(Base3DEngine): main_building_block.append(obstacles_block) if self.min_width and geoms.on_top_of_id is None: + # noinspection PyUnboundLocalVariable main_building_block_inner.append( self._add_polygon('min width', self._satisfy_min_width(buildings).intersection(self.bbox).buffer(0), diff --git a/src/c3nav/mapdata/render/renderer.py b/src/c3nav/mapdata/render/renderer.py index f3682dc0..3a0473cc 100644 --- a/src/c3nav/mapdata/render/renderer.py +++ b/src/c3nav/mapdata/render/renderer.py @@ -32,7 +32,7 @@ class MapRenderer: def render(self, engine_cls, center=True): # add no access restriction to “unlocked“ access restrictions so lookup gets easier - access_permissions = self.access_permissions | set([None]) + access_permissions = self.access_permissions | {None} bbox = prepared.prep(self.bbox) diff --git a/src/c3nav/mapdata/utils/cache/indexed.py b/src/c3nav/mapdata/utils/cache/indexed.py index 62db1997..0750e74a 100644 --- a/src/c3nav/mapdata/utils/cache/indexed.py +++ b/src/c3nav/mapdata/utils/cache/indexed.py @@ -75,7 +75,7 @@ class GeometryIndexed: self._write_metadata(f) f.write(self.data.tobytes('C')) - def _write_metadata(cls, f): + def _write_metadata(self, f): pass def _get_geometry_bounds(self, geometry): @@ -180,7 +180,9 @@ class GeometryIndexed: int(math.ceil((maxx-minx)/self.resolution))), dtype=np.uint8) if self.data.size: + # noinspection PyArgumentList minval = min(self.data.min(), 0) + # noinspection PyArgumentList maxval = max(self.data.max(), minval+0.01) visible_data = ((self.data.astype(float)-minval)*255/(maxval-minval)).clip(0, 255).astype(np.uint8) image_data[self.y:self.y+height, self.x:self.x+width] = visible_data diff --git a/src/c3nav/mapdata/utils/cache/stats.py b/src/c3nav/mapdata/utils/cache/stats.py index ee0e2f8b..e756bae8 100644 --- a/src/c3nav/mapdata/utils/cache/stats.py +++ b/src/c3nav/mapdata/utils/cache/stats.py @@ -59,16 +59,17 @@ fake_request = FakeRequest() def convert_stats(stats): stats = [(name.split('__')[1:], value) for name, value in stats['data'].items()] - result = {} - result['locate'] = convert_locate(_filter_stats('locate', stats)) - result['location_retrieve'] = convert_location(_filter_stats('location_retrieve', stats)) - result['location_geometry'] = convert_location(_filter_stats('location_geometry', stats)) - result['route_origin'] = convert_location( - (['pk']+name, value) for name, value in _filter_stats('route_origin_', stats, startswith=True) - ) - result['route_destination'] = convert_location( - (['pk'] + name, value) for name, value in _filter_stats('route_destination_', stats, startswith=True) - ) + result = { + 'locate': convert_locate(_filter_stats('locate', stats)), + 'location_retrieve': convert_location(_filter_stats('location_retrieve', stats)), + 'location_geometry': convert_location(_filter_stats('location_geometry', stats)), + 'route_origin': convert_location( + (['pk'] + name, value) for name, value in _filter_stats('route_origin_', stats, startswith=True) + ), + 'route_destination': convert_location( + (['pk'] + name, value) for name, value in _filter_stats('route_destination_', stats, startswith=True) + ), + } return result diff --git a/src/c3nav/mapdata/utils/mesh.py b/src/c3nav/mapdata/utils/mesh.py index c806fdc0..3fca2cb7 100644 --- a/src/c3nav/mapdata/utils/mesh.py +++ b/src/c3nav/mapdata/utils/mesh.py @@ -55,6 +55,7 @@ def triangulate_rings(rings, holes=None): # remove triangles with no area facets = np.dstack((np.zeros(mesh_elements.shape), mesh_points[mesh_elements])) + # noinspection PyArgumentList ok_index = np.cross(facets[:, 1] - facets[:, 0], facets[:, 2] - facets[:, 1]).max(axis=1) != 0 mesh_elements = mesh_elements[ok_index] diff --git a/src/c3nav/mapdata/utils/mpl.py b/src/c3nav/mapdata/utils/mpl.py index 8dac3459..ce5e60ff 100644 --- a/src/c3nav/mapdata/utils/mpl.py +++ b/src/c3nav/mapdata/utils/mpl.py @@ -98,7 +98,7 @@ class MplPolygonPath(MplPathProxy): def shapely_to_mpl(geometry): """ convert a shapely Polygon or Multipolygon to a matplotlib Path - :param polygon: shapely Polygon or Multipolygon + :param geometry: shapely Polygon or Multipolygon :return: MplPathProxy """ if isinstance(geometry, Polygon): diff --git a/src/c3nav/mapdata/views.py b/src/c3nav/mapdata/views.py index c1fb224f..8f99a747 100644 --- a/src/c3nav/mapdata/views.py +++ b/src/c3nav/mapdata/views.py @@ -81,7 +81,7 @@ def tile(request, level, zoom, x, y, access_permissions=None): access_permissions = parse_tile_access_cookie(cookie, settings.SECRET_TILE_KEY) access_permissions &= set(level_data.restrictions[minx:maxx, miny:maxy]) else: - access_permissions = set(int(i) for i in access_permissions.split('-')) - set([0]) + access_permissions = set(int(i) for i in access_permissions.split('-')) - {0} # build cache keys last_update = level_data.history.last_update(minx, miny, maxx, maxy) diff --git a/src/c3nav/routing/locator.py b/src/c3nav/routing/locator.py index 4f92c1bd..b07c6f0d 100644 --- a/src/c3nav/routing/locator.py +++ b/src/c3nav/routing/locator.py @@ -108,7 +108,7 @@ class LocatorStations: station = self.stations[station_id] station.frequencies.add(frequency) elif create: - station = LocatorStation(bssid, ssid, set((frequency, ))) + station = LocatorStation(bssid, ssid, {frequency}) station_id = len(self.stations) self.stations_lookup[(bssid, None)] = station_id self.stations.append(station) diff --git a/src/c3nav/routing/route.py b/src/c3nav/routing/route.py index 45ed6680..a56a29eb 100644 --- a/src/c3nav/routing/route.py +++ b/src/c3nav/routing/route.py @@ -95,6 +95,7 @@ class Route: description = item.waytype.description if item.waytype.up_separate and item.edge.rise > 0: description = item.waytype.description_up + # noinspection PyComparisonWithNone if (item.waytype.level_change_description != False and last_primary_level and ((item.last_item and item.level != item.last_item.level) or item.level.on_top_of_id)): # != False because it's lazy @@ -124,8 +125,9 @@ class Route: description = current_space.leave_descriptions.get(next_space.pk, None) if description is None: description = item.space.enter_description + # noinspection PyComparisonWithNone if description == None: # could be a lazy None - description = _('Go to %(space_title)s.') % {'space_title': item.space.title} + description = _('Go to %(space_title)s.') % {'space_title': item.space.title} item.descriptions.append(('more_vert', description)) @@ -140,7 +142,9 @@ class Route: if item.edge: remaining_distance += item.edge.distance if remaining_distance: - item.descriptions.append(('more_vert', _('%d m remaining to your destination.') % max(remaining_distance, 1))) + item.descriptions.append( + ('more_vert', _('%d m remaining to your destination.') % max(remaining_distance, 1)) + ) items[-1].descriptions.append(('done', _('You have reached your destination.'))) @@ -240,6 +244,5 @@ class RouteItem: return result - class NoRoute: distance = np.inf diff --git a/src/c3nav/site/converters.py b/src/c3nav/site/converters.py index bf371050..46619f05 100644 --- a/src/c3nav/site/converters.py +++ b/src/c3nav/site/converters.py @@ -38,6 +38,7 @@ class AtPositionConverter: class ConditionalConverter: + # noinspection PyMethodOverriding def __init_subclass__(cls, /, name): cls.path = '%s/' % name cls.regex = '(%s/)?' % name diff --git a/src/c3nav/site/forms.py b/src/c3nav/site/forms.py index 0a779f11..62a91ff7 100644 --- a/src/c3nav/site/forms.py +++ b/src/c3nav/site/forms.py @@ -36,15 +36,16 @@ class ReportUpdateForm(ModelForm): ('false', _('closed')), ) - def save(self): + def save(self, commit=True): with transaction.atomic(): - super().save() + super().save(commit=commit) report = self.instance.report if self.instance.open is not None: report.open = self.instance.open if self.instance.assigned_to: report.assigned_to = self.instance.assigned_to - report.save() + if commit: + report.save() class Meta: model = ReportUpdate