python / python/mypy

Plugin api named_type() is ambiguous and inconsistent and broken

Open
#6,578 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-1-normal refactoring topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

The plugin API defines two different interfaces (TypeAnalyzerPluginInterface and SemanticAnalyzerPluginInterface) that each define a method named_type(). These look almost identical, but looks are deceptive.

First, the specified signatures differ subtly:

  • The second argument to SemanticAnalyzerPluginInterface.named_type() is optional, while for TypeAnalyzerPluginInterface.named_type() is is mandatory.
  • Also the first argument name differs (qualified_name vs. plain name).

Second, the implementations differ:

  • The semantic analyzer (in semanal.py) starts the lookup in the local namespace, then globals, then builtins.
  • The type checker (in checker.py) has two different strategies: if there's no dot in the name, it starts in globals, then builtins; but if there's a dot, it starts in the table of modules (i.e. the current scope doesn't enter into it).

Third, the type checker's named_type() implementation doesn't actually take a list of type arguments at all -- it's a method with only one argument, the name to look up. It always fills in all type parameters with Any. (It looks like the method we want here is named_generic_type().)

Fourth, apparently mypy's own type checking doesn't catch the discrepancy between the implemented spec (only a name) and the interface spec (name + args).

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 by comparing TypeAnalyzerPluginInterface.named_type() and SemanticAnalyzerPluginInterface.named_type() with the implementations in semanal.py and checker.py. Inspect named_generic_type() and the type-checking setup to understand why the signature discrepancy is not caught. Done means the plugin interfaces and implementations have a consistent, agreed API and the mismatch is covered by type checking or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.