microsoft / microsoft/pyright

`reveal_type` should descend into anonymous types (such as inline `TypeDict`s), not show placeholder

Open
#10,753 0 comments 2 reactions 0 assignees View on GitHub
enhancement request
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

Assuming `enableExperimentalFeatures = true`, Pyright currently reports inline TypedDicts as follows:

```python
from typing import reveal_type, TypedDict

my_dict: TypedDict[{ "foo": TypedDict[{ "bar": str }] }] = { "foo": { "bar": "asdf" } } # uses PEP 764

reveal_type(my_dict) # reports `Type of "my_dict" is ""`, and similarly when hovering over `my_dict` in Pylance (or hovering over a function that returns an anonymous TypedDict`)
```

([Playground](https://pyright-play.net/?enableExperimentalFeatures=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMogCmAboQIYA2A%2BvAoQDRQAqihAJgCJIDGMAUHwhwqbHjABczVpzEBtAN5QARMDBglklnRm8FygEZkQGqAGcYIKAF8AutbsBeKIpVqTLw8clKyptsCVrIKgAYigAV1NCUygABQBRWKgAdgA2ABYBIlJKGlYACiERMQBKKFCCQhw8GIADLUIoMGBlItFeQKQYpQAeBp0YAD4lWsYyFDYzLCQKIwo4KAB3AAtCFChlsFIQVAxUOLhZlG5CPiA))

Needless to say, this is not very useful in practice.

**Describe the solution you’d like**
Part of the motivation behind PEP 764 is to not go through the hassle of giving every dictionary (sub-)structure a name, and to facilitate working with mere structures, possibly nested ones.

In light of this, I'd argue Pyright/Pylance should recursively expand the definition of anonymous TypedDicts and report the type of `my_dict` as `TypedDict[{ "foo": TypedDict[{ "bar": str }] }]`.

This would be similar to how TypeScript reports named vs. anonymous types:

```typescript
type MyNamedType = {
foo: { bar: string };
}

const obj_of_named_type: MyNamedType = { foo: { bar: "asdf" }}

const obj_of_anon_type: {
foo: { bar: string };
} = { foo: { bar: "asdf" }}

console.log(obj_of_named_type) // Hovering produces `const obj_of_named_type: MyNamedType`

console.log(obj_of_anon_type)
// Hovering produces:
// const obj_of_anon_type: {
// foo: {
// bar: string;
// };
// }
```

([Playground](https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiByBDAthAJgFXNAvFA3gFBQlQBmA9hQFwFQBGiATrQM7BMCWAdgOZQBfANyEBhQgGMK3dlAr0AVgH0KZJdxToloSLThJUmbFDz5yVWmcYsoAIkSs0ZW4LGTps+ctVLE3adrYlsSklDR01mwcPPzCoiZ0YZYMzLT2js6u4u4yFAA2EAB0eRS8ABReKmoahoGQAJQkAPRNUAASFABuEFx8UGBMFGgArhIQrFAABlIywHKKVeqaaHUQegjLWJCT2TOs+UUl5ZU+fgE6EPWELe1dPTH9gyNjrNTXrXtzJ2pn3KvBN1CFgI71IYJSNnYvV4IkBJDiNzEQA))

Contributor guide

Open the contributing guide

Research direction

Reproduce the report using the linked Pyright Playground with enableExperimentalFeatures=true, then trace the reveal_type and hover type-display paths responsible for anonymous TypedDicts. Done means nested anonymous TypedDict definitions are recursively shown instead of the placeholder, including for returned values.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.