python / python/mypy

TypeChecker.lookup_qualified can't find names in submodules

Open
#14,954 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the linked lookup_qualified implementation in mypy/checker.py and trace how ctx.api.named_generic_type resolves dotted names. Reproduce the nested-submodule case from the report with a plugin, then verify that qualified names such as foo.bar.baz.Qux can be found without relying on the undocumented TypeChecker.modules interface.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.