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

Method docstring

crates/ty_python_core/src/definition.rs:143–171  ·  view source on GitHub ↗

Extract a docstring from this definition, if applicable. This method returns a docstring for function, class, and attribute definitions. The docstring is extracted from the first statement in the body if it's a string literal.

(self, db: &'db dyn Db)

Source from the content-addressed store, hash-verified

141 /// This method returns a docstring for function, class, and attribute definitions.
142 /// The docstring is extracted from the first statement in the body if it's a string literal.
143 pub fn docstring(self, db: &'db dyn Db) -> Option<String> {
144 let file = self.file(db);
145 let module = parsed_module(db, file).load(db);
146 let kind = self.kind(db);
147
148 match kind {
149 DefinitionKind::Assignment(assign_def) => {
150 let assign_node = assign_def.target(&module);
151 attribute_docstring(&module, assign_node)
152 .map(|docstring_expr| docstring_expr.value.to_str().to_owned())
153 }
154 DefinitionKind::AnnotatedAssignment(assign_def) => {
155 let assign_node = assign_def.target(&module);
156 attribute_docstring(&module, assign_node)
157 .map(|docstring_expr| docstring_expr.value.to_str().to_owned())
158 }
159 DefinitionKind::Function(function_def) => {
160 let function_node = function_def.node(&module);
161 docstring_from_body(&function_node.body)
162 .map(|docstring_expr| docstring_expr.value.to_str().to_owned())
163 }
164 DefinitionKind::Class(class_def) => {
165 let class_node = class_def.node(&module);
166 docstring_from_body(&class_node.body)
167 .map(|docstring_expr| docstring_expr.value.to_str().to_owned())
168 }
169 _ => None,
170 }
171 }
172}
173
174/// The identity of the place defined by a [`Definition`] and whether it is re-exported.

Callers

nothing calls this directly

Calls 11

parsed_moduleFunction · 0.85
attribute_docstringFunction · 0.85
docstring_from_bodyFunction · 0.85
fileMethod · 0.45
loadMethod · 0.45
kindMethod · 0.45
targetMethod · 0.45
mapMethod · 0.45
to_ownedMethod · 0.45
to_strMethod · 0.45
nodeMethod · 0.45

Tested by

no test coverage detected