web-dev-qa-db-ja.com

PyCharmにパラメータがどのタイプであると予想されるかをどのように伝えることができますか?

コンストラクター、割り当て、およびメソッド呼び出しに関しては、PyCharm IDEはソースコードを分析し、各変数がどのようなタイプであるかを把握するのに非常に優れています。コード補完とパラメータ情報が得られ、存在しない属性にアクセスしようとすると警告が表示されるためです。

しかし、パラメータに関しては、何も知りません。コード補完のドロップダウンには何も表示されません。パラメーターがどのタイプになるかわからないためです。コード分​​析は警告を探すことができません。

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

peasant = Person("Dennis", 37)
# PyCharm knows that the "peasant" variable is of type Person
peasant.Dig_filth()   # shows warning -- Person doesn't have a Dig_filth method

class King:
    def repress(self, peasant):
        # PyCharm has no idea what type the "peasant" parameter should be
        peasant.knock_over()   # no warning even though knock_over doesn't exist

King().repress(peasant)
# Even if I call the method once with a Person instance, PyCharm doesn't
# consider that to mean that the "peasant" parameter should always be a Person

これにはある程度の意味があります。他の呼び出しサイトは、そのパラメーターに何でも渡すことができます。しかし、私のメソッドがパラメーターの型を想定している場合、たとえばpygame.Surface、それを何らかの方法でPyCharmに示すことができるようにしたいので、コード補完のドロップダウンにSurfaceのすべての属性を表示し、間違ったメソッドを呼び出すと警告を強調表示することができます。オン。

PyCharmにヒントを与え、「psst、このパラメータはX型であると想定されています」と言う方法はありますか? (または、おそらく、動的言語の精神では、「このパラメーターはXのように振る舞うことになっています」?それで問題ありません。)


編集: CrazyCoderの答えは、下記のトリックです。クイックサマリーを必要とする私のような新規参入者の場合、次のとおりです。

class King:
    def repress(self, peasant):
        """
        Exploit the workers by hanging on to outdated imperialist dogma which
        perpetuates the economic and social differences in our society.

        @type peasant: Person
        @param peasant: Person to repress.
        """
        peasant.knock_over()   # Shows a warning. And there was much rejoicing.

関連する部分は@type peasant: Person docstringの行。

[ファイル]> [設定]> Python Integrated Toolsに移動し、[Docstring format]を[Epytext]に設定すると、PyCharmの[表示]> [クイックドキュメントルックアップ]は、単に印刷するのではなくパラメーター情報をきれいに印刷しますすべての@行はそのままです。

168
Joe White

はい。PyCharmがタイプを認識できるように、メソッドとそのパラメーターに特別なドキュメント形式を使用できます。最新のPyCharmバージョン 最も一般的なドキュメント形式をサポート

たとえば、PyCharmは @ paramスタイルコメント から型を抽出します。

reStructuredText および docstring Conventions (PEP 257)も参照してください。

別のオプションはPython 3注釈。

PyCharmのドキュメントセクションを参照 詳細とサンプルをご覧ください。

83
CrazyCoder

Python 3.0以降を使用している場合、関数とパラメーターに注釈を使用することもできます。PyCharmは、これらを引数または戻り値に必要な型として解釈します。

class King:
    def repress(self, peasant: Person) -> bool:
        peasant.knock_over() # Shows a warning. And there was much rejoicing.

        return peasant.badly_hurt() # Lets say, its not known from here that this method will always return a bool

これは、docstringを必要としない非パブリックメソッドに役立つ場合があります。追加の利点として、これらの注釈にはコードでアクセスできます。

>>> King.repress.__annotations__
{'peasant': <class '__main__.Person'>, 'return': <class 'bool'>}

更新PEP 484 の時点で、Python 3.5、itまた、注釈を使用して引数と戻り値の型を指定する公式の規則です。

46
Feuermurmel

PyCharmは@type pydoc文字列から型を抽出します。PyCharm docs here および here を参照してください。 Epydoc docs 。これはPyCharmの「レガシー」セクションにあり、おそらくいくつかの機能が欠けています。

class King:
    def repress(self, peasant):
        """
        Exploit the workers by hanging on to outdated imperialist dogma which
        perpetuates the economic and social differences in our society.

        @type peasant: Person
        @param peasant: Person to repress.
        """
        peasant.knock_over()   # Shows a warning. And there was much rejoicing.

関連する部分は@type peasant: Person docstringの行。

私の意図は、CrazyCoderまたは元の質問者からポイントを盗むことではなく、必ず彼らにポイントを与えることです。単純な答えは「答え」スロットに入れるべきだと思った。

4
dfrankow

タイプをアサートすることもでき、Pycharmはそれを推測します:

def my_function(an_int):
    assert isinstance(an_int, int)
    # Pycharm now knows that an_int is of type int
    pass

私はPyCharm Professional 2016.1を使用してpy2.6-2.7コードを記述していますが、reStructuredTextを使用すると、より簡潔な方法で型を表現できることがわかりました。

class Replicant(object):
    pass


class Hunter(object):
    def retire(self, replicant):
        """ Retire the rogue or non-functional replicant.
        :param Replicant replicant: the replicant to retire.
        """
        replicant.knock_over()  # Shows a warning.

参照: https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html#legacy

1
pongi