Skip to content

pylance reportPrivateImportUsage #283

Closed

Description

@zigai

When using VS Code, pylance (v2024.7.1, latest) frequently reports reportPrivateImportUsage errors when importing classes.
image

These errors can be fixed by defining a __all__ list in the module's __init__.py file, which explicitly declares the public names for the module.

Here's an example for the http_crawler module:

from .http_crawler import HttpCrawler
from .types import HttpCrawlingContext, HttpCrawlingResult

__all__ = ["HttpCrawler", "HttpCrawlingContext", "HttpCrawlingResult"]

Activity

  1. added
    t-toolingIssues with this label are in the ownership of the tooling team.
    on Jul 10, 2024
  2. vdusek commented on Jul 12, 2024

    @vdusek
    Collaborator

    @zigai Thanks for opening the issue. It's interesting because I also use Pylance v2024.7.1 and haven't encountered this problem.

    Additionally, I believe the __all__ field is intended just for "star imports" statements, such as:

    from crawlee.http_crawler import *

    am I correct?

    Because the statement Pylance tells you isn't true. HttpCrawler is exported, and can be imported directly:

    from crawlee.http_crawler import HttpCrawler
  3. zigai commented on Jul 12, 2024

    @zigai
    Author

    Pylance type checking mode has to be turned on to see these warnings. It's set to "off" by default, which may be why you don't see them.

    The warnings pylance shows aren't true, all imports work correctly on the Python level, but beacuse pylance has problems seeing the actual structure of the module, the user experience for using crawlee inside VS Code is not as good as it could be:

    1. Most of the imports from crawlee get tagged with false reportPrivateImportUsage warnings.
    2. Users don't get code completion when writing import statements. Suggestions in the image below should be HttpCrawler, HttpCrawlingContext and HttpCrawlingResult.

    image

    It's true that the __all__ field is meant for star imports in Python, but some other tools use it for different purposes like determining the public API of a package, which exaplainds why adding it fixes both the warnings and code completion.

    If you are able to replicate these problems keep in mind that this solution is more of a workaround, pylance should work without __all__ being defined. This might actually be a problem with how the package is being built.

  4. visrut-at-incubyte commented on Jul 16, 2024

    @visrut-at-incubyte

    Yeah tried __all__ seems working.

  5. self-assigned this
    on Jul 16, 2024
  6. added a commit that references this issue on Jul 16, 2024
    09d7203
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working.t-toolingIssues with this label are in the ownership of the tooling team.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions