python / python/typing

Allow to use function objects as singletons

オープン
#668 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

I was trying to create some stubs for lxml library, but I encountered a function that depending on a function object it gets (as in specific function object - checked with identity check) only finds objects of specific type.

Here's a very simplified mock-up of it so that you can get some idea on what I mean (the typing for arg argument in the find function is incorrect and that's what I want to be able to type hint in some way):

from typing import Union

class _Element: ...

def Element() -> _Element: ...

class _Comment: ...

def Comment() -> _Comment: ...

def find(arg: Union[Element, Comment]) -> Union[_Element, _Comment]:
    if arg is Element:
        # only find _Element objects
    elif arg is Element:
        # only find _Comment objects

# this function should only accept `Element` and `Comment` function object 
find(arg=Element)

So basically those functions serve as a singletons. Unfortunately typing.Union only allows Enums to work as singletons.

I am not entirely sure if this should be supported, usually the code could just be adjusted to use Enums, but in this case it will be used for type stubs so the code can't be edited. And to be honest, I am not sure if the code really needs editing in that case as if I didn't need typing for that library, I would say it's actually quite intuitive to use these factory functions as arguments in this case.

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

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

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

調査の方向性

issue #668 の factory-function singleton の例と Enum singleton との比較から始めてください。issue では実装ファイルやテストが指定されていないため、まず typing が singleton 値をどこで表現しているか、また関数オブジェクトが現在どのように扱われているかを特定してください。完了条件は、これらの関数オブジェクトを型ヒントで受け入れ、区別して表現できるかどうかを確認し、Element/Comment スタイルのケースをカバーすることです。

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

評価

技術スタック
python
領域
developer-experience
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

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

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