Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
#135 better type hinting for operators
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwunderbar666 committed Feb 18, 2023
1 parent 45c4b31 commit 14e21e6
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions flaskinventory/flaskdgraph/dgraph_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
May later be used for automatic query building
"""

from typing import Union, Any
from typing import Union, Any, Literal
import datetime
import json
from copy import deepcopy
Expand All @@ -31,6 +31,13 @@
from wtforms.validators import DataRequired, Optional


"""
Type Hints
"""

AvailableOperators = Literal['eq', 'between', 'gt', 'lt', 'ge', 'le']
AvailableConnectors = Literal['AND', 'OR', 'NOT']

"""
DGraph Primitives
"""
Expand Down Expand Up @@ -158,7 +165,7 @@ def corece(self, val) -> Any:
except:
return None

def query_filter(self, vals: Union[str, list], operator=None, predicate=None, **kwargs) -> str:
def query_filter(self, vals: Union[str, list], operator: AvailableOperators=None, predicate=None, **kwargs) -> str:

if vals is None:
return None
Expand Down Expand Up @@ -346,7 +353,7 @@ def default(self):
return self._default

@property
def label(self):
def label(self) -> str:
if self._label:
return self._label
else:
Expand Down Expand Up @@ -382,7 +389,7 @@ def validate(self, data, facets=None, **kwargs):
else:
return data

def query_filter(self, vals: Union[str, list], predicate=None, operator=None, connector=None, **kwargs) -> str:
def query_filter(self, vals: Union[str, list], predicate=None, operator: AvailableOperators=None, connector: AvailableConnectors=None, **kwargs) -> str:

if not predicate:
predicate = self.predicate
Expand Down Expand Up @@ -1117,7 +1124,7 @@ def query_field(self) -> IntegerField:
render_kw = {**self.render_kw, **render_kw}
return IntegerField(label=self.query_label, render_kw=self.render_kw)

def query_filter(self, vals: Union[str, list, int], operator: Union['lt', 'gt'] = None, **kwargs):
def query_filter(self, vals: Union[str, list, int], operator: Literal['lt', 'gt'] = None, **kwargs) -> str:
if vals is None:
return f'has({self.predicate})'

Expand Down Expand Up @@ -1195,7 +1202,7 @@ def validation_hook(self, data):
raise InventoryValidationError(
f'Cannot evaluate provided value as bool: {data}!')

def query_filter(self, vals, **kwargs):
def query_filter(self, vals, **kwargs) -> str:
if isinstance(vals, list):
vals = vals[0]

Expand Down

0 comments on commit 14e21e6

Please sign in to comment.