-
Notifications
You must be signed in to change notification settings - Fork 384
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
Make generated Python files mypy
error-free
#2325
Comments
Related is the idea of a marimo specific lint: #1543 Let us know if you come across any other type checking/ static analysis issues particular to mypy and marimo |
@shunichironomura - I dont think this is an alternative or solution, but you can rename cells (in the cell dropdown menu), which is the name given in the generated python file |
Another mypy/marimo issue that I encountered is that mypy cannot infer the module imported in a different cell. Here's a minimal example: import marimo
__generated_with = "0.8.15"
app = marimo.App(width="medium")
@app.cell
def __():
import pathlib
return pathlib,
@app.cell
def __(pathlib):
def read_file(file: pathlib.Path) -> str:
return file.read_text()
return read_file,
if __name__ == "__main__":
app.run() Running
import marimo
__generated_with = "0.8.15"
app = marimo.App(width="medium")
@app.cell
def __():
from pathlib import Path
return Path,
@app.cell
def __(Path):
def read_file(file: Path) -> str:
reveal_type(file) # Any
return file.read_text()
return read_file,
if __name__ == "__main__":
app.run() Still, there are cases where I want to import the module (e.g., My current workaround is to modify the cell like this: from typing import TYPE_CHEKCING
if TYPE_CHECKING:
import pathlib as _pathlib
def read_file(file: _pathlib.Path) -> str:
return file.read_text() Maybe this is a known issue, I haven't searched the issues, but let me know if you want to open a new issue for this. |
Oh I didn't know about that, thank you. I agree that it cannot be the solution, but it's good to know. |
Another option is to make a mypy plugin for marimo? I don't know much about what's possible with mypy plugins, but it may solve these issues without changing the generated Python files. |
Whatever is done here, do bear in mind that there are other static type-checkers beyond mypy; most notably pyright. (Which personally I much prefer, as it seems to have far fewer edge cases.) If I had to give my +1 to something, it would be to give each cell a unique name. This would also avoid trigger spurious Ruff lints like N807. |
Wanted to bump this issue. This issue is causing our CI/CD to fail (with both Is there a reason cells can't simply be serialized in the file as |
Thank you for creating marimo and congratulations for the seeding round! The experience has been pleasant. |
The simplest solution might be best. I'd propose we enumerate the cells: @app.cell
def __1():
pass
@app.cell
def __2():
pass Users can rename cells, but if they are prefixed with two underscores and a number, we will assume they were generated by marimo. Similar to @wasimsandhu's suggestion, but a bit easier to know if the name was user-generated |
Description
When running
mypy
on marimo notebooks,mypy
reportserror: Name "__" already defined on line X [no-redef]
errors because cells in marimo notebooks are all defined as functions with the same name:__
. This makes it difficult to properly type-check marimo notebooks.Suggested solution
There are at least three approaches to resolve this:
# type: ignore[no-redef]
comments to lines containing@app.cell
to suppress errors. This approach has probably the fewest side effects but makes the files a bit noisy.#mypy: disable-error-code="no-redef"
comment. This approach is probably the least noisy but possibly suppresses otherno-redef
errors in the file.Alternative
No response
Additional context
As far as I know, there is no way to configure
mypy
to ignore a certain error in specific files (likeper-file-ignores
config in Ruff) via configuration files such aspyproject.toml
. So it's impossible to suppress theno-redef
errors in marimo notebooks without modifying the notebook files.So my current workaround is to take approach 3 and manually adding the top-level
#mypy: disable-error-code="no-redef"
comment to every marimo notebook.The text was updated successfully, but these errors were encountered: