You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
reportPrivateImportUsage confused by library source #2392
Describe the bug
The Library rich (pip install rich) currently generates the reportPrivateImportUsage error:
/home/gregory/Downloads/testcase/pytest/ricchy.py:5:16 - error: "Live" is not exported from module "rich.box.live" (reportPrivateImportUsage)
but this error gets the source library wrong in case other libraries in the namespace were imported first. The expected error is from module "rich.live" and not "rich.box.live".
VS Code extension or command-line
pyright 1.1.175 commandline Linux
Additional context
I'd like to help get the library in line. There's an issue tracking the problem for tables in the library, and as I mentioned in the bug introducing this error I believe it's due to spurious usage in __main__.py, but I'm actually not sure why this error is triggering on Live in the library at all as I couldn't find a usage pattern matching the documentation. It isn't related to the bug, but I would appreciate any assistance if you have the time.
Although it's not immediately obvious, this is the same as this bug: #2372. I fixed it earlier today, and the fix will be included in the next release of pyright. I confirmed that your sample above works without generating an error. Apologies for any inconvenience this bug caused you.
While 1.1.176 did fix the other issues (thanks!), it did not resolve the title issue for me (nor the sample). Apologies if this is some sort of delayed update or caching thing, but I did recreate venvs and such to be sure. See output below that includes running --version first then running on the test file from the issue.
~/Downloads/testcase/pytestrich-pyright ❯ pyright --versionnpx: installed 1 in 2.042spyright 1.1.176~/Downloads/testcase/pytestrich-pyright ❯ pyright ~/Downloads/testcase/pytest/ricchy.pynpx: installed 1 in 2.021sNo configuration file found.No pyproject.toml file found.stubPath /home/gregory/Downloads/testcase/pytest/typings is not a valid directory.Assuming Python platform LinuxSearching for source filesFile or directory "/home/gregory/Downloads/testcase/pytest/--" does not exist.Found 1 source file/home/gregory/Downloads/testcase/pytest/ricchy.py /home/gregory/Downloads/testcase/pytest/ricchy.py:5:16 - error: "Live" is not exported from module "rich.box.live" (reportPrivateImportUsage)1 error, 0 warnings, 0 infosCompleted in 0.757sec
Ah, apologies. This isn't the same bug. For some reason, I wasn't seeing the error previously, but I'm able to repro the problem now.
The issue, as I think you've already discovered, is that the file rich/live.py is redefining the symbol Live within a code block that is conditioned with the check if __name__ == "__main__". That won't work with the py.typed spec.
Here are some suggestions for you to address this:
Don't include sample code within your library modules. Split it out into separate sample code files. This is what most libraries do.
If you really want to keep the sample code within your library module, add an additional if not __TYPE_CHECKING__ condition so it is "hidden" from type checkers.
Activity
erictraut commentedon Oct 7, 2021
Although it's not immediately obvious, this is the same as this bug: #2372. I fixed it earlier today, and the fix will be included in the next release of pyright. I confirmed that your sample above works without generating an error. Apologies for any inconvenience this bug caused you.
erictraut commentedon Oct 7, 2021
This is included in pyright 1.1.176, which I just published. It will also be included in the next release of pylance.
GBeauregard commentedon Oct 7, 2021
While 1.1.176 did fix the other issues (thanks!), it did not resolve the title issue for me (nor the sample). Apologies if this is some sort of delayed update or caching thing, but I did recreate venvs and such to be sure. See output below that includes running
--versionfirst then running on the test file from the issue.erictraut commentedon Oct 7, 2021
Ah, apologies. This isn't the same bug. For some reason, I wasn't seeing the error previously, but I'm able to repro the problem now.
The issue, as I think you've already discovered, is that the file
rich/live.pyis redefining the symbolLivewithin a code block that is conditioned with the checkif __name__ == "__main__". That won't work with the py.typed spec.Here are some suggestions for you to address this:
if not __TYPE_CHECKING__condition so it is "hidden" from type checkers.GBeauregard commentedon Oct 7, 2021
Thanks for the suggestions and guidance; I really really appreciate your time. I'll make appropriate PRs and discuss with maintainer.