Assuming `self` or `cls` parameter is positional-only
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
I'm interested in guidance from the typing community relating to a question that came up in a recent pyright bug report.
The issue has to do with the self parameter in an instance method and the cls parameter in a class method — whether a type checker should assume that they are implicitly positional-only parameters. In particular, the discussion was about method declarations within a protocol class.
PEP 570 makes a reference to this topic in its introductory section:
One special case of this situation is the
selfparameter for class methods: it is undesirable that a caller can bind by keyword to the nameselfwhen calling the method from the class.
But PEP 570 doesn't provide any specific guidance about self or cls in its specification section. PEP 544 (which introduced protocols) is also silent on the topic.
Pyright currently assumes that self and cls are not positional-only parameters (unless explicitly followed by a / parameter marker or named with double underscores). This assumption affects protocol matching — and more generally, subtype compatibility evaluation for callables.
@AlexWaygood is proposing that the first parameter of instance and class methods should always be considered position-only parameters.
This raises some questions in my mind:
- Is this a safe assumption? Does anyone know of a situation where
selforclsare used as keyword arguments? - Does this apply only to protocol methods, or does it apply generally to methods in any class?
- Should it be considered a type checker error if someone attempts to use a keyword argument to target the first parameter of an instance or class method? It works at runtime.
class Foo:
def bar(self): pass
Foo.bar(self=Foo())
- Does the
clsexemption apply only to explicit class methods, or does it also apply to__new__and__init_subclass__?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia leggendo PEP 570 e PEP 544, quindi esamina il bug report di Pyright collegato e la discussione dell'issue. Determina se self e cls debbano essere trattati come posizionali-only per metodi, protocolli, new e init_subclass. Il lavoro è completato quando si è raggiunta una linea guida documentata della community o una decisione sulla specifica.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Documentazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 20/100