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

Method parse_decorators

crates/ruff_python_parser/src/parser/statement.rs:2924–3102  ·  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

2922 ///
2923 /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorators>
2924 fn parse_decorators(&mut self) -> Stmt {
2925 let start = self.node_start();
2926
2927 let mut decorators = DecoratorList::new();
2928 let mut progress = ParserProgress::default();
2929
2930 // test_err decorator_missing_expression
2931 // @def foo(): ...
2932 // @
2933 // def foo(): ...
2934 // @@
2935 // def foo(): ...
2936 // @test
2937 // @
2938 // class Test
2939 while self.at(TokenKind::At) {
2940 progress.assert_progressing(self);
2941
2942 let decorator_start = self.node_start();
2943 self.bump(TokenKind::At);
2944
2945 let parsed_expr = if self.at(TokenKind::Def) || self.at(TokenKind::Class) {
2946 Expr::Name(self.parse_missing_name()).into()
2947 } else {
2948 self.parse_named_expression_or_higher(ExpressionContext::default())
2949 };
2950
2951 if self.options.target_version < PythonVersion::PY39 {
2952 // test_ok decorator_expression_dotted_ident_py38
2953 // # parse_options: { "target-version": "3.8" }
2954 // @buttons.clicked.connect
2955 // def spam(): ...
2956
2957 // test_ok decorator_expression_identity_hack_py38
2958 // # parse_options: { "target-version": "3.8" }
2959 // def _(x): return x
2960 // @_(buttons[0].clicked.connect)
2961 // def spam(): ...
2962
2963 // test_ok decorator_expression_eval_hack_py38
2964 // # parse_options: { "target-version": "3.8" }
2965 // @eval("buttons[0].clicked.connect")
2966 // def spam(): ...
2967
2968 // test_ok decorator_expression_py39
2969 // # parse_options: { "target-version": "3.9" }
2970 // @buttons[0].clicked.connect
2971 // def spam(): ...
2972 // @(x := lambda x: x)(foo)
2973 // def bar(): ...
2974
2975 // test_err decorator_expression_py38
2976 // # parse_options: { "target-version": "3.8" }
2977 // @buttons[0].clicked.connect
2978 // def spam(): ...
2979
2980 // test_err decorator_named_expression_py37
2981 // # parse_options: { "target-version": "3.7" }

Callers 1

parse_statementMethod · 0.80

Calls 15

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
current_token_rangeMethod · 0.80

Tested by

no test coverage detected