TypeChecker.lookup_qualified can't find names in submodules

Abierto
#14,954 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
compilers

Línea de trabajo

Comienza en la implementación vinculada de lookup_qualified en mypy/checker.py y sigue cómo ctx.api.named_generic_type resuelve nombres con puntos. Reproduce con un plugin el caso de submódulo anidado del informe y, después, verifica que se puedan encontrar nombres cualificados como foo.bar.baz.Qux sin depender de la interfaz no documentada TypeChecker.modules.

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

Descripción

bug topic-plugins

Bug Report

Writing my first mypy plugin, I tried to use the exposed interface ctx.api.named_generic_type to look up a type in my package by name, which is in a submodule like:

ctx.api.named_generic_type('foo.bar.baz.Qux', [])

In this case ctx is a FunctionContext but it applies to most (all?) relevant plugin hook contexts.

The name is not found because foo.bar.baz is a submodule.

To Reproduce

See above.

Expected Behavior

Since this is the only public API for looking up named types, it should be possible--especially for plugins that could be intended for some specific third-party libraries--to be able to look into nested module namespaces.

Actual Behavior

The current implementation uses lookup_qualified which as written assumes that only the first component of a dotted name is necessarily a module name. It then looks into the symbol table for that module to find subsequent name components. This works fine for type lookups like typing.Type but not for other cases, as there is no guarantee that a submodule name is found in its parent module's namespace.

Probably should check the symbol table first, and if not found try the next component as a module name.

To work around this, have to dig into the undocumented TypeChecker.modules to find the required type.

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

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.

Más de python/mypy

Todos los issues de python/mypy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.