Materials-Consortia / Materials-Consortia/optimade-python-tools
Smarter error messages from the filtertransformer
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 91
- Forks
- 54
- PR merge metrics
- No merged PRs in 30d
Description
Consider the following gotcha:
The references endpoint has a year field which could be useful to query on. Unfortunately in our interpretation of BiBTeX, all fields are strings, so the queries
example.org/v1/references/?filter=year > 2015 or example.org/v1/references?filter=year = 2015
return empty results (for the MongoTransformer, at least).
Another example would be the wrong value type in the comparison, e.g. querying filtering a list for equality with an integer or a string:
example.org/v1/structures?filter = elements = "Ag,Cl"
which also just returns empty results without an error.
This may be specific to the MongoDB backend as elasticsearch is much more strongly typed.
Soon we will hopefully have field data types be accessible to the transformer via the mapper (see upcoming PR). We could then review/use the rules from the validator on which data types can be queried with which operators to provide better error messages from the base transformer directly (pseudo-code below). Likewise with the types of the values themselves.
class BaseTransformer(lark.Transformer):
def property_first_comparison(self, prop, op, value):
if op not in self.allowed_ops[self.get_type(prop)]:
raise BadRequest(f"Cannot apply filter with {op} on {prop})
if not isinstance(value, self.get_type(prop)):
try:
value = self.get_type(prop)(value)
except ValueError:
raise BadRequest(f"Cannot coerce {value} into property type")
class MongoTransformer(BaseTransformer):
def property_first_comparison(self, prop, op, value):
super().property_first_comparison(prop, op, value) # is there a shortcut for super().<current_fn>?
# do actual transformation
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the BaseTransformer and MongoTransformer comparison handling, then review how the mapper exposes field types and how the validator defines allowed operators. Done means invalid operators and value types produce clear errors, while valid values are coerced or transformed correctly across the relevant backend.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100