Skip to content
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

Turn test_regex into a click callback #2016

Merged
merged 2 commits into from Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 29 additions & 32 deletions src/black/__init__.py
Expand Up @@ -363,6 +363,17 @@ def target_version_option_callback(
return [TargetVersion[val.upper()] for val in v]


def validate_regex(
ctx: click.Context,
param: click.Parameter,
value: Optional[str],
) -> Optional[Pattern]:
try:
return re_compile_maybe_verbose(value) if value is not None else None
except re.error:
raise click.BadParameter("Not a valid regular expression")


@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
@click.option(
Expand Down Expand Up @@ -441,6 +452,7 @@ def target_version_option_callback(
"--include",
type=str,
default=DEFAULT_INCLUDES,
callback=validate_regex,
help=(
"A regular expression that matches files and directories that should be"
" included on recursive searches. An empty value means all files are included"
Expand All @@ -453,6 +465,7 @@ def target_version_option_callback(
"--exclude",
type=str,
default=DEFAULT_EXCLUDES,
callback=validate_regex,
help=(
"A regular expression that matches files and directories that should be"
" excluded on recursive searches. An empty value means no paths are excluded."
Expand All @@ -464,6 +477,7 @@ def target_version_option_callback(
@click.option(
"--extend-exclude",
type=str,
callback=validate_regex,
help=(
"Like --exclude, but adds additional files and directories on top of the"
" excluded ones. (Useful if you simply want to add to the default)"
Expand All @@ -472,6 +486,7 @@ def target_version_option_callback(
@click.option(
"--force-exclude",
type=str,
callback=validate_regex,
help=(
"Like --exclude, but files and directories matching this regex will be "
"excluded even when they are passed explicitly as arguments."
Expand Down Expand Up @@ -543,10 +558,10 @@ def main(
experimental_string_processing: bool,
quiet: bool,
verbose: bool,
include: str,
exclude: str,
extend_exclude: Optional[str],
force_exclude: Optional[str],
include: Pattern,
exclude: Pattern,
extend_exclude: Optional[Pattern],
force_exclude: Optional[Pattern],
stdin_filename: Optional[str],
src: Tuple[str, ...],
config: Optional[str],
Expand Down Expand Up @@ -612,39 +627,21 @@ def main(
ctx.exit(report.return_code)


def test_regex(
ctx: click.Context,
regex_name: str,
regex: Optional[str],
) -> Optional[Pattern]:
try:
return re_compile_maybe_verbose(regex) if regex is not None else None
except re.error:
err(f"Invalid regular expression for {regex_name} given: {regex!r}")
ctx.exit(2)


def get_sources(
*,
ctx: click.Context,
src: Tuple[str, ...],
quiet: bool,
verbose: bool,
include: str,
exclude: str,
extend_exclude: Optional[str],
force_exclude: Optional[str],
include: Pattern[str],
exclude: Pattern[str],
extend_exclude: Optional[Pattern[str]],
force_exclude: Optional[Pattern[str]],
Copy link
Contributor Author

@thejcannon thejcannon Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't start with an underscore (as don't the others) which makes me question the intended visibility of this method (is it public or private? do we expect people to know not to use it if it's private?).

If you'd like I'd be happy to handle str params here for backwards-compatibility and issue a DeprecationWarning.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I have no idea whether people use this function (I hope not since it certainly wasn't designed for it!), but even though Black technically doesn't have a public API, I bet we're going to hit Hyrum's Law. My personal opinion is that since it's that since it needs a click context anyway and has some behaviour API consumers wouldn't like (like exiting without a choice in some cases), it's fine to just leave this change unmitigated. Others may have a different opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point about the click context object!

FWIW you could easily add underscores to everything to imply everything is private. Then if people really want to use it they'll have to fight their linters. You also get some deniability when people inevitably complain "you broke me!".

However, I think you'll have to be choosy about what to initially mark private, as I feel like I've seen people consuming black from Python in the wild.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to change the API here. We already don't guarantee a public API, and as @ichard26 says this function is already quite inconvenient to use as an API.

report: "Report",
stdin_filename: Optional[str],
) -> Set[Path]:
"""Compute the set of files to be formatted."""

include_regex = test_regex(ctx, "include", include)
exclude_regex = test_regex(ctx, "exclude", exclude)
assert exclude_regex is not None
extend_exclude_regex = test_regex(ctx, "extend_exclude", extend_exclude)
force_exclude_regex = test_regex(ctx, "force_exclude", force_exclude)

root = find_project_root(src)
sources: Set[Path] = set()
path_empty(src, "No Path provided. Nothing to do 😴", quiet, verbose, ctx)
Expand All @@ -665,8 +662,8 @@ def get_sources(

normalized_path = "/" + normalized_path
# Hard-exclude any files that matches the `--force-exclude` regex.
if force_exclude_regex:
force_exclude_match = force_exclude_regex.search(normalized_path)
if force_exclude:
force_exclude_match = force_exclude.search(normalized_path)
else:
force_exclude_match = None
if force_exclude_match and force_exclude_match.group(0):
Expand All @@ -682,10 +679,10 @@ def get_sources(
gen_python_files(
p.iterdir(),
root,
include_regex,
exclude_regex,
extend_exclude_regex,
force_exclude_regex,
include,
exclude,
extend_exclude,
force_exclude,
report,
gitignore,
)
Expand Down
28 changes: 14 additions & 14 deletions tests/test_black.py
Expand Up @@ -1375,8 +1375,8 @@ def test_exclude_for_issue_1572(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude=exclude,
include=re.compile(include),
exclude=re.compile(exclude),
extend_exclude=None,
force_exclude=None,
report=report,
Expand All @@ -1398,8 +1398,8 @@ def test_get_sources_with_stdin(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude=exclude,
include=re.compile(include),
exclude=re.compile(exclude),
extend_exclude=None,
force_exclude=None,
report=report,
Expand All @@ -1422,8 +1422,8 @@ def test_get_sources_with_stdin_filename(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude=exclude,
include=re.compile(include),
exclude=re.compile(exclude),
extend_exclude=None,
force_exclude=None,
report=report,
Expand All @@ -1450,8 +1450,8 @@ def test_get_sources_with_stdin_filename_and_exclude(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude=exclude,
include=re.compile(include),
exclude=re.compile(exclude),
extend_exclude=None,
force_exclude=None,
report=report,
Expand All @@ -1478,9 +1478,9 @@ def test_get_sources_with_stdin_filename_and_extend_exclude(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude="",
extend_exclude=extend_exclude,
include=re.compile(include),
exclude=re.compile(""),
extend_exclude=re.compile(extend_exclude),
force_exclude=None,
report=report,
stdin_filename=stdin_filename,
Expand All @@ -1504,10 +1504,10 @@ def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None:
src=(src,),
quiet=True,
verbose=False,
include=include,
exclude="",
include=re.compile(include),
exclude=re.compile(""),
extend_exclude=None,
force_exclude=force_exclude,
force_exclude=re.compile(force_exclude),
report=report,
stdin_filename=stdin_filename,
)
Expand Down