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

Allow ORM Meta options to be lookups at runtime (not import time) #268

Merged
merged 1 commit into from Jul 31, 2023

Conversation

mesozoic
Copy link
Collaborator

@mesozoic mesozoic commented Jun 6, 2023

This allows the Meta class inside each ORM model to provide a callable that will be executed whenever the ORM requests one of its meta properties. The use case we have is to be able to provide API keys via global configuration singletons or environment variables which might not be loaded at import time but will be present at runtime.

Resolves #192

@mesozoic mesozoic requested a review from gtalarico June 6, 2023 00:03
@codecov
Copy link

codecov bot commented Jun 6, 2023

Codecov Report

Merging #268 (3efa2d6) into main (c08963e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 3efa2d6 differs from pull request most recent head dd46375. Consider uploading reports for the commit dd46375 to get more accurate results

@@           Coverage Diff           @@
##             main     #268   +/-   ##
=======================================
  Coverage   95.74%   95.75%           
=======================================
  Files          18       18           
  Lines         917      919    +2     
=======================================
+ Hits          878      880    +2     
  Misses         39       39           
Files Changed Coverage Δ
pyairtable/orm/model.py 97.56% <100.00%> (+0.03%) ⬆️

@@ -171,6 +187,8 @@ def _get_meta(cls, name: str, default: Any = None, required: bool = False) -> An
if required and not hasattr(cls.Meta, name):
raise ValueError(f"{cls.__name__}.Meta.{name} must be defined")
value = getattr(cls.Meta, name, default)
if callable(value):

Choose a reason for hiding this comment

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

Beyond a nit and this particular usage is already very concise so feel free to completely disregard, PR looks great but was poking around in Python 3.10 docs and this seems like a case where match (ref PEP 634/635/636 could be used in a world where 3.10 is the minimum supported version ie:

match value:
    case callable_func := callable(value):
        value = callable_func()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will admit to not having my head wrapped around the match syntax (or soft keywords in general) but I think it's fair to say we shouldn't use it if it's not present (or available via __future__) in all supported Python versions.

@mesozoic mesozoic merged commit a3b471a into gtalarico:main Jul 31, 2023
6 checks passed
@mesozoic mesozoic deleted the runtime_orm_meta branch July 31, 2023 16:23
@mesozoic mesozoic mentioned this pull request Jul 31, 2023
75 tasks
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.

ORM specify configuration and API key outside the code
2 participants