Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autotyping fails to account for implicit returns #8942

Closed
charliermarsh opened this issue Dec 1, 2023 · 2 comments · Fixed by #8952
Closed

Autotyping fails to account for implicit returns #8942

charliermarsh opened this issue Dec 1, 2023 · 2 comments · Fixed by #8952
Assignees
Labels
bug Something isn't working

Comments

@charliermarsh
Copy link
Member

Given:

def func(x: int):
    if x:
        return 1

We infer a return type of int, rather than Optional[int].

@charliermarsh charliermarsh added the bug Something isn't working label Dec 1, 2023
@charliermarsh charliermarsh self-assigned this Dec 1, 2023
@charliermarsh
Copy link
Member Author

@MichaReiser - Do you think it's feasible to use the unreachable code graph to implement this?

@MichaReiser
Copy link
Member

@MichaReiser - Do you think it's feasible to use the unreachable code graph to implement this?

Using the control flow graph is an option. Although I fear that building the control flow graph just for this might be a bit overkill because we aren't interested in control flows, but only care about exit nodes of the function. I would need to look into it again but from what I remember is that Hindley milner is AST based. We could potentially implement a subset of it. But it heavily depends on how accurate the type analysis should be.

charliermarsh added a commit that referenced this issue Dec 1, 2023
## Summary

Adds detection for branches without a `return` or `raise`, so that we
can properly `Optional` the return types. I'd like to remove this and
replace it with our code graph analysis from the `unreachable.rs` rule,
but it at least fixes the worst offenders.

Closes #8942.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants