Warn about shadowing stdlib modules #2619
Replies: 9 comments 3 replies
-
The challenge here is that this situation isn't a false positive. It's not an error to shadow a module in stdlib. Furthermore, the set of stdlib imports continues to grow each release, so a code base that doesn't shadow a stdlib module might suddenly find itself doing so. I don't think we could enable such a check by default. That means it would need to be an optional check that users need to enable if they want to use it, but novice Python users wouldn't know to enable it. |
Beta Was this translation helpful? Give feedback.
-
You're right that false-positives are possible as there are legitimate reasons to shadow the stdlib (I've done it myself). But the vast majority of folks who do it are doing it by accident. So being opt-out might be a pragmatic solution. And yes, the stdlib is not static over Python's lifetime. But the list of names per Python version is static and Pylance typically knows what version of Python is being run. And in the cases where Pylance doesn't know you can either worst-case it with all historical names or the newest version of Python released. I've let the Jupyter team know about this issue as they have brought this specific case up before as an issue their users run into regularly. |
Beta Was this translation helpful? Give feedback.
-
We probably have telemetry on how often this prevents a user from running a jupyter kernel. But we've had a bunch of issues come in that ended up being this exact problem. We've added our own warning in the jupyter extension, but pylance can do a much better job. |
Beta Was this translation helpful? Give feedback.
-
/cc @DonJayamanne |
Beta Was this translation helpful? Give feedback.
-
Agreed, when we attempted to add a solution for this in the Jupyter extension, thats the approach we took (& made the same assumption).
As mentioned by Rich, in the case of Jupyter, if a novice user creates a simple file named
I'd need to see if we have this today. If not, i'll add this. |
Beta Was this translation helpful? Give feedback.
-
We've seen some beginners face this issue on user studies in the past for Python in general. I do think it would be good to warn users about this issue by default, and allow more advanced users to dismiss the warning. |
Beta Was this translation helpful? Give feedback.
-
I am seeing reportShadowedImports even when nothing is being shadowed in the file. Working in a large project where I'm the owner of one part and not all. In another part of the project there's a file called "typing.py" (shadowing the stdlib typing) which has a lot of standard type classes for the project. That might be a problem for them to consider, but I can use it without shadowing the stdlib library. When I do:
I get a reportShadowedImports error, with the suggestion to rename |
Beta Was this translation helpful? Give feedback.
-
How do we disable this on a per file or per import basis? Specifically |
Beta Was this translation helpful? Give feedback.
-
I just ran into a case where PyLance claimed that |
Beta Was this translation helpful? Give feedback.
-
Beginners often make the mistake of shadowing a module in the stdlib with their own code (e.g. creating a module named
queue.py
will break debugpy).Since Pylance has the knowledge of what's in the stdlib as well as the names of the modules the user has, it could provide a diagnostic at either/both the import line that is getting shadowed or in the module causing the shadowing.
Beta Was this translation helpful? Give feedback.
All reactions