python / python/mypy

Problems with wrapper classes

Abierto
#5,523 9 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature needs discussion priority-1-normal
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el ejemplo reportado de Person/PersonWrapper y la interacción entre la inferencia de asignaciones, Union[Person, PersonWrapper], cast() y getattr dinámico; no se indica ningún archivo fuente ni prueba de mypy. Define el comportamiento de tipado deseado y añade una prueba de regresión específica una vez identificado el lugar de implementación.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.