File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -870,6 +870,32 @@ def unused_code_at_end():
870870 'RETURN_CONST' ,
871871 list (dis .get_instructions (unused_code_at_end ))[- 1 ].opname )
872872
873+ @support .cpython_only
874+ def test_docstring (self ):
875+ src = textwrap .dedent ("""
876+ def with_docstring():
877+ "docstring"
878+
879+ def with_fstring():
880+ f"not docstring"
881+
882+ def with_const_expression():
883+ "also" + " not docstring"
884+ """ )
885+
886+ for opt in [0 , 1 , 2 ]:
887+ with self .subTest (opt = opt ):
888+ code = compile (src , "<test>" , "exec" , optimize = opt )
889+ ns = {}
890+ exec (code , ns )
891+
892+ if opt < 2 :
893+ self .assertEqual (ns ['with_docstring' ].__doc__ , "docstring" )
894+ else :
895+ self .assertIsNone (ns ['with_docstring' ].__doc__ )
896+ self .assertIsNone (ns ['with_fstring' ].__doc__ )
897+ self .assertIsNone (ns ['with_const_expression' ].__doc__ )
898+
873899 @support .cpython_only
874900 def test_docstring_omitted (self ):
875901 # See gh-115347
You can’t perform that action at this time.
0 commit comments