marimo icon indicating copy to clipboard operation
marimo copied to clipboard

`@mo.cache` fails with dataclass using a type alias type

Open Antyos opened this issue 4 months ago • 4 comments

Describe the bug

I get an AttributeError: 'Name' object has no attribute 'startswith' when using @mo.cache on a function that creates a dataclass which has a field with a TypeAliasType type.

See example code below.

Will you submit a PR?

  • [ ] Yes

Environment

{
  "marimo": "0.23.14",
  "editable": false,
  "location": "C:/Users/avg170002/Code/project/code/.venv/Lib/site-packages/marimo",
  "OS": "Windows",
  "OS Version": "11",
  "Processor": "Intel64 Family 6 Model 198 Stepping 2, GenuineIntel",
  "Python Version": "3.13.9",
  "Locale": "--",
  "Binaries": {
    "Browser": "150.0.7871.115",
    "Node": "v22.23.1",
    "uv": "0.9.26 (ee4f00362 2026-01-15)"
  },
  "Dependencies": {
    "click": "8.2.1",
    "docutils": "0.22",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.8.2",
    "narwhals": "2.21.0",
    "packaging": "25.0",
    "psutil": "7.0.0",
    "pygments": "2.19.2",
    "pymdown-extensions": "10.21.2",
    "pyyaml": "6.0.2",
    "starlette": "0.47.3",
    "tomlkit": "0.13.3",
    "typing-extensions": "4.15.0",
    "uvicorn": "0.35.0",
    "websockets": "15.0.1"
  },
  "Optional Dependencies": {
    "altair": "6.1.0",
    "anywidget": "0.9.21",
    "duckdb": "1.3.2",
    "loro": "1.10.3",
    "mcp": "1.18.0",
    "nbformat": "5.10.4",
    "openai": "2.30.0",
    "pandas": "2.3.2",
    "polars": "1.32.3",
    "pyarrow": "21.0.0",
    "pytest": "8.4.1",
    "python-lsp-ruff": "2.2.2",
    "python-lsp-server": "1.13.1",
    "ruff": "0.12.11",
    "sqlglot": "30.7.0",
    "vegafusion": "2.0.3",
    "watchdog": "6.0.0"
  },
  "Experimental Flags": {}
}

Code to reproduce

import marimo

__generated_with = "0.23.14"
app = marimo.App()

with app.setup:
    from dataclasses import dataclass
    from typing import Literal

    import marimo as mo

    type Mode = Literal["train", "eval"]


@app.cell
def _():
    # Note the difference in types
    type MyModeA = Literal["train", "eval"]
    MyModeB = Literal["train", "eval"]

    type(MyModeA), type(MyModeB)
    return


@app.class_definition
@dataclass
class MyConfig:
    x: Mode = "train"


@app.function
@mo.cache
def my_func(config=None):

    if config is None:
        config = MyConfig()
    return config.x * 2


@app.cell
def _():
    my_func()
    return


if __name__ == "__main__":
    app.run()

Antyos avatar Jul 15 '26 21:07 Antyos

I can prepare a focused fix with a regression test for this TypeAliasType cache-analysis failure. The likely change is limited to making the annotation traversal tolerate AST Name nodes rather than assuming strings. Could a maintainer confirm the intended behavior and mark this ready for a PR if that scope is appropriate?

AkhilTrivediX avatar Jul 16 '26 10:07 AkhilTrivediX

@AkhilTrivediX that's about right. I think there should be a bit more defensive programming around it in general- but in this case for Name we can extract the respective identifier

dmadisetti avatar Jul 16 '26 18:07 dmadisetti

Thanks, that confirms the intended path. I'll update the traversal to extract identifiers from Name nodes and add coverage for the cache-analysis case along with nearby non-string annotation nodes.

AkhilTrivediX avatar Jul 17 '26 09:07 AkhilTrivediX

I reviewed the contribution requirements and found that this repository requires a contributor agreement before a PR can proceed. I won't open a patch without completing that requirement, so I’m stepping back to avoid creating work the project cannot accept.

AkhilTrivediX avatar Jul 17 '26 09:07 AkhilTrivediX