python / python/mypy

Mypy plugins: Changing class signature impossible here?

Aperta
#19,195 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Hello,

I'm trying to write a plugin for my paramclasses library. I already posted about it on gitter to ask a few questions (without answer at the moment).

I'm still trying to get to work on my library's typing issues whilst being a complete noob on the subject.

So I started by writing a logger-like dummy plugin which shows the called hooked with their args/kwargs (so that I understand the mechanisms a bit better), and trying to solve the signature problem.

1. Nonparamclass MWE: looks OK

Here's a MWE:

class A:
    def __init__(self, *args: object, **kwargs: object) -> None:
    """Takes anything at runtime but raises unless only 'foo' kwarg."""
        if args or list(kwargs) != ["foo"]:
            msg = "Only 'foo' kwarg is acceptable"
            raise ValueError(msg)

        self.foo = kwargs.get("foo")


A(bar="should fail")

I would like mypy to pick up the fact that the signature is in fact A(*, foo). The logger picks up this (filtered to keep only signature related hooks):
(removing the report hook):

[get_function_signature_hook] → ('builtins.list',), {}
[get_method_signature_hook] → ('builtins.list.__ne__',), {}
[get_function_signature_hook] → ('builtins.ValueError',), {}
[get_method_signature_hook] → ('builtins.dict.get',), {}
[get_method_signature_hook] → ('builtins.dict.get',), {}
[get_method_signature_hook] → ('builtins.object.__init_subclass__',), {}
[get_function_signature_hook] → ('mwe.A',), {}

Q1. Am I right to assume that I logically should use get_function_signature_hook with mwe.A for this example?If so, I think this works fine since I have a way to write a plugin to do what I want.

Now on to my use case

2. Paramclass MWE: not OK

My usecase MWE would be:

from paramclasses import ParamClass


class A(ParamClass):
    foo: int


A(bar="should fail")

When I filter out the logger plugin output, to keep only the "signature" hook, I only get this which refers to internals that seem unrelated:

[get_function_signature_hook] → ('paramclasses.paramclasses._MissingType',), {}
[get_function_signature_hook] → ('paramclasses.paramclasses._run_once',), {}
[get_function_signature_hook] → ('paramclasses.paramclasses._run_once',), {}
[get_function_signature_hook] → ('paramclasses.paramclasses._run_once',), {}

So in the end, I'm completely lost on what I should/could do.

Q2. Is there a way to think about understanding the problem? Note that if useful, the __signature__ attribute returns the desired result at runtim.


Thanks in advance!
Élie

Guida per i contributori

Apri la guida per i contributori

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 i due MWE Python e confronta i callback del plugin mostrati per la classe semplice e la sottoclasse ParamClass. Analizza la relazione tra get_function_signature_hook, ParamClass e il suo signature a runtime; il risultato dovrebbe stabilire se la firma del costruttore desiderata è supportata e identificare il punto di ingresso appropriato del plugin o una limitazione.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.