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

Add orm.fields.RequiredTextField, etc. for fields which cannot be null #363

Merged
merged 3 commits into from Apr 15, 2024

Conversation

mesozoic
Copy link
Collaborator

@mesozoic mesozoic commented Apr 5, 2024

We have several use cases where we want to

  1. ensure we do not store a null value to Airtable,
  2. squawk loudly if a field value in Airtable happens to be null, and
  3. assume a non-null value for type checking.

Our code winds up being peppered with "if value is None, do something, else ..." conditions, and it would be easier if we could declare that as a type checked property of the field. This branch adds support for that.

For example, given this code:

from pyairtable.orm import Model, fields as F

class User(Model):
    class Meta:
        ...
    user_id = F.RequiredNumberField("ID")

The following will all raise an exception:

>>> User(user_id=None)
Traceback (most recent call last):
  ...
MissingValue: User.user_id does not accept empty values

>>> r = User.from_record(fake_record(ID=123))
>>> r.user_id
123
>>> r.user_id = None
Traceback (most recent call last):
  ...
MissingValue: User.user_id does not accept empty values

>>> r = User.from_record(fake_record(ID=None))
>>> r.user_id
Traceback (most recent call last):
  ...
MissingValue: User.user_id received an empty value

It would have been simpler to implement this as a flag on the field's constructor, but with that approach I couldn't find a way to get mypy to understand that the type of r.user_id is int and not Optional[int].

Copy link

codecov bot commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1040512) to head (e969df6).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #363   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines         2379      2406   +27     
=========================================
+ Hits          2379      2406   +27     

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

@mesozoic mesozoic merged commit 75d64d8 into gtalarico:main Apr 15, 2024
9 checks passed
mesozoic added a commit to mesozoic/pyairtable that referenced this pull request Apr 15, 2024
@mesozoic mesozoic mentioned this pull request Apr 16, 2024
@mesozoic mesozoic deleted the orm_required_fields branch April 16, 2024 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant