2012-12-17 34 views
5

-Xembedsignature=True一起使用用Cython可以產生在文檔字符串簽名的形式:斯芬克斯方法返回類型的鏈接

| mymethod(...) 
|  MyCythonModule.mymethod(self, param1, MyCythonType param2, param3=None) -> SomeResultType 

當生成斯芬克斯文檔此使用車博士擴展像這樣是輸出:

mymethod(self, param1, MyCythonType param2, param3=None) → SomeResultType 

問題是,MyCythonType和SomeResultType都不是HTML文檔中的超鏈接,這使得文檔有點不理想,無法瀏覽。

Sphinx爲文檔開發人員提供了掛鉤到'autodoc-process-signature'事件的可能性,該事件可以動態地處理簽名。該方法應返回一個(signature, return_annotation)元組。當修改return_annotation結果來插入像'SomeResultType`或者:class:SomeResultType等東西時,它不會被格式化,而是直接以HTML文檔形式存在,沒有鏈接,以及任何被附加/附加到字符串的東西。

我可以看到類型化的參數可能必須被忽略,因爲Python沒有類似的東西,但是獲得返回類型的超鏈接到它的類文檔必須是可能的,但我不知道想法。

寫一個小的測試案例後,它似乎是這會影響Python的爲好,而不是隻用Cython:

class Foo(object): 
     def __init__(self): 
       pass 

     def get_bar(self): 
       """ 
       get_bar(self) -> Bar  <- here you see 'Bar', it will not 
              become a hyperlink, not even if 
              enclosed in `` 

       Get `Bar` from foo  <- here you see Bar again, it will 
              become a hyperlink 

       :returns: the bar 
       """ 
       return Bar() 

class Bar(object): 
     def __init__(self): 
       pass 

回答

0

相反:returns: the bar的,你應該嘗試:class:`Bar`代替。

所以,像這樣:

class Foo(object): 
    def __init__(self): 
      pass 

    def get_bar(self): 
      '''Get :class:`Bar` from :class:`Foo` 

      :returns: the :class:`Bar` 
      ''' 
      return Bar() 


class Bar(object): 
    def __init__(self): 
      pass 
+0

我嘗試添加一個'車博士 - 過程 - signature'掛鉤,這樣做: '高清process_signature(應用程序,內容,名稱,OBJ,期權,SIG,RET ):return(signature,「:class:'XmmsResult'」)' 但是仍然在html中打印:class:''XmmsResult'',而不是鏈接。但是你明白了我希望的一點。這是我想修改的autodoc簽名,而不是在方法文檔末尾添加明確的返回類型。 – dsvensson

+0

我在2012年沒有任何反饋,爲此提交了一個bug票: https://bitbucket.org/birkenfeld/sphinx/issue/1059/ability-to-add-links-in-signatures – dsvensson

+0

票現在在GitHub上進行跟蹤:https://github.com/sphinx-doc/sphinx/issues/1059 – mzjn