Skip to content
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

Rewrite of pyairtable.formulas #329

Merged
merged 6 commits into from
Mar 19, 2024
Merged

Rewrite of pyairtable.formulas #329

merged 6 commits into from
Mar 19, 2024

Conversation

mesozoic
Copy link
Collaborator

@mesozoic mesozoic commented Jan 21, 2024

This is a breaking change to the pyairtable.formulas module, so it would have to be released as 3.0, but it introduces a more thorough way for library users to construct formulas and chain them together.

This module allows for complex nested expressions that are built up through chaining; for example:

>>> formula = match({"Customer": "Alice"})
>>> formula
EQ(Field('Customer'), 'Alice')
>>> complex = formula & Field('Delivery Date').gte(TODAY())
>>> complex
AND(EQ(Field('Customer'), 'Alice'), GTE(Field('Delivery Date'), TODAY()))
>>> str(complex)
"AND({Customer}='Alice', {Delivery Date}>=TODAY())"

The way all of the functions are exposed is a bit janky, and relies on code generation running against a text file that must be refreshed whenever Airtable releases new functions. I'll look to incorporate the approach laid out in #328 for reading those from the formula playground instead.

A summary of changes:

  • formula= kwargs now accept either str or an instance of Formula
  • The new Formula class has subclasses Comparison and FunctionCall
  • The match() function signature remains the same, though it returns a Formula class instead of str
  • to_airtable_value() is removed, and replaced with a similar (but not same) to_formula_str()
  • field_name() replaces the FIELD() function
  • EQ, NE, GT, GTE, LT, LTE replace equivalent functions (EQUAL(), etc.)
  • Functions like AND() and OR() now return Comparison (a subclass of Formula) instead of str
  • All Airtable formula functions are exposed, and return FunctionCall (a subclass of Formula)
  • ORM fields descriptors contain .eq(), etc. convenience methods for producing formulas.

Resolves #304
Resolves #305
Resolves #324
Resolves #328

Copy link

codecov bot commented Jan 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.95%. Comparing base (b676bf0) to head (be844ec).

❗ Current head be844ec differs from pull request most recent head 2744c67. Consider uploading reports for the commit 2744c67 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #329      +/-   ##
==========================================
+ Coverage   99.60%   99.95%   +0.35%     
==========================================
  Files          25       25              
  Lines        2025     2307     +282     
==========================================
+ Hits         2017     2306     +289     
+ Misses          8        1       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mesozoic mesozoic merged commit 7e7c853 into gtalarico:main Mar 19, 2024
7 checks passed
@mesozoic mesozoic deleted the formulas branch March 19, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant