pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Slowdown when enabling reportShadowedImports checks

Open correctmost opened this issue 9 months ago • 4 comments

Is your feature request related to a problem? Please describe.

I have a private codebase with ~2300 Python files. Most of the files have repeated import blocks like this:

from collections.abc import Iterable
from typing import override

from src.resources.results.result import Resource
from src.resources.sites.pages.page import Page
...

When I enable the reportShadowedImports checks, Pyright runs increase from ~13.5 seconds to ~28 seconds.

Describe the solution you’d like Given the repetitive nature of the imports, I am hoping that it's possible to speed up or avoid duplicate shadow checks.

Steps to reproduce I have prepared a sample repo that illustrates the slowdown:

git clone --branch shadowed-imports-slowdown https://github.com/correctmost/pyright-corpus.git
cd pyright-corpus

# Run with reportShadowedImports already enabled
time npx pyright

--> Toggle reportShadowedImports off in pyrightconfig.json

# Re-run with reportShadowedImports disabled
time npx pyright

Thanks!

correctmost avatar Mar 07 '25 01:03 correctmost

This test can be quite expensive on large code bases with complex import dependency graphs. I've already spent quite a bit of time optimizing it, but I'll take a closer look at your sample to see if there's anything I can do to speed it up further. I just want to set expectations that there probably won't be much, if anything, that I can do.

You probably know this already, but this check is not on by default — even in strict mode. That's for good reason. If you find that the value it provides is not worth the time it takes, you can leave it disabled.

I'll also note that it's arguably not the job of a type checker to be reporting import cycles in your code. I added the check very early in pyright's evolution because the team I was managing at the time had a large and complex code base with a number of problematic import cycles, and I couldn't find other tools at the time that would detect them. There may be other tools available now.

erictraut avatar Mar 07 '25 06:03 erictraut

I misspoke above. I mixed things up and thought you were talking about reportImportCycles, which is intrinsically an expensive test. You're talking about the reportShadowedImports, which is different. I wouldn't expect that to be expensive. This check is disabled by default in pyright but is enabled by default in pylance. It was implemented by the pylance team, so I'll leave it to them to investigate and see if there's a way to speed it up.

@rchiodo, could you or someone else on the pylance team take a look at this? Feel free to transfer to the pylance-release project if you'd prefer to track it there.

erictraut avatar Mar 08 '25 16:03 erictraut

I think this issue can be closed now that reportShadowedImports support has been removed from Pyright: https://github.com/microsoft/pyright/pull/10891

correctmost avatar Sep 08 '25 16:09 correctmost

We usually close Pylance issues when the Pyright code gets pushed across, but we may reimplement this a different way that doesn't have the perf impact.

rchiodo avatar Sep 08 '25 16:09 rchiodo