pylance reportPrivateImportUsage
When using VS Code, pylance (v2024.7.1, latest) frequently reports reportPrivateImportUsage errors when importing classes.
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"]
@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
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:
- Most of the imports from crawlee get tagged with false
reportPrivateImportUsagewarnings. - Users don't get code completion when writing import statements. Suggestions in the image below should be
HttpCrawler,HttpCrawlingContextandHttpCrawlingResult.
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.
Yeah tried __all__ seems working.