microsoft / microsoft/pyright

Function return type depends on previous calls

Open
#11,035 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

Function type inference changes based on previous calls of the same function. [Playground example](https://pyright-play.net/?code=MYGwhgzhAEBKCmAHAlmALvATgLgFDQOgBN4AzaeYACwHsBtAFQF0AKCeEUgGmgA9toDAJTQAtAD5BeQjOiZ4aAK6YAdn1y4S5Ui148AnkOmF5S1XwB0lWi0MbM0ALxwkqDJhZDcYJ9B2YeAEYRWQIAYmgBZBU0XAAjX38eACIaFXhkkPDI6Gi0aAAfaAg0AOg4xXzkckR5ADdkGkUYEGj4XJh5AFsaOvgiHJLMIA)
```python
class Repiater:
def echo[T](self, x: T) -> T:
return x

def f(x, y):
return x.echo(y)

r = Repiater()
a = f(r, 1) # : int
b = f(r, "one") # : int | str, but if previous line is removed : str
```
The inferred type of `f`’s return value depends on whether earlier calls exist. Not necessarily a bug, but unexpected behavior.

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 linked Playground example and reproduce the inferred return types for f with and without the earlier call. Trace how Repiater.echo and f are inferred, then determine whether the result is expected or a bug; done means the behavior is explained and any required change has a regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.