python / python/typing

Assuming `self` or `cls` parameter is positional-only

Ouverte
#1,355 8 commentaires 4 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

topic: other
Langage dominant
Python
Étoiles
1.8k
Forks
302
Merge moyen
23 h
PR mergées (30 j)
8

Description

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 self parameter for class methods: it is undesirable that a caller can bind by keyword to the name self when 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:

  1. Is this a safe assumption? Does anyone know of a situation where self or cls are used as keyword arguments?
  2. Does this apply only to protocol methods, or does it apply generally to methods in any class?
  3. 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())
  1. Does the cls exemption apply only to explicit class methods, or does it also apply to __new__ and __init_subclass__?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire PEP 570 et PEP 544, puis examinez le rapport de bug Pyright lié et la discussion de l’issue. Déterminez si self et cls doivent être traités comme étant uniquement positionnels pour les méthodes, les protocoles, new et init_subclass. C’est terminé lorsqu’une recommandation documentée de la communauté ou une décision de spécification a été atteinte.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
tooling
Type d'issue
Documentation
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.