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

relax validation rules on decorators #926

Merged
merged 2 commits into from
May 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions libcst/_nodes/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
BaseAssignTargetExpression,
BaseDelTargetExpression,
BaseExpression,
Call,
ConcatenatedString,
ExpressionPosition,
From,
Expand Down Expand Up @@ -1619,7 +1618,7 @@ class Decorator(CSTNode):

#: The decorator that will return a new function wrapping the parent
#: of this decorator.
decorator: Union[Name, Attribute, Call]
decorator: BaseExpression

#: Line comments and empty lines before this decorator. The parent
#: :class:`FunctionDef` or :class:`ClassDef` node owns leading lines before
Expand All @@ -1632,19 +1631,6 @@ class Decorator(CSTNode):
#: Optional trailing comment and newline following the decorator before the next line.
trailing_whitespace: TrailingWhitespace = TrailingWhitespace.field()

def _validate(self) -> None:
decorator = self.decorator
if len(decorator.lpar) > 0 or len(decorator.rpar) > 0:
raise CSTValidationError(
"Cannot have parens around decorator in a Decorator."
)
if isinstance(decorator, Call) and not isinstance(
decorator.func, (Name, Attribute)
):
raise CSTValidationError(
"Decorator call function must be Name or Attribute node."
)

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "Decorator":
return Decorator(
leading_lines=visit_sequence(
Expand Down
56 changes: 40 additions & 16 deletions libcst/_nodes/tests/test_funcdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,46 @@ class FunctionDefCreationTest(CSTNodeTest):
"code": "@ bar ( )\n",
"expected_position": CodeRange((1, 0), (1, 10)),
},
# Allow nested calls on decorator
{
"node": cst.FunctionDef(
cst.Name("foo"),
cst.Parameters(),
cst.SimpleStatementSuite((cst.Pass(),)),
(cst.Decorator(cst.Call(func=cst.Call(func=cst.Name("bar")))),),
),
"code": "@bar()()\ndef foo(): pass\n",
},
# Allow any expression in decorator
{
"node": cst.FunctionDef(
cst.Name("foo"),
cst.Parameters(),
cst.SimpleStatementSuite((cst.Pass(),)),
(
cst.Decorator(
cst.BinaryOperation(cst.Name("a"), cst.Add(), cst.Name("b"))
),
),
),
"code": "@a + b\ndef foo(): pass\n",
},
# Allow parentheses around decorator
{
"node": cst.FunctionDef(
cst.Name("foo"),
cst.Parameters(),
cst.SimpleStatementSuite((cst.Pass(),)),
(
cst.Decorator(
cst.Name(
"bar", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
)
),
),
),
"code": "@(bar)\ndef foo(): pass\n",
},
# Parameters
{
"node": cst.Parameters(
Expand Down Expand Up @@ -922,22 +962,6 @@ def test_valid_native(self, **kwargs: Any) -> None:
),
r"Expecting a star prefix of '\*\*'",
),
# Validate decorator name semantics
(
lambda: cst.FunctionDef(
cst.Name("foo"),
cst.Parameters(),
cst.SimpleStatementSuite((cst.Pass(),)),
(
cst.Decorator(
cst.Name(
"bar", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
)
),
),
),
"Cannot have parens around decorator in a Decorator",
),
)
)
def test_invalid(
Expand Down
13 changes: 3 additions & 10 deletions libcst/matchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,13 +3089,6 @@ class Continue(BaseSmallStatement, BaseMatcherNode):
] = DoNotCare()


NameOrAttributeOrCallMatchType = Union[
"Name",
"Attribute",
"Call",
MetadataMatchType,
MatchIfTrue[Union[cst.Name, cst.Attribute, cst.Call]],
]
TrailingWhitespaceMatchType = Union[
"TrailingWhitespace", MetadataMatchType, MatchIfTrue[cst.TrailingWhitespace]
]
Expand All @@ -3104,10 +3097,10 @@ class Continue(BaseSmallStatement, BaseMatcherNode):
@dataclass(frozen=True, eq=False, unsafe_hash=False)
class Decorator(BaseMatcherNode):
decorator: Union[
NameOrAttributeOrCallMatchType,
BaseExpressionMatchType,
DoNotCareSentinel,
OneOf[NameOrAttributeOrCallMatchType],
AllOf[NameOrAttributeOrCallMatchType],
OneOf[BaseExpressionMatchType],
AllOf[BaseExpressionMatchType],
] = DoNotCare()
leading_lines: Union[
Sequence[
Expand Down