-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feature: Add set/zip function to contexts #5107
Conversation
Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide. |
@jtcohen6 any thoughts on adding any of the others mentioned in the issue (i.e.
Looks like my black formatter ignored the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far. Sorry it took a bit to review! We've been having some conversations around the best pattern for functions that we add.
Did you want to try creating the "try_set" methods that @emmyoop asked for? |
Hey @gshank yep keen to do that but kind of waiting to see if we will be adding a new special error type to raise (#5167). You reckon I should just proceed with a generic def try_set(value):
try:
return set(value)
except TypeError:
raise TypeError |
It looks like we mainly raise CompilationExceptions in the contexts, partly because Jinja catches the exceptions and we don't have code to distinguish between parse-time and run-time for those errors. For now, just make it a CompilationException? We plan to do some work on organizing exceptions at some point and we can clean it up then. |
@gshank / @emmyoop what do you think of the
|
@jeremyyeo the |
Looks like this is getting really close :-). If you can just add a couple of simple tests we can get this wrapped up. Probably a test in tests/functional/context_methods would be good; just something simple like a model that uses the functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some test changes but otherwise is looking great!
{% set set_result = set([1, 2, 2, 3, "foo", False]) %} | ||
{% set try_set_result = try_set([1, 2, 2, 3, "foo", False]) %} | ||
|
||
{% set simple_set = (set_result == try_set_result == set((1, 2, 3, "foo", False))) %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is a bit off since you're using the new method to check the new method.
Instead of using tests I would use macros that log the values of set_result
andtry_set_result
. Something like:
{% macro validate_set() %}
{% set set_result = set([1, 2, 2, 3, 'foo', False]) %}
{{ log("set_result: " ~ set_result) }}
{% set try_set_result = try_set([1, 2, 2, 3, 'foo', False]) %}
{{ log("try_set_result: " ~ try_set_result) }}
{% endmacro %}
Then in the test you can capture the logs from run-operation
with the --debug
flag for the macro and assert the logs show up:
def test_builtin_set_function(self, project):
_, log_output = run_dbt_and_capture(["--debug", "run-operation", "validate_set"])
expected_set = {False, 1, 2, 3, 'foo'}
assert f"set_result: {expected_set}" in log_output
assert f"try_set_result: {expected_set}" in log_output
Also do the same for zip for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great @jeremyyeo! Thanks for working with us on the few iterations as we move towards a better way to implement these methods.
I'll get it merged in!
@jtcohen6 should this be back ported to |
* add set function to contexts * add zip function to contexts * add changelog * add try_ equivalents * remove defaults * add tests * update tests
Any chance of this making it into 1.2? |
@skwaugh This will be in v1.2! @jeremyyeo @emmyoop I'm working on docs for this right now (dbt-labs/docs.getdbt.com#1635), and it looks like some of the docstrings need updating. (E.g. all methods include One other note: To my mind, the implementation of
As a dbt user, and as someone familiar with those SQL methods, that's what I was expecting those methods to do, too. I'm also struggling to come up with a working example where
But this just raises:
|
Ah yes - thanks for catching that :)
Yeah I think so too. However, at the same time, I'm pretty sure we were trying to align these new functions (in terms of whether dbt-core/core/dbt/context/base.py Lines 463 to 465 in 3594ced
Which has no
Non-iters seem to raise TypeError in Python... I think it's because:
And |
@jeremyyeo Thanks for the quick response! And good call on the
I'm catching myself back up on these threads from a few months ago, and just dropped a few thoughts in #5167 (comment). I recall agreeing at the time that:
My strong preference is to get this right going forward, by giving the right functionality to the right name. Two options for us right now (while we're in the v1.2 RC period):
@emmyoop @nathaniel-may open to your thoughts as well! |
@nathaniel-may and I had a chance to discuss live. We're definitely lacking a larger "language design" discussion and spec which would make it easier for community members to contribute, and for us to review, these types of changes with confidence. If he's interested, I think @dbeatty10 might be able to help with this :) We don't have the bandwidth to do that in the immediate term, but it's definitely worth doing. In the meantime, let's move forward with one of the short-term fixes identified above, to include in v1.2.0-rc2, just to avoid shipping functionality that does the exact opposite of what (I suspect) many users will expect. |
@jtcohen6 I'd lean toward option 2 to avoid breaking users until we've figured out the standard we want to live by. |
@emmyoop Okay, I buy it! If we choose to adopt @jeremyyeo I can pick this up, unless you have the motivation and capacity to see it through. We'd just need to get the change merged + backported by next Tuesday. |
minor note: would you consider Clarification question: If I see a function with |
👍 good call! all about it
As I understand it, "strict" methods:
Whereas the "non-strict" versions of those methods return IIRC, we had previously discussed (One thought: Should it be |
@jtcohen6 Took a crack at that tags/v1.2.0rc1...rename-strict-mode Should we do |
@jeremyyeo That branch looks great! Want to open a PR for it? I just opened a dedicated issue for this switch: #5475 The timeline to do |
resolves #2345
Description
Adding the
set
andzip
functions as prescribed in the issue linked above.Checklist