Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Jan 20, 2024
1 parent 6fe4b58 commit 6517559
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ where `<arg>` is defined as
```
where `<name>` is the name of the function, `<cond>` is an optional additional check, `<body>` is the body of the function, `<pattern>` is defined by Coconut's [`match` statement](#match), `<default>` is the optional default if no argument is passed, and `<return_type>` is the optional return type annotation (note that argument type annotations are not supported for pattern-matching functions). The `match` keyword at the beginning is optional, but is sometimes necessary to disambiguate pattern-matching function definition from normal function definition, since Python function definition will always take precedence. Note that the `async` and `match` keywords can be in any order.

If `<pattern>` has a variable name (either directly or with `as`), the resulting pattern-matching function will support keyword arguments using that variable name.
If `<pattern>` has a variable name (via any variable binding that binds the entire pattern), the resulting pattern-matching function will support keyword arguments using that variable name.

In addition to supporting pattern-matching in their arguments, pattern-matching function definitions also have a couple of notable differences compared to Python functions. Specifically:
- If pattern-matching function definition fails, it will raise a [`MatchError`](#matcherror) (just like [destructuring assignment](#destructuring-assignment)) instead of a `TypeError`.
Expand Down
14 changes: 8 additions & 6 deletions coconut/compiler/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def match_class(self, tokens, item):
handle_indentation(
"""
raise _coconut.TypeError("too many positional args in class match (pattern requires {num_pos_matches}; '{cls_name}' only supports 1)")
""",
""",
).format(
num_pos_matches=len(pos_matches),
cls_name=cls_name,
Expand All @@ -1063,13 +1063,15 @@ def match_class(self, tokens, item):
other_cls_matcher.add_check("not _coconut.type(" + item + ") in _coconut_self_match_types")
match_args_var = other_cls_matcher.get_temp_var()
other_cls_matcher.add_def(
handle_indentation("""
handle_indentation(
"""
{match_args_var} = _coconut.getattr({cls_name}, '__match_args__', ()) {type_any} {type_ignore}
if not _coconut.isinstance({match_args_var}, _coconut.tuple):
raise _coconut.TypeError("{cls_name}.__match_args__ must be a tuple")
if _coconut.len({match_args_var}) < {num_pos_matches}:
raise _coconut.TypeError("too many positional args in class match (pattern requires {num_pos_matches}; '{cls_name}' only supports %s)" % (_coconut.len({match_args_var}),))
""").format(
""",
).format(
cls_name=cls_name,
match_args_var=match_args_var,
num_pos_matches=len(pos_matches),
Expand All @@ -1089,7 +1091,7 @@ def match_class(self, tokens, item):
"""
{match_args_var} = _coconut.getattr({cls_name}, '__match_args__', ())
{star_match_var} = _coconut.tuple(_coconut.getattr({item}, {match_args_var}[i]) for i in _coconut.range({num_pos_matches}, _coconut.len({match_args_var})))
""",
""",
).format(
match_args_var=self.get_temp_var(),
cls_name=cls_name,
Expand Down Expand Up @@ -1164,7 +1166,7 @@ def match_data_or_class(self, tokens, item):
handle_indentation(
"""
{is_data_result_var} = _coconut.getattr({cls_name}, "{is_data_var}", False) or _coconut.isinstance({cls_name}, _coconut.tuple) and _coconut.all(_coconut.getattr(_coconut_x, "{is_data_var}", False) for _coconut_x in {cls_name}) {type_ignore}
""",
""",
).format(
is_data_result_var=is_data_result_var,
is_data_var=is_data_var,
Expand Down Expand Up @@ -1241,7 +1243,7 @@ def match_view(self, tokens, item):
{func_result_var} = _coconut_sentinel
else:
raise
""",
""",
).format(
func_result_var=func_result_var,
view_func=view_func,
Expand Down

0 comments on commit 6517559

Please sign in to comment.