python / python/mypy

Strange custom type for `__init__` behaviour [@curry plugin]

オープン
#8,801 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

question topic-plugins
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

  • Are you reporting a bug, or opening a feature request?

Bug report / support question.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

I am working on @curry typed decorator support. https://github.com/dry-python/returns/pull/350
It works fine for all cases right now. Except for the case when __init__ method is curried.
In this particular case it changes our correct return type (returned from our custom plugin) to some strange incorrect type.

from returns.curry import curry

class Test(object):
    @curry
    def __init__(self, a: int, b: str) -> None:
        ...

    @curry
    def some(self, a: int, b: str) -> None:
        ...

t: Test

# Works fine!
reveal_type(t.some)
# => Revealed type is 'Overload(def () -> Overload(def (a: builtins.int, b: builtins.str), def (a: builtins.int) -> def (b: builtins.str)), def (a: builtins.int) -> def (b: builtins.str), def (a: builtins.int, b: builtins.str))'

# Modifies the return type from our plugin.
reveal_type(Test)
# => Revealed type is 'Overload(def () -> ex.Test, def (a: builtins.int) -> ex.Test, def (a: builtins.int, b: builtins.str) -> ex.Test)'

Full reproduction:

  1. Clone https://github.com/dry-python/returns/pull/350
  2. poetry install
  3. Drop the code from this issue into ./ex.py
  4. poetry run mypy ex.py
  • What is the actual behavior/output?

Let's focus on the incorrect output we are getting for __init__ methods:

reveal_type(Test)
# => Revealed type is 'Overload(def () -> ex.Test, def (a: builtins.int) -> ex.Test, def (a: builtins.int, b: builtins.str) -> ex.Test)'
  • What is the behavior/output you expect?

I expect it to have the exact same type our plugin produces:

Overload(def (self: ex.Test) -> Overload(def (a: builtins.int, b: builtins.str), def (a: builtins.int) -> def (b: builtins.str)), def (self: ex.Test, a: builtins.int) -> def (b: builtins.str), def (self: ex.Test, a: builtins.int, b: builtins.str))

The thing is this type is produced for both .some() and __init__ methods in this example. But, __init__ does change it for some reason. And .some() works correctly.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
  • mypy: 0.770
  • any python from 3.6 to 3.8
  • What are the mypy flags you are using? (For example --strict-optional)

Full list: https://github.com/dry-python/returns/blob/master/setup.cfg#L101

Useful links:

  1. Failing CI with the helpful error message: https://github.com/dry-python/returns/runs/666196191?check_suite_focus=true#step:7:660
  2. Failing typetest source: https://github.com/orsinium-forks/returns/blob/curry/typesafety/test_curry/test_curry/test_curry_arguments.yml#L20
  3. Implementation: https://github.com/orsinium-forks/returns/blob/curry/returns/curry.py#L38
  4. Plugin source: https://github.com/orsinium-forks/returns/blob/curry/returns/contrib/mypy/decorator_plugin.py#L116
  5. Upstream: https://github.com/dry-python/returns

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた returns の pull request で問題を再現します。Poetry でインストールし、例を ex.py として保存して、poetry run mypy ex.py を実行します。returns/curry.pyreturns/contrib/mypy/decorator_plugin.py、およびリンクされた curry の typetest を読み、__init__some を比較します。完了条件は、型 Test が誤ったコンストラクターオーバーロードではなく、plugin が生成した curried signature を保持することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。