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

Add __all__ with public names #33

Merged
merged 2 commits into from
Apr 18, 2021
Merged

Add __all__ with public names #33

merged 2 commits into from
Apr 18, 2021

Conversation

br3ndonland
Copy link
Owner

Description

In Python, the __all__ object is a sequence (typically a list or tuple) that defines the public names of a module or package. When other applications install the package, the objects corresponding to the public names are exposed for direct import and use.

This pull request will add __all__, defining the public API of the package. To start, the public API will consist of a few useful utilities that are commonly imported in other Python applications, such as the logging configuration logic and HTTP Basic auth implementations.

There are some technical limitations to keep in mind when setting up __all__. For example, this project defines optional dependencies, including FastAPI and Starlette. If objects that use these optional dependencies are added to __all__, but the dependencies are not installed, ImportError exceptions will be raised. A simple solution is to wrap these imports in a try/except block.

As another example, if objects from inboard/start.py are added to __all__, and the module is then invoked with python -m inboard.start (as the project is currently configured to do), a RuntimeWarning is raised. In this situation, Python potentially has two copies of the objects: one copy from inboard/__init__.py, and one copy from inboard/start.py. To avoid this warning, the if __name__ == "__main__" block could be moved to inboard/__main__.py, and Python could be invoked with python -m inboard instead of python -m inboard.start. This could be considered in a future refactor of the project, but it is not particularly important at the moment. The inboard/start.py module can be imported directly with import inboard.start.

Changes

  • Add __all__ to __init__.py (827c1c7)
  • Update LOGGING_CONFIG import in README example (b9b0f2f)

Related

#3
#11
#31
#32

https://docs.python.org/3/reference/simple_stmts.html#the-import-statement

In Python, `__all__` is a sequence (typically a list or tuple) that
defines the public names of a module or package. When other applications
install the package, the objects corresponding to the public names are
exposed for direct import and use.

This commit will add `__all__`, defining the public API of the package.
To start, the public API will consist of a few useful utilities that are
commonly imported in other Python applications, such as the logging
configuration logic and HTTP Basic auth implementations.
Now that inboard has `__all__`, which includes the `LOGGING_CONFIG`, the
`LOGGING_CONFIG` can be imported from the top-level package.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Apr 18, 2021

Sourcery Code Quality Report

This PR has an average code quality of 92.05%

Quality metrics Before After Change
Complexity 1.00 ⭐
Method Length 20.00 ⭐
Working memory 5.00 ⭐
Quality % 92.05% %
Other metrics Before After Change
Lines 21
Changed files Quality Before Quality After Quality Change
inboard/init.py % 92.05% ⭐ %

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Let us know what you think of it by mentioning @sourcery-ai in a comment.

@codecov
Copy link

codecov bot commented Apr 18, 2021

Codecov Report

Merging #33 (b9b0f2f) into develop (aec2cf5) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop       #33   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9        10    +1     
  Lines          264       269    +5     
=========================================
+ Hits           264       269    +5     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

Impacted Files Coverage Δ
inboard/__init__.py 100.00% <100.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 aec2cf5...b9b0f2f. Read the comment docs.

@br3ndonland br3ndonland merged commit 9f361a9 into develop Apr 18, 2021
@br3ndonland br3ndonland deleted the __all__ branch April 18, 2021 21:29
br3ndonland added a commit that referenced this pull request Apr 19, 2021
#20
#32
#33

This commit will refine the imports in inboard/`__init__.py` so that
FastAPI and Starlette are imported separately. This way, if Starlette is
installed but FastAPI is not, the HTTP Basic auth implementation for
Starlette can still be imported and added to `__all__`.
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.

1 participant