python / python/typing

Allow to use function objects as singletons

Aperta
#668 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio di singleton tramite factory function e il confronto con i singleton Enum nell’issue #668. Poiché nell’issue non sono indicati file di implementazione o test, individua innanzitutto dove typing rappresenta i valori singleton e come vengono attualmente gestiti gli oggetti funzione. Il lavoro è completo quando è stato stabilito se questi oggetti funzione possono essere accettati e rappresentati distintamente nelle annotazioni di tipo, con copertura per il caso in stile Element/Comment.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.