Skip to content

reportPrivateImportUsage confused by library source #2392

Closed

Description

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".

To Reproduce

import rich.box
import rich.live


with rich.live.Live("hi!"):
    ...

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.

Activity

  1. erictraut commented on Oct 7, 2021

    @erictraut
    Collaborator

    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.

  2. added
    addressed in next version
    bug
    on Oct 7, 2021
  3. erictraut commented on Oct 7, 2021

    @erictraut
    Collaborator

    This is included in pyright 1.1.176, which I just published. It will also be included in the next release of pylance.

  4. GBeauregard commented on Oct 7, 2021

    @GBeauregard
    Author

    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/pytest
    rich-pyright ❯ pyright --version
    npx: installed 1 in 2.042s
    pyright 1.1.176
    
    ~/Downloads/testcase/pytest
    rich-pyright ❯ pyright ~/Downloads/testcase/pytest/ricchy.py
    npx: installed 1 in 2.021s
    No configuration file found.
    No pyproject.toml file found.
    stubPath /home/gregory/Downloads/testcase/pytest/typings is not a valid directory.
    Assuming Python platform Linux
    Searching for source files
    File 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 infos
    Completed in 0.757sec
  5. erictraut commented on Oct 7, 2021

    @erictraut
    Collaborator

    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:

    1. Don't include sample code within your library modules. Split it out into separate sample code files. This is what most libraries do.
    2. 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.
  6. GBeauregard commented on Oct 7, 2021

    @GBeauregard
    Author

    Thanks for the suggestions and guidance; I really really appreciate your time. I'll make appropriate PRs and discuss with maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions