-
Notifications
You must be signed in to change notification settings - Fork 13.8k
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
feat: add decorator to guard public APIs #12635
Conversation
b310ffc
to
5759363
Compare
I know hashing is fast for most cases, but I'm wondering whether we can move this to be an offline job in some way? I.e. instead of a decorator, store the hash for all public APIs in a separate file and run the checks in CI. |
Good point... we could also just add unit tests that import the functions and compare their signature hashes to hard coded values, that might be a more efficient approach. (Also, the hashes are computed just once when the files are "compiled", not when the functions are called.) |
Codecov Report
@@ Coverage Diff @@
## master #12635 +/- ##
==========================================
- Coverage 66.80% 63.35% -3.45%
==========================================
Files 1017 487 -530
Lines 49730 30014 -19716
Branches 4864 0 -4864
==========================================
- Hits 33221 19016 -14205
+ Misses 16386 10998 -5388
+ Partials 123 0 -123
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
I reviewed
|
cdaf06b
to
be5077d
Compare
be5077d
to
47214aa
Compare
29bf532
to
0be108a
Compare
@pytest.mark.parametrize("interface,expected_hash", list(hashes.items())) | ||
def test_public_interfaces(interface, expected_hash): | ||
"""Test that public interfaces have not been accidentally changed.""" | ||
current_hash = compute_hash(interface) | ||
assert current_hash == expected_hash, get_warning_message(interface, current_hash) |
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.
@ktmud this is what I had in mind; now we can just update hashes
and each one will get its own unit test.
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.
Nice. Not that it matters much, but can pytest display which parameter failed the test in case of assertion errors?
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.
It does, it treats each parameter as a separate test called unit_test_name[parameters]
: https://docs.pytest.org/en/stable/parametrize.html#pytest-mark-parametrize
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.
Should we clean up the decorator?
We should. I'm emotionally attached to it, but I'll remove it. :-P |
d12e0ed
to
beef427
Compare
EDIT
Instead of the decorator approach described below I implemented the guard as a custom unit test.
SUMMARY
This PR adds a decorator called
guard
, used to protect public APIs from breaking changes without a major version bump (see #12566). If the API is changed the user receives a warning.For example, we allow people to specify a custom function called
SQL_QUERY_MUTATOR
in theirsuperset_config.py
. This API is already broken, since the signature described insuperset/config.py
is out-of-sync with how it's actually called:If we look at where the function is called we can see that the signature should be slightly different:
Notice the extra
database
argument, and the small difference betweenusername
anduser_name
. This was probably changed at some point without updating the config, breaking any existingSQL_QUERY_MUTATOR
that don't expect thedatabase
argument!To prevent this in the future, we create a dummy function and protect it:
We change the code a little bit so that the function is always called (either the custom or the dummy one):
Now what happens if we change the signature of the function? Let's try it:
Changing the code like this will print the following warning:
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
TEST PLAN
Changed signature and run unit tests, it printed the warning message above.
ADDITIONAL INFORMATION