python / python/mypy

Any is excessively used as outer context for inference

Open
#8,760 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Consider this example:

from typing import Any
  
def foo(**kwargs: Any) -> None: ...

foo(bar=reveal_type([1])) # Revealed type is List[Any]

Such behavior causes many false negatives for plugins that provide precise type checking for various ORMs, this includes SQLAlchemy plugin, our internal plugins, and likely Django plugin.

My guess this was added so that an empty list in Any context is inferred as List[Any] instead of List[<nothing>], but I think this is counterproductive. If empty collections cause issues for plugins they can easily work around using private APIs to re-infer argument type in generated precise context (for example using has_uninhabited_component() and ctx.api.accept()). Or maybe we can provide public API for inference.

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

Reproduce the example with mypy and inspect how the Any outer context affects reveal_type([1]) in a **kwargs: Any call. Review the plugin re-inference APIs mentioned in the issue, including has_uninhabited_component() and ctx.api.accept(). Done means agreeing on and implementing a consistent inference behavior or public API, with regression coverage for the example and affected plugin scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.