python / python/mypy

Problems with wrapper classes

Ouverte
#5,523 9 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature needs discussion priority-1-normal
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

Let's say I have a Person class with a bunch of attributes; it comes from a library I don't control but for which type information exists. Now I have a PersonWrapper class which provides for defaults so that accessing missing attributes never raises an exception (think, use of instances in f-strings).

class PersonWrapper:
    def __init__(self, person: Person, default: Any = '') -> None:
        self.person = person
        self.default = default

    def __getattr__(self, attribute: str) -> Any:
        return getattr(self.person, attribute, self.default)

In general, any place I want to use a Person I might also want to use a PersonWrapper, so I have variables that can be assigned either a Person or a PersonWrapper. mypy seems not to be able to handle this, and I don't really know of any way to tell the type checker that it should treat Person and PersonWrapper as equivalent. For example, if I have this code:

def print_person(original_person: Person) -> None:
    person = PersonWrapper(original_person)
    # This attribute might be missing from the original person object.
    print(f'  Username: {person.username}')
    person = original_person
    # Only print this if the attribute exists.
    phone = getattr(person, 'phoneNumber', None)
    if phone:
        print(f'    Phone: {phone}')

mypy complains about the assignment of original_person to person:

error: Incompatible types in assignment (expression has type "Person", variable has type "PersonWrapper")

Because Person has some dynamic attribute access as well, I can't change the original assignment to:

    person: Person = cast(Person, PersonWrapper(original_person))

because then I get errors such as:

error: "Person" has no attribute "office"

The way I fix this is by changing the second assignment to use cast():

    person = cast(PersonWrapper, original_person)

This feels wrong, but maybe it's okay. What I think i'd like to do is to create an equivalence between PersonWrapper and Person, but then I'd probably still have to handle the dynamic parts of Person in some way. FWIW I didn't have much luck using Union[Person, PersonWrapper] either.

Is there a better way to handle wrapper classes?

Guide de contribution

Ouvrir le guide de contribution

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 l’exemple Person/PersonWrapper signalé et par l’interaction entre l’inférence d’affectation, Union[Person, PersonWrapper], cast() et getattr dynamique ; aucun fichier source ni test de mypy n’est indiqué. Définissez le comportement de typage souhaité et ajoutez un test de régression ciblé une fois l’emplacement de l’implémentation identifié.

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

Évaluation

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

Recevez les nouvelles issues par e-mail

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