Documenting with Sphinx python methods that do have default parameters with sentinel objects?

前端 未结 3 2064
北海茫月
北海茫月 2021-01-20 21:48

If you want to be able to allow people to call some methods using None you have to do use a sentinel object when you define the method.

 _senti         


        
相关标签:
3条回答
  • 2021-01-20 22:24

    I don't think it is possible to persuade Sphinx to be more "friendly" as long as you have a sentinel that creates an object outside the function. Sphinx' autodoc extension imports the module, which means that module-level code is executed.

    Are you sure you can't use something like this?

    def foo(param1=None):
        if param1 == None:
            param1 = whatever you want...
        else:
             ... 
    
    0 讨论(0)
  • 2021-01-20 22:26

    This can be handled by manually specifying function signature in autodoc directive, e.g.:

    .. automodule:: pymorphy.contrib.tokenizers
    
        .. autofunction:: extract_tokens(foo, bar)
    
        .. autofunction:: extract_words
    
    0 讨论(0)
  • 2021-01-20 22:35

    The <object object at 0x108c1a520> part of generated method signature can be changed by overriding the __repr__ method of the sentinel object.

    _sentinel = type('_sentinel', (object,),
                     {'__repr__': lambda self: '_sentinel'})()
    

    It will be rendered by Sphinx as something like this:

    mymodule.foo(param1=_sentinel)
    
    0 讨论(0)
提交回复
热议问题