MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_decorators

Method parse_decorators

crates/ruff_python_parser/src/parser/statement.rs:2894–3072  ·  view source on GitHub ↗

Parses a decorator list followed by a class, function or async function definition. See:

(&mut self)

Source from the content-addressed store, hash-verified

2892 ///
2893 /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorators>
2894 fn parse_decorators(&mut self) -> Stmt {
2895 let start = self.node_start();
2896
2897 let mut decorators = DecoratorList::new();
2898 let mut progress = ParserProgress::default();
2899
2900 // test_err decorator_missing_expression
2901 // @def foo(): ...
2902 // @
2903 // def foo(): ...
2904 // @@
2905 // def foo(): ...
2906 // @test
2907 // @
2908 // class Test
2909 while self.at(TokenKind::At) {
2910 progress.assert_progressing(self);
2911
2912 let decorator_start = self.node_start();
2913 self.bump(TokenKind::At);
2914
2915 let parsed_expr = if self.at(TokenKind::Def) || self.at(TokenKind::Class) {
2916 Expr::Name(self.parse_missing_name()).into()
2917 } else {
2918 self.parse_named_expression_or_higher(ExpressionContext::default())
2919 };
2920
2921 if self.options.target_version < PythonVersion::PY39 {
2922 // test_ok decorator_expression_dotted_ident_py38
2923 // # parse_options: { "target-version": "3.8" }
2924 // @buttons.clicked.connect
2925 // def spam(): ...
2926
2927 // test_ok decorator_expression_identity_hack_py38
2928 // # parse_options: { "target-version": "3.8" }
2929 // def _(x): return x
2930 // @_(buttons[0].clicked.connect)
2931 // def spam(): ...
2932
2933 // test_ok decorator_expression_eval_hack_py38
2934 // # parse_options: { "target-version": "3.8" }
2935 // @eval("buttons[0].clicked.connect")
2936 // def spam(): ...
2937
2938 // test_ok decorator_expression_py39
2939 // # parse_options: { "target-version": "3.9" }
2940 // @buttons[0].clicked.connect
2941 // def spam(): ...
2942 // @(x := lambda x: x)(foo)
2943 // def bar(): ...
2944
2945 // test_err decorator_expression_py38
2946 // # parse_options: { "target-version": "3.8" }
2947 // @buttons[0].clicked.connect
2948 // def spam(): ...
2949
2950 // test_err decorator_named_expression_py37
2951 // # parse_options: { "target-version": "3.7" }

Callers 1

parse_statementMethod · 0.80

Calls 15

NameClass · 0.85
node_startMethod · 0.80
assert_progressingMethod · 0.80
parse_missing_nameMethod · 0.80
node_rangeMethod · 0.80
expectMethod · 0.80
current_token_kindMethod · 0.80

Tested by

no test coverage detected