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

Runtime patching unsupported methods #1014

Closed
whitphx opened this issue Jul 14, 2024 · 2 comments · Fixed by #1148
Closed

Runtime patching unsupported methods #1014

whitphx opened this issue Jul 14, 2024 · 2 comments · Fixed by #1148

Comments

@whitphx
Copy link
Owner

whitphx commented Jul 14, 2024

  • time.sleep() -> await asyncio.sleep()

Such auto-conversion is already implemented in #965 for st.write_stream() that converts st.write_stream() to await st.write_stream(), so we can simply extend it.

@whitphx
Copy link
Owner Author

whitphx commented Jul 24, 2024

Converting lines inside functions (precisely, "code blocks") is difficult because they are undeterministic.

Here is an example where what sleep in foo refers to is dynamically determined at runtime.

def foo():
    sleep(1)

from time import sleep

foo()

sleep = lambda x: None  # no-op

foo()

On the other hand, the scope of the sleep can be determined by parsing the AST by the nearest-enclosing-scope policy.
The point is, the nearest-enclosing-scope is determined at build time (so the scope is deterministic), the name resolution is done by the scope at runtime.

Let's see what some existing static analyzers do:
Pylance, for example, parses the example above and infers the sleep as sleep: ((secs: float) -> None) | ((x: Any) -> None), a union type.

So, for this issue, it would be a solution to deal with only the case where the inferred name resolution can be determined as a single result. If so, the example above becomes not a target of this auto-conversion.

@whitphx
Copy link
Owner Author

whitphx commented Jul 30, 2024

  • Feature/ast patch await #1021 only implemented AST conversion of time.sleep() -> await asyncio.sleep(), and converting the normal functions is still todo.
  • Converting time.sleep() to await asyncio.sleep() can be replaced with patching time.sleep with asyncio.sleep and AST transformation from time.sleep() to await time.sleep(). This makes the AST processing simpler and looks better.

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

Successfully merging a pull request may close this issue.

1 participant