basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

Treat normal python file as IPython / Jupyter

Open kiyoon opened this issue 9 months ago • 4 comments

Description

It's a pretty common practice to save ipynb files as a python file with Jupytext.

However, when await keyword is used, (based)pyright reports that it's only allowed in an async function. Apparently in Jupyter Notebook you can run an async function like await foo() in a cell directly.

So, is there a way to trick pyright into thinking that a normal python file is actually a Jupyter Cell?

The original issue is from https://github.com/kiyoon/jupynium.nvim/issues/136. The plugin synchronizes a python file to Jupyter Notebook so you can use neovim to edit and Jupyter Notebook to preview the result.

kiyoon avatar Mar 21 '25 01:03 kiyoon

If this isn't possible, it would be good to make it possible to ignore the await error by assigning it a report code.

kiyoon avatar Mar 21 '25 01:03 kiyoon

hmm looks like pylance doesn't support this either, but i'm happy to.

since this is a normal python file i think this needs to also be a new diagnostic rule to prevent accidentally importing a .py file with # %% comments in them from a regular python file. otherwise it would be unsafe for basedpyright to assume it will only ever be executed as a notebook

If this isn't possible, it would be good to make it possible to ignore the await error by assigning it a report code.

top-level await errors are currently reported under reportGeneralTypeIssues. or do you mean it should be a more specific rule? i agree, but i haven't thought of a way to split errors up into separate rules in such a way that doesn't break backwards compatibility with pyright.

DetachHead avatar Mar 23 '25 14:03 DetachHead

Detecting # %% might be too erroneous since a normal python file could contain it for some reason.. I'm not so sure if all third party libraries I installed will absolutely not have the comment.

Maybe it could be configured instead with a file pattern? For example jupyterFilePatterns = ["tools/**/*.ju.py"] kind of setting.

kiyoon avatar Mar 24 '25 01:03 kiyoon

do you mean it should be a more specific rule? i agree, but i haven't thought of a way to split errors up into separate rules in such a way that doesn't break backwards compatibility with pyright.

Okay, let's not break the compatibility then.

kiyoon avatar Mar 24 '25 01:03 kiyoon