microsoft / microsoft/pyright

Improvement to type controls: apply across statement rather than line

Open
#10,977 0 comments 0 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

I'm sure this has been asked before but I couldn't find the existing issue to comment on.

**Is your feature request related to a problem? Please describe.**
Line-level type controls fight with the formatter

Consider the following line. I use pandas as an example, but this really applies to whenever we have to work with
untyped libraries.
```
valid_df = cast(pd.DataFrame, dataframe[dataframe.valid_execution])
```

Let's add a ` # pyright: ignore[reportUnknownMemberType]` to suppress the error

```
valid_df = cast(pd.DataFrame, dataframe[dataframe.valid_execution]) # pyright: ignore[reportUnknownMemberType]
```
The line is too long, so let's hit save and autoformat:

```
valid_df = cast(
pd.DataFrame, dataframe[dataframe.valid_execution]
) # pyright: ignore[reportUnknownMemberType]
```
Now that `# pyright: ignore` has been moved to a different line, it no longer suppresses the error
caused by the previous. Furthermore, I am now warned about an unnecessary type ignore 👎

Let's move the type suppression again to the correct line

```
valid_df = cast(
pd.DataFrame, dataframe[dataframe.valid_execution] # pyright: ignore[reportUnknownMemberType]
)
```
Hit save and autoformat:
```
valid_df = cast(
pd.DataFrame,
dataframe[
dataframe.valid_execution
], # pyright: ignore[reportUnknownMemberType]
)
```
...and now the type ignore is on the wrong line again. Finally, we can make one more modification:
```
valid_df = cast(
pd.DataFrame,
dataframe[
dataframe.valid_execution # pyright: ignore[reportUnknownMemberType]
],
)
```
and it sticks this time. But my one-liner cast statement has turned into 5 lines, one of which nests across brackets.

**Describe the solution you’d like**
It would be nice if the result after the first auto-format suppressed the error in the entire statement, rather than the line itself:
```
valid_df = cast(
pd.DataFrame, dataframe[dataframe.valid_execution]
) # pyright: ignore[reportUnknownMemberType]
```
This would be useful even in the most limited scope, ie, across parenthesis only, because `black`/`ruff` typically break apart long lines by adding parenthesis.

Contributor guide

Open the contributing guide

Research direction

No implementation files, tests, or entry points are named. Start by reproducing the examples with pyright and a formatter, then trace how line-level type ignores are associated with diagnostics. Done means a suppression placed after the formatted statement applies across that statement without producing an unnecessary-ignore warning.

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.