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

field_provider.py mutable default argument #187

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions field_friend/automations/field_provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import uuid
from typing import Any, Optional
from typing import Any

import rosys
from geographiclib.geodesic import Geodesic
Expand Down Expand Up @@ -53,7 +53,9 @@ def invalidate(self) -> None:
self.request_backup()
self.FIELDS_CHANGED.emit()

def create_field(self, points: list[GeoPoint] = []) -> Field:
def create_field(self, points: list[GeoPoint] | None = None) -> Field:
if points is None:
points = []
new_id = str(uuid.uuid4())
field = Field(id=f'{new_id}', name=f'field_{len(self.fields)+1}', points=points)
self.fields.append(field)
Expand All @@ -70,7 +72,9 @@ def clear_fields(self) -> None:
self.FIELDS_CHANGED.emit()
self.invalidate()

def create_obstacle(self, field: Field, points: list[GeoPoint] = []) -> FieldObstacle:
def create_obstacle(self, field: Field, points: list[GeoPoint] | None = None) -> FieldObstacle:
if points is None:
points = []
obstacle = FieldObstacle(id=f'{str(uuid.uuid4())}', name=f'obstacle_{len(field.obstacles)+1}', points=points)
field.obstacles.append(obstacle)
self.invalidate()
Expand All @@ -80,7 +84,9 @@ def remove_obstacle(self, field: Field, obstacle: FieldObstacle) -> None:
field.obstacles.remove(obstacle)
self.invalidate()

def create_row(self, field: Field, points: list[GeoPoint] = []) -> Row:
def create_row(self, field: Field, points: list[GeoPoint] | None = None) -> Row:
if points is None:
points = []
row = Row(id=f'{str(uuid.uuid4())}', name=f'row_{len(field.rows)+1}', points=points)
field.rows.append(row)
self.invalidate()
Expand Down Expand Up @@ -132,7 +138,7 @@ def get_distance(row: Row, direction: str):
self.fields[field_index].rows = sorted(field.rows, key=lambda row: get_distance(row, direction=direction))
self.FIELDS_CHANGED.emit()

async def add_field_point(self, field: Field, point: Optional[GeoPoint] = None, new_point: Optional[GeoPoint] = None) -> None:
async def add_field_point(self, field: Field, point: GeoPoint | None = None, new_point: GeoPoint | None = None) -> None:
assert self.gnss.current is not None
positioning = self.gnss.current.location
if positioning is None or positioning.lat == 0 or positioning.long == 0:
Expand All @@ -149,15 +155,15 @@ async def add_field_point(self, field: Field, point: Optional[GeoPoint] = None,
field.points.append(new_point)
self.invalidate()

def remove_field_point(self, field: Field, point: Optional[GeoPoint] = None) -> None:
def remove_field_point(self, field: Field, point: GeoPoint | None = None) -> None:
if point is not None:
index = field.points.index(point)
del field.points[index]
elif field.points:
del field.points[-1]
self.invalidate()

def add_obstacle_point(self, field: Field, obstacle: FieldObstacle, point: Optional[GeoPoint] = None, new_point: Optional[GeoPoint] = None) -> None:
def add_obstacle_point(self, field: Field, obstacle: FieldObstacle, point: GeoPoint | None = None, new_point: GeoPoint | None = None) -> None:
if new_point is None:
assert self.gnss.current is not None
positioning = self.gnss.current.location
Expand All @@ -175,7 +181,7 @@ def add_obstacle_point(self, field: Field, obstacle: FieldObstacle, point: Optio
obstacle.points.append(new_point)
self.invalidate()

def remove_obstacle_point(self, obstacle: FieldObstacle, point: Optional[GeoPoint] = None) -> None:
def remove_obstacle_point(self, obstacle: FieldObstacle, point: GeoPoint | None = None) -> None:
if obstacle.points:
if point is not None:
index = obstacle.points.index(point)
Expand All @@ -184,7 +190,7 @@ def remove_obstacle_point(self, obstacle: FieldObstacle, point: Optional[GeoPoin
del obstacle.points[-1]
self.invalidate()

def add_row_point(self, field: Field, row: Row, point: Optional[GeoPoint] = None, new_point: Optional[GeoPoint] = None) -> None:
def add_row_point(self, field: Field, row: Row, point: GeoPoint | None = None, new_point: GeoPoint | None = None) -> None:
if new_point is None:
assert self.gnss.current is not None
positioning = self.gnss.current.location
Expand All @@ -202,7 +208,7 @@ def add_row_point(self, field: Field, row: Row, point: Optional[GeoPoint] = None
row.points.append(new_point)
self.invalidate()

def remove_row_point(self, row: Row, point: Optional[GeoPoint] = None) -> None:
def remove_row_point(self, row: Row, point: GeoPoint | None = None) -> None:
if row.points:
if point is not None:
index = row.points.index(point)
Expand Down
Loading