2016-10-20 15 views
3

Pycharm hat eine nette Funktion, wobei es liest die Typ-Deklaration innerhalb einer Funktion Docstring und prüft die korrekte Verwendung, wo diese Funktion verwendet wird.Liste der gängigen Docstring: Typen für pycharm

Gibt es eine nette Ressource, die Regeln und Namen für alle gängigen Typen auflistet?

Hinweis:
ich eine Antwort unten zur Verfügung gestellt habe, mit allem, was ich in der Lage gewesen, um herauszufinden. Es ist jedoch alles andere als umfassend. Hier

Antwort

5

ist die Liste der Typen I häufig verwenden:

string:    str 
unicode:   unicode 
integer:   int 
float:    float 
general dictionary: dict 
general list:  list 
general tuple:  tuple 
None:    None 
not defined:  Any 
your own object: MySpecialClass 

specific tuple:  (str, int) 
specific list:  list of str 
specific dict:  dict[str, int] 
multiple options: str or list 

Beispiel:

import requests 
def example_function(list_var, str_var, num_var, request_var): 
    """ 

    :param list_var: 
    :type list_var: list of str 
    :param str_var: 
    :type str_var: str or unicode 
    :param num_var: 
    :type num_var: int 
    :param request_var: 
    :type request_var: requests.models.Request 
    :return: 
    :rtype: (list of dict) or None 
    """ 
    return [{}] 


example_function(
    ['a', 'b'], 
    u'unicode accepted because stated', 
    1.234, 
    requests.Request('GET', 'http://going/somewhere') 
) 

richtige Format für Antrag folgende Modul und Klasse von __init__.py

Dann wurde gefunden, wenn man drückt Strg und schwebt über den Funktionsaufruf, man bekommt einen Typ-Hilfetext. Dies ist sehr nützlich, da die Namenskonventionen PEP 8 typenspezifisch vage sind.

In diesem Fall wird der Hilfetext sein:

Inferred type: (list_var: List[str], str_var: Union[str, unicode], num_var: int, request_var: Request) -> Optional[List[dict]] 

Und eine Warnung für das num_var Argument:

Expected type 'int', got 'float' 
0

Es gibt repo auf GitHub mit Python Skeletten. README enthält mögliche Typdeklarationen. Hier

0

ist ein Auszug aus README-obsolete.mdTypen Abschnitt, mentioned von user2235698):

Foo    # Class Foo visible in the current scope 
x.y.Bar   # Class Bar from x.y module 
Foo | Bar   # Foo or Bar 
(Foo, Bar)   # Tuple of Foo and Bar 
list[Foo]   # List of Foo elements 
dict[Foo, Bar]  # Dict from Foo to Bar 
T     # Generic type (T-Z are reserved for generics) 
T <= Foo   # Generic type with upper bound Foo 
Foo[T]    # Foo parameterized with T 
(Foo, Bar) -> Baz # Function of Foo and Bar that returns Baz 

unknown   # Unknown type 
None    # type(None) 
string    # Py2: str | unicode, Py3: str 
bytestring   # Py2: str | unicode, Py3: bytes 
bytes    # Py2: str, Py3: bytes 
unicode   # Py2: unicode, Py3: str 

By the way: Es ist auch erwähnen Abschnitt Type Hinting im PyCharm documentation.

Verwandte Themen