File tree Expand file tree Collapse file tree 3 files changed +31
-6
lines changed Expand file tree Collapse file tree 3 files changed +31
-6
lines changed Original file line number Diff line number Diff line change 1515DEPRECATION_KEYWORDS  =  {"deprecation" , "deprecated" }
1616RETURNS_KEYWORDS  =  {"return" , "returns" }
1717YIELDS_KEYWORDS  =  {"yield" , "yields" }
18+ EXAMPLES_KEYWORDS  =  {"example" , "examples" }
1819
1920
2021class  ParseError (RuntimeError ):
@@ -130,6 +131,19 @@ def __init__(
130131        self .description  =  description 
131132
132133
134+ class  DocstringExample (DocstringMeta ):
135+     """DocstringMeta symbolizing example metadata.""" 
136+ 
137+     def  __init__ (
138+         self ,
139+         args : T .List [str ],
140+         description : T .Optional [str ],
141+     ) ->  None :
142+         """Initialize self.""" 
143+         super ().__init__ (args , description )
144+         self .description  =  description 
145+ 
146+ 
133147class  Docstring :
134148    """Docstring object representation.""" 
135149
@@ -184,3 +198,10 @@ def deprecation(self) -> T.Optional[DocstringDeprecated]:
184198            if  isinstance (item , DocstringDeprecated ):
185199                return  item 
186200        return  None 
201+ 
202+     @property  
203+     def  examples (self ) ->  T .List [DocstringExample ]:
204+         """Return a list of information on function examples.""" 
205+         return  [
206+             item  for  item  in  self .meta  if  isinstance (item , DocstringExample )
207+         ]
Original file line number Diff line number Diff line change 77from  enum  import  IntEnum 
88
99from  .common  import  (
10+     EXAMPLES_KEYWORDS ,
1011    PARAM_KEYWORDS ,
1112    RAISES_KEYWORDS ,
1213    RETURNS_KEYWORDS ,
1314    YIELDS_KEYWORDS ,
1415    Docstring ,
16+     DocstringExample ,
1517    DocstringMeta ,
1618    DocstringParam ,
1719    DocstringRaises ,
@@ -133,6 +135,8 @@ def _build_single_meta(section: Section, desc: str) -> DocstringMeta:
133135            return  DocstringRaises (
134136                args = [section .key ], description = desc , type_name = None 
135137            )
138+         if  section .key  in  EXAMPLES_KEYWORDS :
139+             return  DocstringExample (args = [section .key ], description = desc )
136140        if  section .key  in  PARAM_KEYWORDS :
137141            raise  ParseError ("Expected paramenter name." )
138142        return  DocstringMeta (args = [section .key ], description = desc )
Original file line number Diff line number Diff line change @@ -632,9 +632,9 @@ def test_examples() -> None:
632632            more here 
633633        """ 
634634    )
635-     assert  len (docstring .meta ) ==  2 
636-     assert  docstring .meta [0 ].description  ==  "example: 1" 
637-     assert  docstring .meta [1 ].description  ==  "long example\n \n more here" 
635+     assert  len (docstring .examples ) ==  2 
636+     assert  docstring .examples [0 ].description  ==  "example: 1" 
637+     assert  docstring .examples [1 ].description  ==  "long example\n \n more here" 
638638
639639
640640def  test_broken_meta () ->  None :
@@ -696,9 +696,9 @@ def test_empty_example() -> None:
696696        """ 
697697    )
698698
699-     assert  len (docstring .meta ) ==  2 
700-     assert  docstring .meta [0 ].args  ==  ["examples" ]
701-     assert  docstring .meta [0 ].description  ==  "" 
699+     assert  len (docstring .examples ) ==  1 
700+     assert  docstring .examples [0 ].args  ==  ["examples" ]
701+     assert  docstring .examples [0 ].description  ==  "" 
702702
703703
704704@pytest .mark .parametrize ( 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments