예제 #1
0
    def run(self) -> YieldFlake8Error:
        """Run the check plugin."""
        has_errors = False
        app = NitpickApp.current()
        for err in app.init_errors:
            has_errors = True
            yield NitpickApp.as_flake8_warning(err)
        if has_errors:
            return []

        current_python_file = Path(self.filename)
        if current_python_file.absolute() != app.main_python_file.absolute():
            # Only report warnings once, for the main Python file of this project.
            LOGGER.debug("Ignoring file: %s", self.filename)
            return []
        LOGGER.debug("Nitpicking file: %s", self.filename)

        yield from itertools.chain(app.config.merge_styles(),
                                   self.check_files(True),
                                   self.check_files(False))

        has_errors = False
        for err in app.style_errors:
            has_errors = True
            yield NitpickApp.as_flake8_warning(err)
        if has_errors:
            return []

        # Get all root keys from the style TOML.
        for path, config_dict in app.config.style_dict.items():
            # All except "nitpick" are file names.
            if path == PROJECT_NAME:
                continue

            # For each file name, find the plugin that can handle the file.
            tags = identify.tags_from_filename(path)
            for base_file in app.plugin_manager.hook.handle_config_file(  # pylint: disable=no-member
                    config=config_dict,
                    file_name=path,
                    tags=tags):
                yield from base_file.check_exists()

        return []
예제 #2
0
    def flake8_error(self,
                     number: int,
                     message: str,
                     suggestion: str = None,
                     add_to_base_number=True) -> Flake8Error:
        """Return a flake8 error as a tuple."""
        # pylint: disable=import-outside-toplevel
        from nitpick.app import NitpickApp
        from nitpick.exceptions import NitpickError

        error = NitpickError()
        error.error_base_number = self.error_base_number
        error.error_prefix = self.error_prefix
        error.number = number
        error.message = message
        if suggestion:
            error.suggestion = suggestion
        error.add_to_base_number = add_to_base_number
        return NitpickApp.as_flake8_warning(error)