Skip to content
Merged
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
12 changes: 12 additions & 0 deletions mypy/nativeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ def read_func_def(state: State, data: ReadBuffer) -> FuncDef:
# TODO: This seems wasteful, can we avoid it?
func_def.unanalyzed_type = typ.copy_modified()
expect_end_tag(data)
if state.options.include_docstrings:
func_def.docstring = get_docstring(body)
return func_def


Expand Down Expand Up @@ -769,9 +771,19 @@ def read_class_def(state: State, data: ReadBuffer) -> ClassDef:
)
check_type_param_defaults(state, type_params, class_def.line, class_def.column)
expect_end_tag(data)
if state.options.include_docstrings:
class_def.docstring = get_docstring(body)
return class_def


def get_docstring(body: Block) -> str | None:
if body.body and isinstance(body.body[0], ExpressionStmt):
expr = body.body[0].expr
if isinstance(expr, StrExpr):
return expr.value
return None


def read_type_alias_stmt(state: State, data: ReadBuffer) -> TypeAliasStmt:
"""Read PEP 695 type alias statement."""
name = read_expression(state, data)
Expand Down
Loading