Skip to content

Commit 5069b8c

Browse files
authored
Merge pull request #462 from Geson-anko/master
Add type hints to all public methods, functions and classes
2 parents 1aee0c3 + 68a57ca commit 5069b8c

File tree

6 files changed

+146
-59
lines changed

6 files changed

+146
-59
lines changed

.github/workflows/python-package.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ jobs:
5454
run: pip install --editable . --verbose
5555
- name: Run tests
5656
run: python -m pytest
57+
58+
type-check:
59+
runs-on: ubuntu-latest
60+
steps:
61+
- uses: actions/checkout@v4
62+
with:
63+
submodules: true
64+
- uses: actions/setup-python@v5
65+
with:
66+
python-version: "3.9"
67+
- name: Install requirements
68+
run: pip install numpy pytest pyright
69+
- name: "Workaround: Generate _soundfile.py explicitly"
70+
run: |
71+
pip install cffi>=1.0
72+
python soundfile_build.py
73+
- name: Install editable package
74+
run: pip install --editable . --verbose
75+
- name: Run type check
76+
run: python -m pyright soundfile.py

CONTRIBUTING.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ regressions), if you add a feature, you should add tests for it as well.
1414

1515
Set up local environment with the following commands::
1616

17-
pip install numpy pytest "cffi>=1.0"
17+
pip install numpy pytest "cffi>=1.0" typing-extensions
1818
python soundfile_build.py
1919

2020
To run the tests, use::
@@ -35,6 +35,30 @@ This uses pytest_;
3535

3636
.. _known problem: http://www.mega-nerd.com/libsndfile/api.html#open_fd
3737

38+
Type Checking
39+
^^^^^^^^^^^^^
40+
41+
Type hints have been added to the codebase to support static type checking.
42+
You can use pyright to check the types:
43+
44+
.. code-block:: bash
45+
46+
pip install pyright
47+
pyright soundfile.py
48+
49+
Or you can use the VS Code extension for inline type checking.
50+
51+
When contributing, please maintain type hints for all public functions, methods, and classes.
52+
Make sure to use appropriate types from the typing and typing-extensions modules.
53+
54+
The following conventions are used:
55+
56+
- Use Literal types for enumerated string values
57+
- Use TypeAlias for complex type definitions
58+
- Use overloads to provide precise return type information
59+
- Use Optional for parameters that can be None
60+
- Use Union for values that can be different types
61+
3862
Coverage
3963
^^^^^^^^
4064

pyrightconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"pythonVersion": "3.9",
3+
"exclude": [
4+
"**/node_modules",
5+
"**/__pycache__",
6+
"**/.venv",
7+
"tests/"
8+
]
9+
}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get_tag(self):
9696
zip_safe=zip_safe,
9797
license='BSD 3-Clause License',
9898
setup_requires=["cffi>=1.0"],
99-
install_requires=['cffi>=1.0', 'numpy'],
99+
install_requires=['cffi>=1.0', 'numpy', 'typing-extensions'],
100100
cffi_modules=["soundfile_build.py:ffibuilder"],
101101
extras_require={'numpy': []}, # This option is no longer relevant, but the empty entry must be left in to avoid breaking old build scripts.
102102
platforms='any',

0 commit comments

Comments
 (0)