DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.18k stars 21 forks source link

False positives in reportShadowedImports rule for cython and mypyc compiled projects #705

Open oguzhanmeteozturk opened 1 month ago

oguzhanmeteozturk commented 1 month ago

When using basedpyright's reportShadowedImports rule in projects compiled with Cython or mypyc, there are recurring false positives where it incorrectly flags project-specific files as overriding standard library modules (e.g., typing, copy). This behavior impacts compiled Python projects, where the original source and the compiled modules coexist.

Context:

Projects that compile Python code using tools like Cython and mypyc often include both the compiled output (e.g., .so or .pyd files) and the original Python source files (e.g., .py) in the src directory or a build directory within the project. During this process, basedpyright appears to misinterpret these files as "shadowing" or overriding standard library modules, which isn't the case in a properly configured project.

Problem:

The issue typically manifests with false positives like:

basedpyright is detecting the compiled modules as if they were replacing core Python libraries, even though no actual replacement or interference with the standard library is happening at runtime. This causes unnecessary noise, making it harder to differentiate real issues from false positives.

Expected Behavior:

In projects using Cython, mypyc, or similar tools, basedpyright should recognize that the compiled modules are not overriding or shadowing standard library modules. The current shadowing detection fails to account for the context in which the compiled modules exist.

Example:

Consider a project that has a structure like this after compilation:

src/
    build/
        foo.cpython-linux-gnu.so
    project_name/
        __init__.py (imports typing)
        foo.py (imports copy)
        bar.py (imports itertools)

In this case, basedpyright reports false positives after compilation even though the compiled modules are just importing modules and do not replace or interfere with the actual standard library modules, or use the same module name.

Proposed Solution:

  1. basedpyright should be able to recognize the difference between imported modules and actual shadowing of standard library modules in the context of Cython and mypyc compiled projects.
  2. It would be beneficial if basedpyright could automatically detect and handle compiled modules, or provide a configuration option to specify directories where such false positives should be ignored.

This enhancement would reduce noise and improve accuracy for developers working on compiled Python projects without losing the benefits of static analysis.

Thank you for considering this issue, and for all the excellent work on basedpyright!

DetachHead commented 1 month ago

sorry i'm not really familiar with cython or mypyc. is the issue that basedpyright is checking compiled code? if so, does excluding those files by adding them to exclude solve the problem?

if that's not the issue, my follow up questions:

thanks