Skip to content

Commit

Permalink
Add py.typed file to make test_project_python PEP 561 compliant
Browse files Browse the repository at this point in the history
- this change addresses the following regression from the previous commit:
/path/to/test_project/python/test_project_python_tests/test_package_builder.py:4:1:4:1: error: Skipping analyzing "test_project_python.package_builder": module is installed, but missing library stubs or py.typed marker  [import]
    from test_project_python.package_builder import (
    ^
/path/to/test_project/python/test_project_python_tests/test_package_builder.py:4:1:4:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
- removing the `os.chdir()` commands are safe now since the `py.typed` file
  allows `mypy` to analyze the `import`ed `test_project_python` package
  without running into the error above
  - see the previous commit for more information about why
    manually moving into the `python` directory might be problematic
  • Loading branch information
shailshouryya committed Apr 17, 2023
1 parent e809e5d commit 027d3f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 0 additions & 2 deletions python/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def static_type_check_packages_and_modules(
# directory, so this module needs to move into the
# test_project/python directory in order to find the
# test_project_python and test_project_python_tests packages
# os.chdir('python') # change into python directory to allow mypy to find test_project_python package
mypy_flags = [
'--strict',
'--show-error-context',
Expand All @@ -51,7 +50,6 @@ def static_type_check_packages_and_modules(
current_config = mypy_flags + package_config
print(f'''mypy {' '.join(current_config)}''')
mypy.main.main(args=current_config, clean_exit=True)
# os.chdir('..') # change back to root directory to allow relative paths to work properly from pre-commit hook (which runs from the root directory of the git repo)
for changed_py_file in changed_py_files:
current_config = mypy_flags + [changed_py_file]
print(f'''mypy {' '.join(current_config)}''')
Expand Down
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
# If there are data files included in your packages that need to be installed, specify them here.
# If using Python 2.6 or earlier, then these have to be included in MANIFEST.in as well.
package_data = { # Optional
# 'sample': ['package_data.dat'],
'test_project_python': ['py.typed'],
},
# Although 'package_data' is the preferred approach, in some case you may need to place data files outside of your packages. See:
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files
Expand Down
24 changes: 24 additions & 0 deletions python/test_project_python/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
This py.typed file makes this package (test_project_python)
a PEP 561 compliant stub package.

Make sure to add a `package_data` argument consisting of
a `dict` with the key(s) corresponding to the package name(s)
and the value(s) corresponding to the file(s) in that package
that contain the type hint information to the `setup`
function (e.g. in `distutils.core.setup` or `setuptools.setup`).
If the type hint information is contained in inline type annotations,
a `py.typed` file should suffice.

```
package_data={"package_name": ["py.typed"]},
# OR if the package has stub files (ending in .pyi)
package_data={"package_name": ["py.typed", "module1.pyi", "module2.pyi"]},
```

Read the following for more information:
- https://mypy.readthedocs.io/en/stable/installed_packages.html
- https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker
- https://mypy.readthedocs.io/en/stable/stubs.html#stub-files
- https://peps.python.org/pep-0561/
'''

0 comments on commit 027d3f9

Please sign in to comment.