-
Notifications
You must be signed in to change notification settings - Fork 769
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
Final annotation is not recognized across notebook cells #6455
Comments
Is it not works for jupyter notebook? I had set to |
It does work for me in the untitled case too though. Must be the typecheckingmode is not being applied for some reason to your notebook. |
Not work. I had tried both untitled or saved file. |
Can you include your logs after running 'Pylance : Start Logging'. It should tell us which workspace we think the notebook is in. |
Hope these helpful. 1.mp4 |
Hmm, I can reproduce it now too using workspace settings.json. Not sure why that matters. |
Yeah there's an error reading the settings for some reason. Here:
|
Figured out the problem internally. We're not propagating the imports from the first cell to the second cell. If you put the import in the same cell, the error shows up:
|
I can reproduce this with two python files (no need for a notebook). Should be a pyright issue? Create two files: exporting_file.py: from typing import Final importing_file.py from exporting_file import *
x: Final[int] = 1
x = 2 # should be an error here and there isn't This is basically the internal structure of how notebook cells are linked together. |
@rchiodo, you can't re-export In most languages, |
Hmm, that means in a notebook they have to be in the same cell then as our workaround for cells referencing each other doesn't work in this case. |
Transferring back to Pylance. We might have to special case this for notebooks then. |
@erictraut is it only TypeAliases that can't be reexported? Or might there be other cases? I mean functions and variables obviously work and normal imports as well. Like this is fine: # Exporting_file.py
import pytest as py # Importing_file.py
from exporting_file import *
py.foo() |
It's only the How is pylance currently making symbols from one cell visible to subsequent cells? Are you synthesizing a wildcard import statement for pyright, like you show in the above sample? |
Sort of? The binder binds them as implicit imports, but I'm having trouble finding how that affects the import resolver. Meaning how they are actually considered. Maybe we can tweak something in the import resolver for this special case. |
Oh it's the binding that's linking everything together. It's not the same as a wildcard import. It specifies the previous cell as the list of builtins. This makes all of the symbols in the previous cell count as builtin symbols for the next cell. |
OK, got it. I have some ideas for how to fix this, but they're going to require some explorations. I'll get back to you. |
I've had a chance to explore some options. The underlying issue here is that the binder needs to make some assumptions about I explored a couple of options:
Option 2 is more surgical, so I went with that solution. This will be included in the next release of pyright. @rchiodo, when you pull the code from pyright, please verify that it works with pylance's notebook support. You may want to add a pylance test case specifically for this. |
Thanks a lot. Will do. |
@erictraut I don't think the fix worked. I believe this code here is assuming there's more than one assignment in the same file: The reportRedeclaration doesn't get logged because the checker is only running on the one cell so the sawAssignment always starts as false. I'm not sure but could sawAssignment default to true if sawFinal is true? That would fix the problem, but maybe I'm misunderstanding what sawFinal means. |
…then attempting to overwrite it. This partially addresses microsoft/pylance-release#6455.
… is shadowing a `Final` variable declared by the builtins module or some other chained file. This addresses microsoft/pylance-release#6455.
…then attempting to overwrite it. This partially addresses microsoft/pylance-release#6455. Added check for an attempt to assign to a module-local variable if it is shadowing a `Final` variable declared by the builtins module or some other chained file. This addresses microsoft/pylance-release#6455.
…then attempting to overwrite it. This partially addresses microsoft/pylance-release#6455. (#9532) Added check for an attempt to assign to a module-local variable if it is shadowing a `Final` variable declared by the builtins module or some other chained file. This addresses microsoft/pylance-release#6455.
@rchiodo, that's a different problem than the one I fixed above. The problem I fixed previously allows the The problem you're highlighting involves a different scenario: defining a To understand why this is a problem, we need to consider how the "chained file" mechanism in pylance works. It models each cell as a different global module namespace and chains them together. It's similar to how the From the perspective of the type checker, each notebook cell is a separate module scope with its own symbol table. A variable assigned in a subsequent notebook cell results in a new symbol that shadows the same-named symbol of the earlier cells. This is also true in a non-notebook context with respect to I think it's reasonable to expect that if |
Environment data
v2024.9.2
Win11 23H2
Conda Python 3.11.9
Code Snippet
Repro Steps
Expected behavior
Show me some warning like: "you can't change constant".
Actual behavior
No warning.
Logs
The text was updated successfully, but these errors were encountered: