Skip to content

Commit

Permalink
Ensure PEP0561 compliance for proto stubs.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 339362317
  • Loading branch information
Cameron Tew committed Oct 28, 2020
1 parent a56ab2d commit 138ba2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions py/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
include requirements.txt
include README.md
include google/fhir/py.typed
include proto/google/fhir/proto/py.typed
recursive-include proto/google/fhir/proto *.pyi
global-exclude *_test.py
14 changes: 12 additions & 2 deletions py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@


def _generate_python_protos(proto_files: List[str], *, python_out: str = '.'):
"""Generates FHIR R4 Python protobuf classes.
"""Generates FHIR Python protobuf classes.
Args:
proto_files: A list of .proto file paths.
python_out: Indicates where the generated definitions should be placed.
Defaults to '.', which indicates to place alongside the .proto definition.
This directory will be created if it does not exist.
"""
if not proto_files:
return

subpackages = []
for proto_file in proto_files:
proto_dir = os.path.dirname(os.path.abspath(proto_file))

Expand All @@ -61,7 +65,9 @@ def _generate_python_protos(proto_files: List[str], *, python_out: str = '.'):
# Create Python package
init_py = os.path.join(python_out, relpath, '__init__.py')
if not os.path.exists(init_py):
pathlib.Path(os.path.dirname(init_py)).mkdir(parents=True)
subpackage = os.path.dirname(init_py)
subpackages.append(subpackage)
pathlib.Path(subpackage).mkdir(parents=True)
pathlib.Path(init_py).touch()

# Generate _py_pb2.py file
Expand All @@ -81,6 +87,10 @@ def _generate_python_protos(proto_files: List[str], *, python_out: str = '.'):
if subprocess.call(protoc_cmd) != 0:
sys.exit(-1)

# Add py.typed for PEP0561 compliance
root_package = os.path.commonpath(subpackages)
pathlib.Path(os.path.join(root_package, 'py.typed')).touch()


def _parse_requirements(path: str) -> List[str]:
"""Parses a requirements.txt file into a list of strings."""
Expand Down

0 comments on commit 138ba2f

Please sign in to comment.