python / python/mypy

[Plugins] Access value of a StarExpr

Open
#12,651 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi folks, I'm trying to add support for StarExpr in a plugin but I'm unable to figure out the value of the StarExpr, here's an example of the code I want to make a plugin for:

def fake_union(name: str, types: Tuple[Types, ...]) -> Union[Types]:
    ...

UserErrors = (A, B)

Response = fake_union(types=(User, *UserErrors), name="Response")

I don't know if this is possible to figure out using MyPy, so far I was only able to fetch the Var definition, but not the type

def union_hook(ctx: DynamicClassDefContext) -> None:
    types_expr = ctx.call.args[ctx.call.arg_names.index("types")]

    assert isinstance(types_expr, TupleExpr)

    for type_ in types_expr.items:
        if isinstance(type_, StarExpr):
            assert isinstance(type_.expr, NameExpr)
            name = type_.expr.name

            # how can I find the value of this expr?
            print(name)

I made a repo with a full example here: https://github.com/patrick91/mypy-star-plugin

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 with the plugin example repository and the DynamicClassDefContext, TupleExpr, StarExpr, and NameExpr entry points shown in the issue. Trace how the plugin can inspect the StarExpr for UserErrors, then verify that the example can access the expanded value when processing the types argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.