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
Spurious cycle detection when classes are only used in annotations #385
Comments
Thanks for the bug report! We should be able to fix this. Will write back with updates. |
@mwchase, I have a fix, but I just want to make sure this is a bug, instead of intended behavior. Can you help me think through it? It's possible to use type annotations at runtime. Popular libraries such as Pydantic do this. In such cases, could it be that marimo should indeed treat type annotations as names, not strings? In your specific case, you could replace the type annotations with actual strings, in which case the Python AST will contain For example, consider this toy notebook. In this case it could be argued that the cell defining import marimo
__generated_with = "0.1.56"
app = marimo.App()
@app.cell
def __():
class A:
"""hello world"""
return A,
@app.cell
def __(A, typing):
class B:
def use_type_annotations(self, a: A) -> None:
print(typing.get_type_hints(B.use_type_annotations)["a"].__doc__)
return B,
@app.cell
def __(B):
b = B()
return b,
@app.cell
def __(A, b):
b.use_type_annotations(A())
return
@app.cell
def __():
import typing
return typing,
if __name__ == "__main__":
app.run() Given this complication I am leaning toward "intended behavior", but I am not certain. Let me know what you think? |
Huh. I took that code, started messing with it, and got very confused. Taking a closer look, here's what happens if I change the example you gave to use Cell 1 no longer depends on Cell 0, but Cell 3 still depends on Cell 0, so the code in Cell 1 can properly resolve the types, when Cell 3 calls it. If I break the dependency from Cell 3 to Cell 0, I get undesirable behavior in the form of changes not propagating through the dependency graph. From a quick glance at Pydantic, I can see that it's not unreasonable to end up in a scenario analogous to "there is no dependency between Cell 3 and Cell 0". Unless someone comes up with a really compelling reason, probably the behavior should stay as-is (for maximum expressiveness within Python), but this probably merits some documentation. |
@mwchase , thanks for taking a look and for the thoughtful experiment.
Great, we're on the same page.
I like this suggestion — it's a good way to think about it. If dependencies are really needed, then best to not use strings, and declare the two dependent types/classes/functions in the same cell when possible. I will add documentation to this effect somewhere on Thanks again for the thoughtful and fun discussion! |
Describe the bug
I was defining some classes that have methods that depend on each other, in different cells, and marimo claimed that there were cyclic dependencies between cells. This makes it somewhat less convenient than I'd like to use annotations.
It appears that the code is parsed with annotations as strings, so any value that's only used in annotations shouldn't factor into dependency ordering.
Environment
Code to reproduce
The text was updated successfully, but these errors were encountered: