Skip to content

Add some Python types to the base provider #2040

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
43 changes: 31 additions & 12 deletions pygeoapi/provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import logging
from enum import Enum
from http import HTTPStatus
from typing import Literal, NotRequired, Optional, TypedDict

from pygeoapi.error import GenericError

Expand All @@ -43,11 +44,29 @@ class SchemaType(Enum):
update = 'update'
replace = 'replace'

# All the potential properties on a field
# as defined by the OGC API features spec
FieldProperties = TypedDict(
"FieldProperties",
{
"type": Literal["string", "number", "integer", "boolean", "object", "array"],
"title": str,
"description": str,
"format": NotRequired[str],
"x-ogc-unit": NotRequired[str],
"x-ogc-role": NotRequired[str],
"enum": NotRequired[list[str]],
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webb-ben does this seem like the correct interpretation of the queryables spec? https://docs.ogc.org/is/19-079r2/19-079r2.html#queryables

It appears there are some other properties related to uuid's / uri's but I have never actually seen these in practice. So as such not sure if they should be on the properties type.
image

)

# Dict type representing a mapping of the field
# to its associated data type
FieldMapping = dict[str, FieldProperties]

class BaseProvider:
"""generic Provider ABC"""

def __init__(self, provider_def):
def __init__(self, provider_def: dict):
"""
Initialize object

Expand All @@ -73,15 +92,15 @@ def __init__(self, provider_def):
self.title_field = provider_def.get('title_field')
self.properties = provider_def.get('properties', [])
self.file_types = provider_def.get('file_types', [])
self._fields = {}
self._fields: FieldMapping = {}
self.filename = None

# for coverage providers
self.axes = []
self.crs = None
self.num_bands = None

def get_fields(self):
def get_fields(self) -> FieldMapping:
"""
Get provider field information (names, types)

Expand All @@ -94,7 +113,7 @@ def get_fields(self):
raise NotImplementedError()

@property
def fields(self) -> dict:
def fields(self) -> FieldMapping:
"""
Store provider field information (names, types)

Expand All @@ -110,7 +129,7 @@ def fields(self) -> dict:
else:
return self.get_fields()

def get_schema(self, schema_type: SchemaType = SchemaType.item):
def get_schema(self, schema_type: SchemaType = SchemaType.item) -> tuple:
"""
Get provider schema model

Expand All @@ -122,7 +141,7 @@ def get_schema(self, schema_type: SchemaType = SchemaType.item):

raise NotImplementedError()

def get_data_path(self, baseurl, urlpath, dirpath):
def get_data_path(self, baseurl: str , urlpath: str, dirpath: str) -> dict:
"""
Gets directory listing or file description or raw file dump

Expand All @@ -145,7 +164,7 @@ def get_metadata(self):

raise NotImplementedError()

def get_domains(self, properties=[], current=False):
def get_domains(self, properties: list[str] = [], current=False):
"""
Get domains from dataset

Expand All @@ -168,7 +187,7 @@ def query(self):

raise NotImplementedError()

def get(self, identifier, **kwargs):
def get(self, identifier: str, **kwargs):
"""
query the provider by id

Expand All @@ -179,7 +198,7 @@ def get(self, identifier, **kwargs):

raise NotImplementedError()

def create(self, item):
def create(self, item: dict) -> str:
"""
Create a new item

Expand All @@ -190,7 +209,7 @@ def create(self, item):

raise NotImplementedError()

def update(self, identifier, item):
def update(self, identifier: str, item: dict) -> bool:
"""
Updates an existing item

Expand All @@ -202,7 +221,7 @@ def update(self, identifier, item):

raise NotImplementedError()

def delete(self, identifier):
def delete(self, identifier: str) -> bool:
"""
Deletes an existing item

Expand All @@ -213,7 +232,7 @@ def delete(self, identifier):

raise NotImplementedError()

def _load_and_prepare_item(self, item, identifier=None,
def _load_and_prepare_item(self, item: str, identifier: Optional[str]=None,
accept_missing_identifier=False,
raise_if_exists=True):
"""
Expand Down
Loading