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

IDE confusion around functions calling each other? #2635

Open
zsiegel92 opened this issue Dec 10, 2024 · 4 comments
Open

IDE confusion around functions calling each other? #2635

zsiegel92 opened this issue Dec 10, 2024 · 4 comments

Comments

@zsiegel92
Copy link
Contributor

If modal function fn_a calls modal function fn_b, and fn_b also calls fn_a, pyright gets confused!

Screenshot 2024-12-10 at 12 42 40 AM

The screenshot is from what I think is a MWE of this bug:

import modal


app = modal.App()


@app.function()
def fn_a(call_b: bool = True):
    if call_b:
        fn_b.remote()
    return


@app.function()
def fn_b():
    fn_a.remote(call_b=False)


@app.local_entrypoint()
def main():
    fn_a.remote()

The program above ia valid and runs fine, but my IDE is having a problem with something. The issue is cryptic - fn_a.remote appears to the IDE to be undefined - but only when both functions call each other! If the functions don't both call each other, either one can call either one with .remote with no issues.

Using VS Code, Python 3.12.

@erikbern
Copy link
Contributor

Python typing expert @freider – any idea what's going on?

@freider
Copy link
Contributor

freider commented Dec 10, 2024

Weird! Since the issue only happens if both functions call each other, I would guess this has something to do with pyright/pylance internals rather than the Modal types.

Here is my best guess: pyright for some reason tries to determine the type of each function by looking inside the function body where there is a reference to the other function, which is then evaluated etc, causing a reference "loop" between the functions, which is probably terminated by pyright by just setting the type to "function".

I'm not sure why pyright would actually need to look at the function body to determine types here though, maybe it does so just to type the body... Maybe it would help to add return type annotations (-> None) so it can determine return type directly from the signature? I don't know anything about pyright internals I'm afraid

@freider
Copy link
Contributor

freider commented Dec 10, 2024

I just tested it, and adding the -> None return type annotation in your example seems to fix the issue! :)

@zsiegel92
Copy link
Contributor Author

@freider annotating one of the functions as -> X did indeed fix the bug, both in the toy example and in my actual problem code.

This was very weird and pretty hard to pin down. Thanks for validating and responding so quickly! 🙏 @erikbern @freider

FWIW I only experienced this with Python 3.12, I switched my IDE to 3.11 and did not have this problem (though can't just switch my project ofc). Probably not worth leaving an issue on the Pyright repo or the Pylance repo since I can't come up with a MWE without using modal.App.function-decorated functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants