Skip to content
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

Slowdown when enabling reportShadowedImports checks #7025

Open
correctmost opened this issue Mar 7, 2025 · 2 comments
Open

Slowdown when enabling reportShadowedImports checks #7025

correctmost opened this issue Mar 7, 2025 · 2 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@correctmost
Copy link

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!

@erictraut
Copy link
Contributor

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 erictraut added the needs investigation Could be an issue - needs investigation label Mar 7, 2025
@erictraut
Copy link
Contributor

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 erictraut removed the needs investigation Could be an issue - needs investigation label Mar 8, 2025
@debonte debonte transferred this issue from microsoft/pyright Mar 8, 2025
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Mar 8, 2025
@debonte debonte assigned rchiodo and unassigned bschnurr Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants