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

Method parse_decorators

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

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