python / python/mypy

metaclass/__init_subclass__ keyword checking

Abierto
#11,057 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature topic-metaclasses topic-plugins
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature

A way for plugins to mark a metaclass as safely passing keyword arguments to __init_subclass__.

Pitch

The current situation

Currently, if a class is inherited from that both has a custom metaclass and __init_subclass__ defined then there is no type checking done for the keyword arguments in __init_subclass__. For example the following is an error:

class Base:
    def __init_subclass__(cls, keyword: int = 0): ...
    
class Derived(Base, keyword='fail'): ...

error: Argument "keyword" to "__init_subclass__" of "Base" has incompatible type "str"; expected "int"

But this is not:

class Meta(type): ...

class Base(metaclass=Meta):
    def __init_subclass__(cls, keyword: int = 0): ...
    
class Derived(Base, keyword='fail'): ...

The specifics around this are in #7723.

My issue

I have a base class that operates similar to dataclass object. I have a plugin that provides the semantics to mypy already. However, this class uses a metaclass and __init_subclass__ and I would like type checking on the __init_subclass__ keyword arguments. The metaclass __new__ does not do anything with the keyword arguments but pass them along, so I know it's safe to do the type checking. I would like for my plugin to be able to tell mypy that this metaclass is safe.

Unfortunately I can't just get rid of the metaclass as it provides a class level __await__.

My current solution is just to hack the TypeInfo._fullname for the metaclass to be builtins.type.

The solutions that come to mind would be either:

  • A boolean on TypeInfo that says that the metaclass is __init_subclass__ safe (plugins just need to flip this in one of the existing hooks)
  • A way for plugins to manipulate the set of safe metaclasses that Checker uses (hook to customize Checker prior to run?)

I'm happy to do the work if a solution can be found.

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

Empieza leyendo la lógica de Checker que determina qué metaclases son seguras y, a continuación, inspecciona TypeInfo y los hooks de plugin existentes mencionados en el issue. Compara el flag de TypeInfo propuesto con un hook para personalizar el conjunto de metaclases seguras. Se considera terminado cuando un plugin puede marcar una metaclase como capaz de reenviar de forma segura las palabras clave de init_subclass y el ejemplo recibe comprobación de tipos.

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

Evaluación

Stack tecnológico
python
Área
compilers, devtools
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.