Skip to content

Commit 27e048e

Browse files
authored
Automatically add a py.typed file alongside stubs (#98)
* Automatically add py.typed file to generated stubs Remove docstub-stubs in CI before regenerating them. This tests that the `py.typed` is actually regenerated by docstub. * Add distribution section and mention py.typed in docs * Avoid early exit in assert-unchanged.sh and also show git status as a summary on failure. * Add missing py.typed to example_pkg-stubs * Also remove examples/example_pkg-stubs before regeneration * Disable pip's progress bar during installation
1 parent 6e3604e commit 27e048e

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

.github/scripts/assert-unchanged.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ CHECK_DIR=$1
99

1010
# Find untracked files
1111
UNTRACKED=$(git ls-files --others --exclude-standard "$CHECK_DIR")
12-
# and display their content by comparing with '/dev/null'
13-
echo "$UNTRACKED" | xargs -I _ git --no-pager diff --no-index /dev/null _
12+
13+
# Display diff of each untracked file by comparing with '/dev/null'
14+
# Hide exit code of `git diff` to avoid early exit due to `set -e`
15+
echo "$UNTRACKED" | xargs -I _ git --no-pager diff /dev/null _ || true
1416

1517
# Display changes in tracked files and capture non-zero exit code if so
16-
set +e
17-
git diff --exit-code HEAD "$CHECK_DIR"
18+
git diff --exit-code HEAD "$CHECK_DIR" || true
1819
GIT_DIFF_HEAD_EXIT_CODE=$?
19-
set -e
2020

2121
# Display changes in tracked files and capture exit status
2222
if [ $GIT_DIFF_HEAD_EXIT_CODE -ne 0 ] || [ -n "$UNTRACKED" ]; then
2323
echo "::error::Uncommited changes in directory '$CHECK_DIR'"
24+
git status --porcelain
2425
exit 1
2526
else
2627
echo "::notice::No Uncommited changes, directory '$CHECK_DIR' is clean"

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ env:
1717
# Many color libraries just need this to be set to any value, but at least
1818
# one distinguishes color depth, where "3" -> "256-bit color".
1919
FORCE_COLOR: 3
20+
PIP_PROGRESS_BAR: "off"
2021

2122
defaults:
2223
run:
@@ -81,6 +82,7 @@ jobs:
8182
# Check that stubs for example_pkg are up-to-date by regenerating them
8283
# with docstub and looking for differences.
8384
run: |
85+
rm -rf examples/example_pkg-stubs
8486
python -m docstub run -v \
8587
--config=examples/docstub.toml \
8688
--out-dir=examples/example_pkg-stubs \
@@ -91,6 +93,7 @@ jobs:
9193
# Check that stubs for docstub are up-to-date by regenerating them
9294
# with docstub and looking for differences.
9395
run: |
96+
rm -rf src/docstub-stubs
9497
python -m docstub run -v src/docstub -o src/docstub-stubs
9598
.github/scripts/assert-unchanged.sh src/docstub-stubs/
9699

docs/introduction.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,13 @@ class Foo:
204204

205205
If all of the above does not solve your issue, you can fall back to writing a correct stub file by hand.
206206
Docstub will preserve this file and integrated it with other automatically generated stubs.
207+
208+
209+
## Distributing stub files
210+
211+
The simplest option is to include generated stubs in the [distribution package](https://packaging.python.org/en/latest/glossary/#term-Distribution-Package) alongside your source files.
212+
For more complex setups please consult the official guide on [Packaging Type Information](https://typing.python.org/en/latest/spec/distributing.html#packaging-type-information).
213+
214+
As required, Docstub will automatically place an empty `py.typed` file in the root directory of generated stubs to support type checking.
215+
If you need to [mark your stubs as partial](https://typing.python.org/en/latest/spec/distributing.html#partial-stub-packages), create the `py.typed` file beforehand.
216+
Docstub will not overwrite it.

examples/example_pkg-stubs/py.typed

Whitespace-only changes.

src/docstub/_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ def run(
399399
logger.info("Wrote %s", stub_path)
400400
fo.write(stub_content)
401401

402+
py_typed_out = out_dir / "py.typed"
403+
if not py_typed_out.exists():
404+
py_typed_out.touch()
405+
logger.info("Created %s", py_typed_out)
406+
402407
# Reporting --------------------------------------------------------------
403408

404409
if group_errors:

0 commit comments

Comments
 (0)