def _write_checkpoint_script_to_disk( context_directory: str, checkpoint_name: str, script_path: str ) -> None: script_full_path = os.path.abspath(os.path.join(script_path)) template = _load_script_template().format(checkpoint_name, context_directory) linted_code = lint_code(template) with open(script_full_path, "w") as f: f.write(linted_code)
def add_code_cell(self, code: str, lint: bool = False) -> None: """ Add the given code as a new code cell. """ if lint: code = lint_code(code).rstrip("\n") cell = nbformat.v4.new_code_cell(code) self._notebook["cells"].append(cell)
def _write_tap_file_to_disk(batch_kwargs, context_directory, suite, tap_filename): tap_file_path = os.path.abspath(os.path.join(context_directory, "..", tap_filename)) template = _load_template().format( tap_filename, context_directory, suite.expectation_suite_name, batch_kwargs ) linted_code = lint_code(template) with open(tap_file_path, "w") as f: f.write(linted_code) return tap_file_path
def add_code_cell(self, code: str, lint: bool = False) -> None: """ Add the given code as a new code cell. Args: code: Code to render into the notebook cell lint: Whether to lint the code before adding it Returns: Nothing, adds a cell to the class instance notebook """ if lint: code: str = lint_code(code).rstrip("\n") cell = nbformat.v4.new_code_cell(code) self._notebook["cells"].append(cell)
def add_code_cell( self, code: str, lint: bool = False, enforce_py_syntax: bool = True ) -> None: """ Add the given code as a new code cell. Args: code: Code to render into the notebook cell lint: Whether to lint the code before adding it Returns: Nothing, adds a cell to the class instance notebook """ if enforce_py_syntax: code = convert_json_string_to_be_python_compliant(code) if lint: code = lint_code(code).rstrip("\n") cell = nbformat.v4.new_code_cell(code) self._notebook["cells"].append(cell)
def _check_linting( expectation_instance) -> ExpectationDiagnosticCheckMessage: """Check if linting checks pass for Expectation""" sub_messages: List[dict] = [] message: str = "Passes all linting checks" passed: bool = False black_ok: bool = False isort_ok: bool = False file_and_class_names_ok: bool = False rx_expectation_instance_repr = re.compile( r"<.*\.([^\.]*) object at .*") try: expectation_camel_name = rx_expectation_instance_repr.match( repr(expectation_instance)).group(1) except AttributeError: sub_messages.append({ "message": "Arg passed to _check_linting was not an instance of an Expectation, so cannot check linting", "passed": False, }) return ExpectationDiagnosticCheckMessage( message=message, passed=passed, sub_messages=sub_messages, ) impl = get_expectation_impl(camel_to_snake(expectation_camel_name)) try: source_file_path = inspect.getfile(impl) except TypeError: sub_messages.append({ "message": "inspect.getfile(impl) raised a TypeError (impl is a built-in class)", "passed": False, }) return ExpectationDiagnosticCheckMessage( message=message, passed=passed, sub_messages=sub_messages, ) snaked_impl_name = camel_to_snake(impl.__name__) source_file_base_no_ext = os.path.basename(source_file_path).rsplit( ".", 1)[0] with open(source_file_path) as fp: code = fp.read() if snaked_impl_name != source_file_base_no_ext: sub_messages.append({ "message": f"The snake_case of {impl.__name__} ({snaked_impl_name}) does not match filename part ({source_file_base_no_ext})", "passed": False, }) else: file_and_class_names_ok = True if black is None: sub_messages.append({ "message": "Could not find 'black', so cannot check linting", "passed": False, }) if isort is None: sub_messages.append({ "message": "Could not find 'isort', so cannot check linting", "passed": False, }) if black and isort: blacked_code = lint_code(code) if code != blacked_code: sub_messages.append({ "message": "Your code would be reformatted with black", "passed": False, }) else: black_ok = True isort_ok = isort.check_code( code, **isort.profiles.black, ignore_whitespace=True, known_local_folder=["great_expectations"], ) if not isort_ok: sub_messages.append({ "message": "Your code would be reformatted with isort", "passed": False, }) passed = black_ok and isort_ok and file_and_class_names_ok return ExpectationDiagnosticCheckMessage( message=message, passed=passed, sub_messages=sub_messages, )
def test_linter_leaves_clean_code(): code = "foo = [1, 2, 3]\n" assert lint_code(code) == "foo = [1, 2, 3]\n"
def test_linter_changes_dirty_code(): code = "foo = [1,2,3]" assert lint_code(code) == "foo = [1, 2, 3]\n"
def test_linter_raises_error_on_non_string_input(): with pytest.raises(TypeError): lint_code(99)