-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5759363
commit 47214aa
Showing
4 changed files
with
122 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from base64 import b85encode | ||
from hashlib import md5 | ||
from inspect import ( | ||
getmembers, | ||
getsourcefile, | ||
getsourcelines, | ||
isclass, | ||
isfunction, | ||
isroutine, | ||
signature, | ||
) | ||
from textwrap import indent | ||
from typing import Any, Callable | ||
|
||
|
||
def compute_hash(decorated: Callable[..., Any]) -> str: | ||
if isfunction(decorated): | ||
return compute_func_hash(decorated) | ||
|
||
if isclass(decorated): | ||
return compute_class_hash(decorated) | ||
|
||
raise Exception(f"Invalid decorated object: {decorated}") | ||
|
||
|
||
def compute_func_hash(function: Callable[..., Any]) -> str: | ||
hashed = md5() | ||
hashed.update(function.__name__.encode()) | ||
hashed.update(str(signature(function)).encode()) | ||
return b85encode(hashed.digest()).decode("utf-8") | ||
|
||
|
||
def compute_class_hash(class_: Callable[..., Any]) -> str: | ||
hashed = md5() | ||
public_methods = { | ||
method | ||
for name, method in getmembers(class_, predicate=isroutine) | ||
if not name.startswith("_") or name == "__init__" | ||
} | ||
for method in public_methods: | ||
hashed.update(method.__name__.encode()) | ||
hashed.update(str(signature(method)).encode()) | ||
return b85encode(hashed.digest()).decode("utf-8") | ||
|
||
|
||
def get_warning_message(decorated: Callable[..., Any], expected_hash: str) -> str: | ||
sourcefile = getsourcefile(decorated) | ||
sourcelines = getsourcelines(decorated) | ||
code = indent("".join(sourcelines[0]), " ") | ||
lineno = sourcelines[1] | ||
return ( | ||
f"The decorated object `{decorated.__name__}` (in {sourcefile} " | ||
f"line {lineno}) has a public interface which has currently been " | ||
"modified. This MUST only be released in a new major version of " | ||
"Superset according to SIP-57. To remove this warning message " | ||
f"update the associated hash to '{expected_hash}'.\n\n{code}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=no-self-use | ||
from superset.sql_lab import dummy_sql_query_mutator | ||
from superset.utils.public_interfaces import compute_hash, get_warning_message | ||
from tests.base_tests import SupersetTestCase | ||
|
||
# These are public interfaces exposed by Superset. Make sure | ||
# to only change the interfaces and update the hashes in new | ||
# major versions of Superset. | ||
hashes = { | ||
dummy_sql_query_mutator: "?1Y~;3l_|ss3^<`P;lWt", | ||
} | ||
|
||
|
||
class PublicInterfacesTest(SupersetTestCase): | ||
|
||
"""Test that public interfaces have not been accidentally changed.""" | ||
|
||
def test_dummy_sql_query_mutator(self): | ||
for interface, expected_hash in hashes.items(): | ||
current_hash = compute_hash(dummy_sql_query_mutator) | ||
assert current_hash == expected_hash, get_warning_message( | ||
dummy_sql_query_mutator, current_hash | ||
) |