Skip to content

Fix all reportPrivateImportUsage in pyright for library users - #1551

Closed
GBeauregard wants to merge 1 commit into
Textualize:masterfrom
GBeauregard:pyright-fixes
Closed

GBeauregard wants to merge 1 commit into
Textualize:masterfrom
GBeauregard:pyright-fixes

Conversation

@GBeauregard

@GBeauregard GBeauregard commented Oct 7, 2021 •

Copy link
Copy Markdown
Contributor

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

This PR adds TYPE_CHECKING guards to the __main__ sections of all files that imported/redefined part of their own API in this section. This hides the redefinition from type checkers as needed to avoid accidentally signalling the API as private and causing pyright errors for everyone using rich as a library.

Note there are many more __main__ sections in the library; it was only necessary to add the guard to files importing their own API.

This PR resolves #1523

I didn't update CONTRIBUTORS since the change is minor, but if you have a policy of always updating it let me know and I'll add my name.

This commit adds TYPE_CHECKING guards to the __main__ sections of all
files that imported/redefined part of their own API in this section.
This hides the redefinition from type checkers as needed to avoid
accidentally signalling the API as private.
@GBeauregard

GBeauregard commented Oct 7, 2021 •

Copy link
Copy Markdown
Contributor Author

I brought up the possibility of excluding __main__ sections with upstream, but the feeling was that it's really a library issue. The proper TYPE_CHECKING guard is simple enough anyway. See also microsoft/pyright#2392 (comment)

GBeauregard added a commit to GBeauregard/pyffstream that referenced this pull request Oct 7, 2021
@GBeauregard GBeauregard changed the title Fix all reportPrivateImportUsage in pyright Fix all reportPrivateImportUsage in pyright for library users Oct 13, 2021
@codecov

codecov Bot commented Oct 15, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #1551 (bbc44ca) into master (3f192d3) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1551      +/-   ##
==========================================
- Coverage   99.70%   99.70%   -0.01%     
==========================================
  Files          71       71              
  Lines        6854     7091     +237     
==========================================
+ Hits         6834     7070     +236     
- Misses         20       21       +1     
Flag Coverage Δ
unittests 99.70% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/box.py 100.00% <ø> (ø)
rich/table.py 100.00% <ø> (ø)
rich/live.py 100.00% <100.00%> (ø)
rich/pager.py 91.66% <0.00%> (-8.34%) ⬇️
rich/text.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 18bedc6...bbc44ca. Read the comment docs.

@willmcgugan

Copy link
Copy Markdown
Member

I'm not overly keen on that workaround. The main sections are excluded from coverage, but I still like them to be typechecked.

Could the individual lines be excluded in Pyright, rather than the entire block?

@GBeauregard

GBeauregard commented Oct 15, 2021 •

Copy link
Copy Markdown
Contributor Author

I'm not overly keen on that workaround. The main sections are excluded from coverage, but I still like them to be typechecked.

Could the individual lines be excluded in Pyright, rather than the entire block?

My understanding is no since the error is happening downstream. I will double check for posterity and open a PR with such a change if it works.

I believe you have 3 options that resolve the issue:

  1. type checking guards on the entire section (this PR)
  2. redundant imports on the lines that import their own API; this is the least intrusive and the minimally "correct" option. I've opened a PR with this option at Minimally fix reportPrivateImportUsage in pyright for library users  #1596
  3. Move the sample code to a separate part of the codebase. This has the advantage of sidestepping other static analysis issues of the codebase too. For example, my tooling analyzing the inter-dependency of rich's imports isn't really useful due to the __main__ sections.

@willmcgugan

Copy link
Copy Markdown
Member

Thanks. I think I'll go with 2.

  1. Isn't really an option. The main blocks are fairly integral and popular feature.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pyright: "Table" is not exported from module "rich.table"

2 participants