Strip pragmas from your docs.
Project description
Improve developer experience:
Write better docs.
Do not repeat yourself.
Assure code low-maintenance.
TL;DR
sphinx-no-pragma is a Sphinx extension for stripping pragma comments from source code used in documentation.
If that’s all you need to know to move forward, jump right to the installation. Otherwise, read further.
Some say, “documentation is the king”. Others argue - “no, demos are”. While some say, “testing is everything!” and yet there will be someone else who will jump in with “write clean code! black, isort, mypy and ruff everywhere!”
And yet there’s you, who want to be good and write a better package, because there’s a generic problem that needs to be solved, and you know how, you want to share it with the world. You also want to assure or at least make an effort in making your project developer friendly, attractive for making contributions, which eventually leads to continuous improvement and make it live long(er).
So, combining the best practices, you:
Introduce examples in your repository to make it easier to start with.
Write awesome docs with usage examples (by eventually repeating yourself, copying things from your actual code examples).
Write tests for your code. Then you realize it’s good to test the examples too. Eventually, you have now almost the same code in 3 places: tests, examples and docs.
Introduce linters and MyPy.
Then you invest your time in making sure all your code looks correct and fix the never-ending MyPy issues.
Then you need to make a small change, which unfortunately, among other, requires altering the examples code. You need to change the examples, the docs, the tests and the examples tests. However, you also need to push the change quickly. As many times before, you skip documentation update, leaving it for “another time”.
By that time you discover that code maintenance is a hell. You fix everything, tests pass you’re happy to push, by then MyPy starts to nag about issues you have no idea how to solve and by that moment you don’t care about them. You’re sick of it and start using pragma comments to silence the errors, leaving the fix for another day. Your maintenance work involves a lot of copy-pasting from one place to another (examples, tests, documentation).
Does this sound familiar?
What if I tell you that actually a couple of steps can be taken out. Namely, that you can use your example code directly in your documentation, using .. literalinclude:: directive of Sphinx. That part has already been well covered in jsphinx project (JavaScript primarily). However, what jsphinx didn’t solve is presence of pragma comments in your documentation. This project does take care of that part. You don’t need to choose or balance between readability, explainability and low-maintenance.
Written by lazy developer for lazy developers to improve developer experience in writing low-maintenance code.
Features
Accurately stips out pragma comments from your source code that you include in your documentation.
Prerequisites
Python 3.8+
Installation
pip install sphinx-no-pragma
Documentation
Documentation is available on Read the Docs.
For guidelines on contributing check the Contributor guidelines.
Usage example
In order to move forward, you first need to get educate yourself a little on Sphinx’s directives. Namely the .. literalinclude:: and :download:. For that, first read the jsphinx documentation.
But there might be a little problem with that. Of course you might be lucky and have zero pragma comments in your code (no # noqa, no # type: ignore, etc). But more often, you get at least a couple of these. Your perfectionist nature doesn’t easily allow you to let them be part of your concise, beautiful documentation. Cursing me for earlier advices, you start to replace your DRY documentation part with copy-pasted examples.
This is where this package jumps in. It simply is a Sphinx extension that strips all pragma comments from your code that goes into documentation.
Sphinx configuration
Filename: docs/conf.py
extensions = [
# ... other extensions
"sphinx_no_pragma",
# ... other extensions
]
Code example
Filename: examples/example_1.py
from typing import Any, Optional
class ThirdPartyLibrary:
@staticmethod
def get_dynamic_object() -> Any:
# Returns an object whose type is not known at compile time
return "a string" # In reality, this could be any type
# Usage of the third-party library
obj = ThirdPartyLibrary.get_dynamic_object()
# Attempt to use the object as a string, even though its type is 'Any'
length = len(obj) # type: ignore
# Deliberately long line to violate PEP 8 line length rule, suppressed with noqa
print(f"The length of the object, a dynamically typed one, is just {length}") # noqa
Given that this is your code structure:
├── examples
│ └── example_1.py
├── docs
│ ├── conf.py
│ ├── index.rst
│ ├── Makefile
│ ├── _static
│ │ └── example_1.py
│ └── usage.rst
├── LICENSE
├── Makefile
├── pyproject.toml
├── README.rst
└── sphinx_no_pragma.py
Either use html_extra_path = ["examples"] or make a symlink to examples/example_1.py from docs/_static.
Then include it in your docs as follows:
Filename: example.rst
.. container:: jsphinx-download
.. literalinclude:: _static/example_1.py
:language: python
:lines: 1-
*See the full example*
:download:`here <_static/example_1.py>`
Now, rendered, your code will not contain # type: ignore or # noqa pragma comments.
See the demo. Click on the See the full example here link to see the original code.
Tests
Run the tests with unittest:
python -m unittest sphinx_no_pragma.py
Or pytest:
pytest
License
MIT
Support
For security issues contact me at the e-mail given in the Author section.
For overall issues, go to GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sphinx_no_pragma-0.1.1.tar.gz
.
File metadata
- Download URL: sphinx_no_pragma-0.1.1.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7705899a36ccc8f94f2e7c9b78b4490fb246d8c29b603cfc4ede123a28f337a8 |
|
MD5 | ce229410082e97d68124fa451a9ea1e7 |
|
BLAKE2b-256 | 1b28b8d15278f7dbb5f01aa7f136a109cf92b377def2472e0ae8cb54713ef2da |
File details
Details for the file sphinx_no_pragma-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: sphinx_no_pragma-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1762d3f934c317b61fde8629c15f5b02d76290c6865a26012e7dd158818c1ba |
|
MD5 | a410408c33def63560f2c28d35eeae0d |
|
BLAKE2b-256 | ee9b9041dcc34cdd7ce3e7ef08c35012fb25ab9e6b0da4166810a4ba8153663f |