Skip to content

Extra nesting for classes with embedded type alias #7089

@Antyos

Description

@Antyos

Describe the bug

Class definitions with embedded/local type aliases using TypeAlias or Python 3.13's type syntax get nested inside a cell, as if they had externally defined variables.

Why would one do this instead of just defining the type in app.setup? I don't have a great answer, but it's valid syntax, and since I found the bug I'm reporting it.

Expected

@app.class_definition
class EmbeddedTypeAlias:
    MyEmbeddedTypeAlias: TypeAlias = int

    def __init__(self, value: MyEmbeddedTypeAlias):
        self.value = value
return


@app.class_definition
class EmbeddedType:
    type MyEmbeddedType = int

    def __init__(self, value: MyEmbeddedType):
        self.value = value
return

Actual

@app.cell
def _():
    class EmbeddedTypeAlias:
        MyEmbeddedTypeAlias: TypeAlias = int

        def __init__(self, value: MyEmbeddedTypeAlias):
            self.value = value
    return


@app.cell
def _():
    class EmbeddedType:
        type MyEmbeddedType = int

        def __init__(self, value: MyEmbeddedType):
            self.value = value
    return

Will you submit a PR?

  • Yes

Environment

Details
{
  "marimo": "0.17.7",
  "editable": false,
  "location": "C:/Users/Andrew/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": "en_US",
  "Binaries": {
    "Browser": "--",
    "Node": "v22.21.0"
  },
  "Dependencies": {
    "click": "8.2.1",
    "docutils": "0.22",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.8.2",
    "narwhals": "2.2.0",
    "packaging": "25.0",
    "psutil": "7.0.0",
    "pygments": "2.19.2",
    "pymdown-extensions": "10.16.1",
    "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": "5.5.0",
    "duckdb": "1.3.2",
    "loro": "1.5.4",
    "mcp": "1.18.0",
    "nbformat": "5.10.4",
    "openai": "1.102.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": "27.9.0",
    "watchdog": "6.0.0"
  },
  "Experimental Flags": {}
}

Code to reproduce

import marimo

__generated_with = "0.17.7"
app = marimo.App(width="medium")

with app.setup:
    from typing import TypeAlias

    MySetupTypeAlias: TypeAlias = int
    type MySetupType = int


@app.class_definition
class SetupTypeAlias:
    def __init__(self, value: MySetupTypeAlias):
        self.value = value


@app.class_definition
class SetupType:
    def __init__(self, value: MySetupType):
        self.value = value


@app.cell
def _():
    class EmbeddedTypeAlias:
        MyEmbeddedTypeAlias: TypeAlias = int

        def __init__(self, value: MyEmbeddedTypeAlias):
            self.value = value
    return


@app.cell
def _():
    class EmbeddedType:
        type MyEmbeddedType = int

        def __init__(self, value: MyEmbeddedType):
            self.value = value
    return


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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions