Skip to content

Commit

Permalink
style: use Walrus operator
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Apr 16, 2024
1 parent 4cb75cd commit ae49a49
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
3 changes: 1 addition & 2 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ def _ask(self) -> None:
answer = question.parse_answer(result.init[var_name])
# Try to validate the answer value if the question has a
# validator.
err_msg = question.validate_answer(answer)
if err_msg:
if err_msg := question.validate_answer(answer):
raise ValueError(err_msg)
# At this point, the answer value is valid. Do not ask the
# question again, but set answer as the user's answer instead.
Expand Down
6 changes: 2 additions & 4 deletions copier/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ class Template:
use_prereleases: bool = False

def _cleanup(self) -> None:
temp_clone = self._temp_clone()
if temp_clone:
if temp_clone := self._temp_clone():
if sys.version_info >= (3, 12):
rmtree(
temp_clone,
Expand All @@ -226,11 +225,10 @@ def _temp_clone(self) -> Path | None:
"""
if "local_abspath" not in self.__dict__:
return None
clone_path = self.local_abspath
original_path = Path(self.url).expanduser()
with suppress(OSError): # triggered for URLs on Windows
original_path = original_path.resolve()
if clone_path != original_path:
if (clone_path := self.local_abspath) != original_path:
return clone_path
return None

Expand Down
9 changes: 3 additions & 6 deletions copier/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,11 @@ def _formatted_choices(self) -> Sequence[Choice]:
def get_message(self) -> str:
"""Get the message that will be printed to the user."""
if self.help:
rendered_help = self.render_value(self.help)
if rendered_help:
if rendered_help := self.render_value(self.help):
return force_str_end(rendered_help) + " "
# Otherwise, there's no help message defined.
message = self.var_name
answer_type = self.get_type_name()
if answer_type != "str":
if (answer_type := self.get_type_name()) != "str":
message += f" ({answer_type})"
return message + "\n "

Expand Down Expand Up @@ -387,8 +385,7 @@ def _validate(answer: str) -> str | Literal[True]:
if lexer:
result["lexer"] = lexer
result["multiline"] = self.get_multiline()
placeholder = self.get_placeholder()
if placeholder:
if placeholder := self.get_placeholder():
result["placeholder"] = placeholder
if questionary_type in {"input", "checkbox"}:
result["validate"] = _validate
Expand Down
3 changes: 1 addition & 2 deletions copier/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ def clone(url: str, ref: str | None = None) -> str:
_clone = git["clone", "--no-checkout", url, location]
# Faster clones if possible
if git_version >= Version("2.27"):
url_match = re.match("(file://)?(.*)", url)
if url_match is not None:
if url_match := re.match("(file://)?(.*)", url):
file_url = url_match.groups()[-1]
else:
file_url = url
Expand Down

0 comments on commit ae49a49

Please sign in to comment.