Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use module level __dir__ to restrict public API views #84

Merged
merged 7 commits into from
Apr 17, 2021

Conversation

matthewfeickert
Copy link
Member

@matthewfeickert matthewfeickert commented Apr 17, 2021

Description

Implemented from Brett Cannon's blog An approach to lazy importing in Python 3.7 and implemented it for hist in scikit-hep/hist#179:

I’ve used Python 3.7 module-level __getattr__ for a while now to allow mistyping hist.axis import as hist.axes to show a warning but work anyway. However, I completely missed a fantastic usage for Python 3.7 module-level __dir__ : you can “fix” IPython’s tab completion to just show the items in __all__! Wow! One of the main things that bugged me about importing and defining unrelated things in modules, and one reason that boost-histogram has an _internal module.

c.f. scikit-hep/pyhf#1402

Used in PR #83

Checklist Before Requesting Reviewer

  • Tests are passing
  • "WIP" removed from the title of the pull request
  • Selected an Assignee for the PR to be responsible for the log summary

Before Merging

For the PR Assignees:

  • Summarize commit messages into a comprehensive review of the PR
* Use module level __dir__ in combination with __all__ to restrict the public API import views to useful imports
   - Only available in Python 3.7+
* c.f. https://snarky.ca/lazy-importing-in-python-3-7/
* Add tests for output of dir(pylhe)

@codecov
Copy link

codecov bot commented Apr 17, 2021

Codecov Report

Merging #84 (eb2f579) into master (a084774) will increase coverage by 0.63%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #84      +/-   ##
==========================================
+ Coverage   65.62%   66.25%   +0.63%     
==========================================
  Files           1        1              
  Lines         160      163       +3     
  Branches       30       30              
==========================================
+ Hits          105      108       +3     
  Misses         48       48              
  Partials        7        7              
Flag Coverage Δ
unittests 66.25% <100.00%> (+0.63%) ⬆️

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

Impacted Files Coverage Δ
src/pylhe/__init__.py 66.25% <100.00%> (+0.63%) ⬆️

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 a084774...eb2f579. Read the comment docs.

@matthewfeickert matthewfeickert merged commit 3669bed into master Apr 17, 2021
@matthewfeickert matthewfeickert deleted the feat/use-dir-for-api branch April 17, 2021 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant