FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Preserve docstrings in the new parser (#21890) · python/mypy@1dab3c5 · GitHub

/ mypy Public

Commit 1dab3c5

Browse files
authored
Preserve docstrings in the new parser (#21890)
Old parser has some special treatment for docstrings. Match that in the new parser. Not adding tests, since some existing tests fail when switched to new parser, see #21823
1 parent cea9058 commit 1dab3c5

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎mypy/nativeparse.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ def read_func_def(state: State, data: ReadBuffer) -> FuncDef:
723723
# TODO: This seems wasteful, can we avoid it?
724724
func_def.unanalyzed_type = typ.copy_modified()
725725
expect_end_tag(data)
726+
if state.options.include_docstrings:
727+
func_def.docstring = get_docstring(body)
726728
return func_def
727729

728730

@@ -769,9 +771,19 @@ def read_class_def(state: State, data: ReadBuffer) -> ClassDef:
769771
)
770772
check_type_param_defaults(state, type_params, class_def.line, class_def.column)
771773
expect_end_tag(data)
774+
if state.options.include_docstrings:
775+
class_def.docstring = get_docstring(body)
772776
return class_def
773777

774778

779+
def get_docstring(body: Block) -> str | None:
780+
if body.body and isinstance(body.body[0], ExpressionStmt):
781+
expr = body.body[0].expr
782+
if isinstance(expr, StrExpr):
783+
return expr.value
784+
return None
785+
786+
775787
def read_type_alias_stmt(state: State, data: ReadBuffer) -> TypeAliasStmt:
776788
"""Read PEP 695 type alias statement."""
777789
name = read_expression(state, data)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL