Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

PLS doesn't flag error in a relative import if it is found in another root directory #509

Closed

Description

@erictraut

The analyzer incorrectly accepts relative imports in cases where the path is relative to a root but not to the importing file.

Consider the following case:
Directory structure:
src
|->module
|--->init.py
test.py

PYTHONPATH=src

test.py contains the following code:

from .module import Foo

This import should be flagged as invalid because the path ".module" should be relative to the test.py file. However, the code in PathResolverSnapshot.GetImportsFromRelativePath considers all of the root search directories when it performs its search. It therefore finds an import match when it shouldn't.

I think the problem is in PathResolverSnapshot.RootContains. It doesn't do anything to validate that the specified lastEdge starts in the same location as the root. If the relative path happens to match items within that root, the method returns true. If the relative path is zero length, all roots return true.

A proposed fix is to add the following piece of code to PathResolverSnapshot.RootContains before the while loop:

if (root != sourceEdge.Next.Start) {
    rootEdge = default;
    return false;
}

Activity

  1. AlexanderSher commented on Dec 31, 2018

    @AlexanderSher
    Contributor

    The problem isn't related to the multiple roots. With "python.autoComplete.extraPaths": ["path_to_folder/src"] and the following file structure:

    ├─ package
    │  └─ test.py
    └─ src
       └─ package
          └─ module
             └─ __init__.py
    

    code from .module import * in test.py is valid.

    But, from .module is invalid when test.py is in root, no matter if we have module or not.

  2. AlexanderSher commented on Dec 31, 2018

    @AlexanderSher
    Contributor

    We probably should have an exception for the working directory, cause it can be a top directory of the module.

  3. added 2 commits that reference this issue on Jan 2, 2019
    ab15e1e
    23f6d67
  4. added 2 commits that reference this issue on Feb 4, 2019
    00aebad
    a5b6e5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions