Skip to content

Commit

Permalink
feat: add --autofix flag
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Sep 12, 2021
1 parent 38803c2 commit 1428958
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/tryceratops/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@


EXPERIMENTAL_FLAG_OPTION = dict(is_flag=True, help="Whether to enable experimental analyzers.")
AUTOFIX_FLAG_OPTION = dict(
is_flag=True, help="Whether to fix violations (that support it) automatically."
)
IGNORE_OPTION = dict(
multiple=True,
help="A violation to be ignored. e.g. -i TC200 -i TC201",
Expand All @@ -32,20 +35,22 @@
@click.option("-i", "--ignore", **IGNORE_OPTION)
@click.option("-x", "--exclude", **EXCLUDE_OPTION)
@click.option("-v", "--verbose", **VERBOSE_OPTION)
@click.option("-a", "--autofix", **AUTOFIX_FLAG_OPTION)
@click.version_option(tryceratops.__version__)
def entrypoint(
dir: Tuple[str],
experimental: bool,
ignore: Tuple[str, ...],
exclude: Tuple[str, ...],
verbose: bool,
autofix: bool,
):
pyproj_config = load_config(dir)
if pyproj_config:
global_filter = GlobalFilter.create_from_config(pyproj_config)
global_filter.overwrite_from_cli(experimental, ignore, exclude)
global_filter.overwrite_from_cli(experimental, ignore, exclude, autofix)
else:
global_filter = GlobalFilter(experimental, ignore, exclude)
global_filter = GlobalFilter(experimental, ignore, exclude, autofix)

if verbose:
logger = logging.getLogger("tryceratops")
Expand Down
8 changes: 7 additions & 1 deletion src/tryceratops/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class GlobalFilter:
include_experimental: bool
ignore_violations: Iterable[str]
exclude_dirs: Iterable[str]
autofix: bool = False

def _self_check(self):
self.exclude_dirs = [excluded for excluded in self.exclude_dirs if excluded]
Expand Down Expand Up @@ -86,14 +87,16 @@ def create_from_config(cls, config: PyprojectConfig) -> GlobalFilter:
experimental = config.get("experimental", False)
ignore = config.get("ignore", [])
exclude = config.get("exclude", [])
autofix = config.get("autofix", False)

return cls(experimental, ignore, exclude)
return cls(experimental, ignore, exclude, autofix)

def overwrite_from_cli(
self,
include_experimental: bool,
ignore_violations: Iterable[str],
exclude_dirs: Iterable[str],
autofix: bool,
):
"""In case any value is set it overwrites the previous value"""
if include_experimental:
Expand All @@ -105,4 +108,7 @@ def overwrite_from_cli(
if exclude_dirs:
self.exclude_dirs = exclude_dirs

if autofix:
self.autofix = autofix

self._self_check()
1 change: 1 addition & 0 deletions src/tryceratops/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ class PyprojectConfig(TypedDict):
exclude: List[str]
ignore: List[str]
experimental: bool
autofix: bool

0 comments on commit 1428958

Please sign in to comment.