pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Pyrefly forgets (?) a type in the middle of a function definition

Open bigfootjon opened this issue 8 months ago • 5 comments

Describe the Bug

I assume this must be a known problem but I don't even know how to find it, or reduce this repro further

See sandbox link. Why is an error reported for func but not func2?

Sandbox Link

https://pyrefly.org/sandbox/?code=EYQwTgXABAzgLmKBeKAiUZUCgsBMCmAZlIQK4B2AxgBQCUEWUTJA9i9PIgD5QByL5fMj4D8jZuKaEWiAB5QAluSgBtALoNmW1ogCei5es3aTABxAwYOLaABeHBMOksoMqBhwFiZKgCY6xlJsDtwigsL8gtbM0nIGqhqS2uaW0Ux2IU5srogYQA

(Only applicable for extension issues) IDE Information

No response

bigfootjon avatar May 16 '25 23:05 bigfootjon

I have another example of what might be the same underlying bug?

https://pyrefly.org/sandbox/?code=CYUwZgBGD20BQDcCGAbAXBAlgOwC4QB8IA5abEASggFoA+LPNAKAla0mRSwGcSyRmbIRFwAnAJ6Dh00SFwBXUdggBGFtIggAHgGMQAB1xSNbWQqUQATOtZnFyzkyahIMaJcSoMOfEVLkqOgYjG3YITh4+cmNhMUlQkzsLNRMwHFQUeJMhJOVrHLl7cNQgA

I can submit this separately if preferred

bigfootjon avatar May 17 '25 19:05 bigfootjon

This is super weird! Let me investigate!

ndmitchell avatar May 19 '25 20:05 ndmitchell

These bugs are quite different. Breaking down the second:

def foo(val: int | None, b: bool) -> int:
    if val is None:
        if b:
            return 1
        else:
            return 2
    return val

We get this wrong too, failing to infer the return type.

ndmitchell avatar May 19 '25 21:05 ndmitchell

I've fixed the second issue. So no need to split it up.

ndmitchell avatar May 19 '25 21:05 ndmitchell

The first bug is just that we create a Var, and when doing or short circuit stuff we forgot to expand Var. Doing that solves the issue.

ndmitchell avatar May 19 '25 21:05 ndmitchell