Skip to content

Commit

Permalink
grass.temporal: Simple typing annotations (#4641)
Browse files Browse the repository at this point in the history
* grass.temporal: Add typing annotations for None returns

* grass.temporal: Add typing annotations for boolean parameters

* grass.temporal: Add typing annotations for simple scalar returns

* grass.temporal: Sort imports

* grass.temporal: Add typing annotations for certain magic methods

* grass.temporal: Add typing annotations for integer parameters

* grass.temporal: Add typing annotations for string parameters

* grass.temporal: Add typing annotations of guessing common names, manually filtered

* grass.temporal.datetime_math: Add typing annotations for mydate parameter as datetime

* grass.temporal.datetime_math: Add typing annotations for datetime return types and increment

* grass.temporal.datetime_math: Add typing annotations for other simple conversions

* grass.temporal.datetime_math: Remove unneeded comment line separators

* grass.temporal: Fix some small typos
  • Loading branch information
echoix authored Nov 12, 2024
1 parent 0cab996 commit 147c005
Show file tree
Hide file tree
Showing 47 changed files with 1,040 additions and 943 deletions.
26 changes: 13 additions & 13 deletions python/grass/temporal/abstract_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class AbstractDataset(

__metaclass__ = ABCMeta

def __init__(self):
def __init__(self) -> None:
SpatialTopologyDatasetConnector.__init__(self)
TemporalTopologyDatasetConnector.__init__(self)
self.msgr = get_tgis_message_interface()

def reset_topology(self):
def reset_topology(self) -> None:
"""Reset any information about temporal topology"""

self.reset_spatial_topology()
Expand Down Expand Up @@ -88,12 +88,12 @@ def get_number_of_relations(self):

return None

def set_topology_build_true(self):
def set_topology_build_true(self) -> None:
"""Use this method when the spatio-temporal topology was build"""
self.set_spatial_topology_build_true()
self.set_temporal_topology_build_true()

def set_topology_build_false(self):
def set_topology_build_false(self) -> None:
"""Use this method when the spatio-temporal topology was not build"""
self.set_spatial_topology_build_false()
self.set_temporal_topology_build_false()
Expand All @@ -110,13 +110,13 @@ def is_topology_build(self):

return d

def print_topology_info(self):
def print_topology_info(self) -> None:
if self.is_temporal_topology_build():
self.print_temporal_topology_info()
if self.is_spatial_topology_build():
self.print_spatial_topology_info()

def print_topology_shell_info(self):
def print_topology_shell_info(self) -> None:
if self.is_temporal_topology_build():
self.print_temporal_topology_shell_info()
if self.is_spatial_topology_build():
Expand Down Expand Up @@ -223,7 +223,7 @@ def print_shell_info(self):
def print_self(self):
"""Print the content of the internal structure to stdout"""

def set_id(self, ident):
def set_id(self, ident) -> None:
"""Set the identifier of the dataset"""
self.base.set_id(ident)
self.temporal_extent.set_id(ident)
Expand Down Expand Up @@ -351,7 +351,7 @@ def get_spatial_extent(self):
"""Return the spatial extent"""
return self.spatial_extent

def select(self, dbif=None, mapset=None):
def select(self, dbif=None, mapset=None) -> None:
"""Select temporal dataset entry from database and fill
the internal structure
Expand Down Expand Up @@ -392,7 +392,7 @@ def is_in_db(self, dbif=None, mapset=None):
def delete(self):
"""Delete dataset from database if it exists"""

def insert(self, dbif=None, execute=True):
def insert(self, dbif=None, execute: bool = True):
"""Insert dataset into database
:param dbif: The database interface to be used
Expand Down Expand Up @@ -434,7 +434,7 @@ def insert(self, dbif=None, execute=True):
dbif.close()
return statement

def update(self, dbif=None, execute=True, ident=None):
def update(self, dbif=None, execute: bool = True, ident=None):
"""Update the dataset entry in the database from the internal structure
excluding None variables
Expand Down Expand Up @@ -468,7 +468,7 @@ def update(self, dbif=None, execute=True, ident=None):
dbif.close()
return statement

def update_all(self, dbif=None, execute=True, ident=None):
def update_all(self, dbif=None, execute: bool = True, ident=None):
"""Update the dataset entry in the database from the internal structure
and include None variables.
Expand Down Expand Up @@ -589,7 +589,7 @@ class AbstractDatasetComparisonKeyStartTime:
sorted_map_list = sorted(map_list, key=AbstractDatasetComparisonKeyStartTime)
"""

def __init__(self, obj, *args):
def __init__(self, obj, *args) -> None:
self.obj = obj

def __lt__(self, other):
Expand Down Expand Up @@ -641,7 +641,7 @@ class AbstractDatasetComparisonKeyEndTime:
sorted_map_list = sorted(map_list, key=AbstractDatasetComparisonKeyEndTime)
"""

def __init__(self, obj, *args):
def __init__(self, obj, *args) -> None:
self.obj = obj

def __lt__(self, other):
Expand Down
64 changes: 33 additions & 31 deletions python/grass/temporal/abstract_map_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AbstractMapDataset(AbstractDataset):

__metaclass__ = ABCMeta

def __init__(self):
def __init__(self) -> None:
AbstractDataset.__init__(self)
self.ciface = get_tgis_c_library_interface()

Expand Down Expand Up @@ -167,7 +167,7 @@ def get_map_id(self):
return self.base.get_map_id()

@staticmethod
def split_name(name, layer=None, mapset=None):
def split_name(name: str, layer=None, mapset=None):
"""Convenient method to split a map name into three potentially
contained parts: map name, map layer and mapset. For the layer and
mapset, default keyword arguments can be given if not present in
Expand All @@ -194,9 +194,9 @@ def split_name(name, layer=None, mapset=None):
return name, layer, mapset

@staticmethod
def build_id_from_search_path(name, element):
def build_id_from_search_path(name: str, element) -> str:
"""Convenient method to build the unique identifier while
checking the current seach path for the correct mapset.
checking the current search path for the correct mapset.
Existing mapset definitions in the name string will be reused.
Expand Down Expand Up @@ -225,7 +225,7 @@ def build_id_from_search_path(name, element):
else:
gs.fatal(
_(
"Map <{map_name}> of element tpye '{element}' not found on \
"Map <{map_name}> of element type '{element}' not found on \
search path"
).format(element=element, map_name=name)
)
Expand All @@ -235,7 +235,7 @@ def build_id_from_search_path(name, element):
return f"{name}@{mapset}"

@staticmethod
def build_id(name, mapset, layer=None):
def build_id(name: str, mapset, layer=None) -> str:
"""Convenient method to build the unique identifier
Existing layer and mapset definitions in the name
Expand Down Expand Up @@ -266,15 +266,15 @@ def get_layer(self):
"""
return self.base.get_layer()

def print_self(self):
def print_self(self) -> None:
"""Print the content of the internal structure to stdout"""
self.base.print_self()
self.temporal_extent.print_self()
self.spatial_extent.print_self()
self.metadata.print_self()
self.stds_register.print_self()

def print_info(self):
def print_info(self) -> None:
"""Print information about this object in human readable style"""

if self.get_type() == "raster":
Expand Down Expand Up @@ -322,7 +322,7 @@ def print_info(self):
" +----------------------------------------------------------------------------+" # noqa: E501
)

def print_shell_info(self):
def print_shell_info(self) -> None:
"""Print information about this object in shell style"""
self.base.print_shell_info()
self.temporal_extent.print_shell_info()
Expand All @@ -343,7 +343,7 @@ def print_shell_info(self):
if self.is_topology_build():
self.print_topology_shell_info()

def insert(self, dbif=None, execute=True):
def insert(self, dbif=None, execute: bool = True):
"""Insert the map content into the database from the internal
structure
Expand All @@ -364,7 +364,7 @@ def insert(self, dbif=None, execute=True):
self.write_timestamp_to_grass()
return AbstractDataset.insert(self, dbif=dbif, execute=execute)

def update(self, dbif=None, execute=True):
def update(self, dbif=None, execute: bool = True):
"""Update the map content in the database from the internal structure
excluding None variables
Expand All @@ -383,7 +383,7 @@ def update(self, dbif=None, execute=True):
self.write_timestamp_to_grass()
return AbstractDataset.update(self, dbif, execute)

def update_all(self, dbif=None, execute=True):
def update_all(self, dbif=None, execute: bool = True):
"""Update the map content in the database from the internal structure
including None variables
Expand All @@ -403,15 +403,15 @@ def update_all(self, dbif=None, execute=True):
self.write_timestamp_to_grass()
return AbstractDataset.update_all(self, dbif, execute)

def set_time_to_absolute(self):
def set_time_to_absolute(self) -> None:
"""Set the temporal type to absolute"""
self.base.set_ttype("absolute")

def set_time_to_relative(self):
def set_time_to_relative(self) -> None:
"""Set the temporal type to relative"""
self.base.set_ttype("relative")

def set_absolute_time(self, start_time, end_time=None):
def set_absolute_time(self, start_time, end_time=None) -> bool:
"""Set the absolute time with start time and end time
The end time is optional and must be set to None in case of time
Expand Down Expand Up @@ -501,7 +501,7 @@ def set_absolute_time(self, start_time, end_time=None):

return True

def update_absolute_time(self, start_time, end_time=None, dbif=None):
def update_absolute_time(self, start_time, end_time=None, dbif=None) -> None:
"""Update the absolute time
The end time is optional and must be set to None in case of time
Expand Down Expand Up @@ -543,7 +543,7 @@ def update_absolute_time(self, start_time, end_time=None, dbif=None):
if get_enable_timestamp_write():
self.write_timestamp_to_grass()

def set_relative_time(self, start_time, end_time, unit):
def set_relative_time(self, start_time, end_time, unit) -> bool:
"""Set the relative time interval
The end time is optional and must be set to None in case of time
Expand Down Expand Up @@ -624,7 +624,7 @@ def set_relative_time(self, start_time, end_time, unit):

return True

def update_relative_time(self, start_time, end_time, unit, dbif=None):
def update_relative_time(self, start_time, end_time, unit, dbif=None) -> None:
"""Update the relative time interval
The end time is optional and must be set to None in case of time
Expand Down Expand Up @@ -664,7 +664,7 @@ def update_relative_time(self, start_time, end_time, unit, dbif=None):
if get_enable_timestamp_write():
self.write_timestamp_to_grass()

def set_temporal_extent(self, extent):
def set_temporal_extent(self, extent) -> None:
"""Convenient method to set the temporal extent from a temporal extent
object
Expand Down Expand Up @@ -721,7 +721,7 @@ def set_temporal_extent(self, extent):

self.set_absolute_time(start, end)

def temporal_buffer(self, increment, update=False, dbif=None):
def temporal_buffer(self, increment, update: bool = False, dbif=None) -> None:
"""Create a temporal buffer based on an increment
For absolute time the increment must be a string of type "integer
Expand Down Expand Up @@ -827,7 +827,9 @@ def temporal_buffer(self, increment, update=False, dbif=None):
else:
self.set_relative_time(new_start, new_end, unit)

def set_spatial_extent_from_values(self, north, south, east, west, top=0, bottom=0):
def set_spatial_extent_from_values(
self, north, south, east, west, top=0, bottom=0
) -> None:
"""Set the spatial extent of the map from values
This method only modifies this object and does not commit
Expand All @@ -844,7 +846,7 @@ def set_spatial_extent_from_values(self, north, south, east, west, top=0, bottom
north, south, east, west, top, bottom
)

def set_spatial_extent(self, spatial_extent):
def set_spatial_extent(self, spatial_extent) -> None:
"""Set the spatial extent of the map
This method only modifies this object and does not commit
Expand All @@ -868,7 +870,7 @@ def set_spatial_extent(self, spatial_extent):
"""
self.spatial_extent.set_spatial_extent(spatial_extent)

def spatial_buffer(self, size, update=False, dbif=None):
def spatial_buffer(self, size, update: bool = False, dbif=None) -> None:
"""Buffer the spatial extent by a given size in all
spatial directions.
Expand Down Expand Up @@ -900,7 +902,7 @@ def spatial_buffer(self, size, update=False, dbif=None):
if update:
self.spatial_extent.update(dbif)

def spatial_buffer_2d(self, size, update=False, dbif=None):
def spatial_buffer_2d(self, size, update: bool = False, dbif=None) -> None:
"""Buffer the spatial extent by a given size in 2d
spatial directions.
Expand Down Expand Up @@ -930,7 +932,7 @@ def spatial_buffer_2d(self, size, update=False, dbif=None):
if update:
self.spatial_extent.update(dbif)

def check_for_correct_time(self):
def check_for_correct_time(self) -> bool:
"""Check for correct time
:return: True in case of success, False otherwise
Expand Down Expand Up @@ -970,7 +972,7 @@ def check_for_correct_time(self):

return True

def delete(self, dbif=None, update=True, execute=True):
def delete(self, dbif=None, update: bool = True, execute: bool = True):
"""Delete a map entry from database if it exists
Remove dependent entries:
Expand Down Expand Up @@ -1032,7 +1034,7 @@ def delete(self, dbif=None, update=True, execute=True):

return statement

def unregister(self, dbif=None, update=True, execute=True):
def unregister(self, dbif=None, update: bool = True, execute: bool = True):
"""Remove the map entry in each space time dataset in which this map
is registered
Expand Down Expand Up @@ -1123,7 +1125,7 @@ def get_registered_stds(self, dbif=None, mapset=None):

# this fn should not be in a class for maps,
# but instead in a class for stds: AbstractSpaceTimeDataset ?
def add_stds_to_register(self, stds_id, dbif=None, execute=True):
def add_stds_to_register(self, stds_id, dbif=None, execute: bool = True):
"""Add a new space time dataset to the register
:param stds_id: The id of the space time dataset to be registered
Expand Down Expand Up @@ -1172,7 +1174,7 @@ def add_stds_to_register(self, stds_id, dbif=None, execute=True):

return statement

def remove_stds_from_register(self, stds_id, dbif=None, execute=True):
def remove_stds_from_register(self, stds_id, dbif=None, execute: bool = True):
"""Remove a space time dataset from the register
:param stds_id: The id of the space time dataset to removed from
Expand Down Expand Up @@ -1220,7 +1222,7 @@ def remove_stds_from_register(self, stds_id, dbif=None, execute=True):

return statement

def read_semantic_label_from_grass(self):
def read_semantic_label_from_grass(self) -> None:
"""Read the band identifier of this map from the map metadata
in the GRASS file system based spatial database and
set the internal band identifier that should be insert/updated
Expand All @@ -1230,7 +1232,7 @@ def read_semantic_label_from_grass(self):
silently pass.
"""

def set_semantic_label(self, semantic_label):
def set_semantic_label(self, semantic_label) -> None:
"""Set semantic label identifier
Currently only implemented in RasterDataset. Otherwise
Expand Down
Loading

0 comments on commit 147c005

Please sign in to comment.