Skip to content

Commit

Permalink
Issue #460 avoid string (return) annotations
Browse files Browse the repository at this point in the history
PEP 563: Postponed Evaluation of Annotations
  • Loading branch information
soxofaan committed Aug 28, 2023
1 parent 7518a10 commit 10b3491
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 70 deletions.
16 changes: 9 additions & 7 deletions openeo/api/process.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import warnings
from typing import Union

Expand Down Expand Up @@ -35,7 +37,7 @@ def to_dict(self) -> dict:
return d

@classmethod
def raster_cube(cls, name: str = "data", description: str = "A data cube.") -> 'Parameter':
def raster_cube(cls, name: str = "data", description: str = "A data cube.") -> Parameter:
"""
Helper to easily create a 'raster-cube' parameter.
Expand All @@ -46,7 +48,7 @@ def raster_cube(cls, name: str = "data", description: str = "A data cube.") -> '
return cls(name=name, description=description, schema={"type": "object", "subtype": "raster-cube"})

@classmethod
def datacube(cls, name: str = "data", description: str = "A data cube.") -> "Parameter":
def datacube(cls, name: str = "data", description: str = "A data cube.") -> Parameter:
"""
Helper to easily create a 'datacube' parameter.
Expand All @@ -59,7 +61,7 @@ def datacube(cls, name: str = "data", description: str = "A data cube.") -> "Par
return cls(name=name, description=description, schema={"type": "object", "subtype": "datacube"})

@classmethod
def string(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED, values=None) -> 'Parameter':
def string(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED, values=None) -> Parameter:
"""Helper to create a 'string' type parameter."""
schema = {"type": "string"}
if values is not None:
Expand All @@ -68,21 +70,21 @@ def string(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED,


@classmethod
def integer(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> 'Parameter':
def integer(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> Parameter:
"""Helper to create a 'integer' type parameter."""
return cls(name=name, description=description, schema={"type": "integer"}, default=default)

@classmethod
def number(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> 'Parameter':
def number(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> Parameter:
"""Helper to create a 'number' type parameter."""
return cls(name=name, description=description, schema={"type": "number"}, default=default)

@classmethod
def boolean(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> 'Parameter':
def boolean(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> Parameter:
"""Helper to create a 'boolean' type parameter."""
return cls(name=name, description=description, schema={"type": "boolean"}, default=default)

@classmethod
def array(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> 'Parameter':
def array(cls, name: str, description: str = None, default=_DEFAULT_UNDEFINED) -> Parameter:
"""Helper to create a 'array' type parameter."""
return cls(name=name, description=description, schema={"type": "array"}, default=default)
9 changes: 5 additions & 4 deletions openeo/capabilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import contextlib
from abc import ABC
import re
from typing import Union, Tuple

from abc import ABC
from typing import Tuple, Union

# TODO Is this base class (still) useful?

Expand All @@ -24,7 +25,7 @@ def api_version(self) -> str:
raise NotImplementedError

@property
def api_version_check(self) -> 'ComparableVersion':
def api_version_check(self) -> ComparableVersion:
"""Helper to easily check if the API version is at least or below some threshold version."""
api_version = self.api_version()
if not api_version:
Expand Down
8 changes: 5 additions & 3 deletions openeo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"""

from __future__ import annotations

import logging
import os
import platform
from configparser import ConfigParser
from copy import deepcopy
from pathlib import Path
from typing import Union, Any, Sequence, Iterator, Optional, List
from typing import Any, Iterator, List, Optional, Sequence, Union

from openeo.util import in_interactive_mode

Expand Down Expand Up @@ -116,13 +118,13 @@ def get(self, key: Union[str, Sequence[str]], default=None) -> Any:
# TODO: option to cast/convert to certain type?
return self._config.get(self._key(key), default)

def load_ini_file(self, path: Union[str, Path]) -> "ClientConfig":
def load_ini_file(self, path: Union[str, Path]) -> ClientConfig:
cp = ConfigParser()
read_ok = cp.read(path)
self._sources.extend(read_ok)
return self.load_config_parser(cp)

def load_config_parser(self, parser: ConfigParser) -> "ClientConfig":
def load_config_parser(self, parser: ConfigParser) -> ClientConfig:
for section in parser.sections():
for option, value in parser.items(section=section):
self._set(key=(section, option), value=value)
Expand Down
9 changes: 6 additions & 3 deletions openeo/internal/graph_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Functionality for abstracting, building, manipulating and processing openEO process graphs.
"""

from __future__ import annotations

import abc
import collections
import json
Expand Down Expand Up @@ -78,7 +81,7 @@ class _FromNodeMixin(abc.ABC):
"""Mixin for classes that want to hook into the generation of a "from_node" reference."""

@abc.abstractmethod
def from_node(self) -> "PGNode":
def from_node(self) -> PGNode:
# TODO: "from_node" is a bit a confusing name:
# it refers to the "from_node" node reference in openEO process graphs,
# but as a method name here it reads like "construct from PGNode",
Expand Down Expand Up @@ -203,7 +206,7 @@ def to_process_graph_argument(value: Union['PGNode', str, dict]) -> dict:
raise ValueError(value)

@staticmethod
def from_flat_graph(flat_graph: dict, parameters: Optional[dict] = None) -> 'PGNode':
def from_flat_graph(flat_graph: dict, parameters: Optional[dict] = None) -> PGNode:
"""Unflatten a given flat dict representation of a process graph and return result node."""
return PGNodeGraphUnflattener.unflatten(flat_graph=flat_graph, parameters=parameters)

Expand Down Expand Up @@ -259,7 +262,7 @@ def dimension(self):
def reducer_process_graph(self) -> PGNode:
return self.arguments["reducer"]["process_graph"]

def clone_with_new_reducer(self, reducer: PGNode) -> 'ReduceNode':
def clone_with_new_reducer(self, reducer: PGNode) -> ReduceNode:
"""Copy/clone this reduce node: keep input reference, but use new reducer"""
return ReduceNode(
data=self.arguments["data"]["from_node"],
Expand Down
6 changes: 4 additions & 2 deletions openeo/internal/process_graph_visitor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import json
from abc import ABC
from typing import Union, Tuple, Any
from typing import Any, Tuple, Union

from openeo.internal.warnings import deprecated
from openeo.rest import OpenEoClientException
Expand Down Expand Up @@ -63,7 +65,7 @@ def resolve_from_node(process_graph, node, from_node):
raise ProcessGraphVisitException("No result node in process graph: " + dump[:1000])
return result_node

def accept_process_graph(self, graph: dict) -> 'ProcessGraphVisitor':
def accept_process_graph(self, graph: dict) -> ProcessGraphVisitor:
"""
Traverse a (flat) process graph
Expand Down
18 changes: 10 additions & 8 deletions openeo/internal/processes/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Functionality and tools to process openEO processes.
For example: parse a bunch of JSON descriptions and generate Python (stub) functions.
"""
from __future__ import annotations

import json
from pathlib import Path
from typing import List, Union, Iterator
from typing import Iterator, List, Union

import requests

Expand All @@ -16,7 +18,7 @@ def __init__(self, schema: Union[dict, list]):
self.schema = schema

@classmethod
def from_dict(cls, data: dict) -> 'Schema':
def from_dict(cls, data: dict) -> Schema:
return cls(schema=data)


Expand All @@ -34,7 +36,7 @@ def __init__(self, name: str, description: str, schema: Schema, default=NO_DEFAU
self.optional = optional

@classmethod
def from_dict(cls, data: dict) -> 'Parameter':
def from_dict(cls, data: dict) -> Parameter:
return cls(
name=data["name"], description=data["description"], schema=Schema.from_dict(data["schema"]),
default=data.get("default", cls.NO_DEFAULT), optional=data.get("optional", False)
Expand All @@ -52,7 +54,7 @@ def __init__(self, description: str, schema: Schema):
self.schema = schema

@classmethod
def from_dict(cls, data: dict) -> 'Returns':
def from_dict(cls, data: dict) -> Returns:
return cls(description=data["description"], schema=Schema.from_dict(data["schema"]))


Expand All @@ -71,7 +73,7 @@ def __init__(
# TODO: more properties?

@classmethod
def from_dict(cls, data: dict) -> 'Process':
def from_dict(cls, data: dict) -> Process:
"""Construct openEO process from dictionary values"""
return cls(
id=data["id"],
Expand All @@ -82,17 +84,17 @@ def from_dict(cls, data: dict) -> 'Process':
)

@classmethod
def from_json(cls, data: str) -> 'Process':
def from_json(cls, data: str) -> Process:
"""Parse openEO process JSON description."""
return cls.from_dict(json.loads(data))

@classmethod
def from_json_url(cls, url: str) -> 'Process':
def from_json_url(cls, url: str) -> Process:
"""Parse openEO process JSON description from given URL."""
return cls.from_dict(requests.get(url).json())

@classmethod
def from_json_file(cls, path: Union[str, Path]) -> 'Process':
def from_json_file(cls, path: Union[str, Path]) -> Process:
"""Parse openEO process JSON description file."""
with Path(path).open("r") as f:
return cls.from_json(f.read())
Expand Down
18 changes: 9 additions & 9 deletions openeo/rest/_datacube.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import pathlib
import re
Expand Down Expand Up @@ -33,7 +35,7 @@ class _ProcessGraphAbstraction(_FromNodeMixin, FlatGraphableMixin):
raster data cubes, vector cubes, ML models, ...
"""

def __init__(self, pgnode: PGNode, connection: "Connection"):
def __init__(self, pgnode: PGNode, connection: Connection):
self._pg = pgnode
self._connection = connection

Expand All @@ -60,7 +62,7 @@ def _api_version(self):
return self._connection.capabilities().api_version_check

@property
def connection(self) -> "Connection":
def connection(self) -> Connection:
return self._connection

def result_node(self) -> PGNode:
Expand Down Expand Up @@ -162,7 +164,7 @@ def __init__(
def __repr__(self):
return f"<{type(self).__name__} runtime={self._runtime!r} code={str_truncate(self.code, width=200)!r}>"

def get_runtime(self, connection: "openeo.Connection") -> str:
def get_runtime(self, connection: Connection) -> str:
return self._runtime or self._guess_runtime(connection=connection)

@classmethod
Expand All @@ -172,7 +174,7 @@ def from_file(
runtime: Optional[str] = None,
version: Optional[str] = None,
context: Optional[dict] = None,
) -> "UDF":
) -> UDF:
"""
Load a UDF from a local file.
Expand All @@ -197,7 +199,7 @@ def from_url(
runtime: Optional[str] = None,
version: Optional[str] = None,
context: Optional[dict] = None,
) -> "UDF":
) -> UDF:
"""
Load a UDF from a URL.
Expand All @@ -216,7 +218,7 @@ def from_url(
code=code, runtime=runtime, version=version, context=context, _source=url
)

def _guess_runtime(self, connection: "openeo.Connection") -> str:
def _guess_runtime(self, connection: Connection) -> str:
"""Guess UDF runtime from UDF source (path) or source code."""
# First, guess UDF language
language = None
Expand Down Expand Up @@ -250,9 +252,7 @@ def _guess_runtime_from_suffix(self, suffix: str) -> Union[str]:
".r": "R",
}.get(suffix.lower())

def get_run_udf_callback(
self, connection: "openeo.Connection", data_parameter: str = "data"
) -> PGNode:
def get_run_udf_callback(self, connection: Connection, data_parameter: str = "data") -> PGNode:
"""
For internal use: construct `run_udf` node to be used as callback in `apply`, `reduce_dimension`, ...
"""
Expand Down
4 changes: 3 additions & 1 deletion openeo/rest/auth/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""

from __future__ import annotations

import base64
import contextlib
import enum
Expand Down Expand Up @@ -272,7 +274,7 @@ def __init__(
self.default_clients = default_clients

@classmethod
def from_dict(cls, data: dict) -> "OidcProviderInfo":
def from_dict(cls, data: dict) -> OidcProviderInfo:
return cls(
provider_id=data["id"], title=data["title"],
issuer=data["issuer"],
Expand Down
12 changes: 7 additions & 5 deletions openeo/rest/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Helpers for data conversions between Python ecosystem data types and openEO data structures.
"""

from __future__ import annotations

import typing

import numpy as np
Expand Down Expand Up @@ -94,28 +96,28 @@ def timeseries_json_to_pandas(timeseries: dict, index: str = "date", auto_collap


@deprecated("Use :py:meth:`XarrayDataCube.from_file` instead.", version="0.7.0")
def datacube_from_file(filename, fmt='netcdf') -> "XarrayDataCube":
def datacube_from_file(filename, fmt="netcdf") -> XarrayDataCube:
from openeo.udf.xarraydatacube import XarrayDataCube
return XarrayDataCube.from_file(path=filename, fmt=fmt)


@deprecated("Use :py:meth:`XarrayDataCube.save_to_file` instead.", version="0.7.0")
def datacube_to_file(datacube: "XarrayDataCube", filename, fmt='netcdf'):
def datacube_to_file(datacube: XarrayDataCube, filename, fmt="netcdf"):
return datacube.save_to_file(path=filename, fmt=fmt)


@deprecated("Use :py:meth:`XarrayIO.to_json_file` instead", version="0.7.0")
def _save_DataArray_to_JSON(filename, array: "xarray.DataArray"):
def _save_DataArray_to_JSON(filename, array: xarray.DataArray):
from openeo.udf.xarraydatacube import XarrayIO
return XarrayIO.to_json_file(array=array, path=filename)


@deprecated("Use :py:meth:`XarrayIO.to_netcdf_file` instead", version="0.7.0")
def _save_DataArray_to_NetCDF(filename, array: "xarray.DataArray"):
def _save_DataArray_to_NetCDF(filename, array: xarray.DataArray):
from openeo.udf.xarraydatacube import XarrayIO
return XarrayIO.to_netcdf_file(array=array, path=filename)


@deprecated("Use :py:meth:`XarrayDataCube.plot` instead.", version="0.7.0")
def datacube_plot(datacube: "XarrayDataCube", *args, **kwargs):
def datacube_plot(datacube: XarrayDataCube, *args, **kwargs):
datacube.plot(*args, **kwargs)
Loading

0 comments on commit 10b3491

Please sign in to comment.