Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate type aliases for request_class and response_class #37

Merged
merged 1 commit into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions genmypy/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ._compat import lru_cache
from ._typing import TYPE_CHECKING
from .stub_element import (
AliasElement,
ClassElement,
ClassMethodElement,
CommentElement,
Expand Down Expand Up @@ -191,13 +192,13 @@ def convert_message_class(first_party_package, spec, imports):
return msgclass


def convert_service_class(spec):
# type: (SrvSpec) -> ClassElement
def convert_service_class(spec, request_class, response_class):
# type: (SrvSpec, ClassElement, ClassElement) -> ClassElement
srvclass = ClassElement(spec.short_name, "object")
srvclass.add(FieldElement("_type", "str"))
srvclass.add(FieldElement("_md5sum", "str"))
srvclass.add(FieldElement("_request_class", "str"))
srvclass.add(FieldElement("_response_class", "str"))
srvclass.add(AliasElement("_request_class", request_class.name))
srvclass.add(AliasElement("_response_class", response_class.name))

return srvclass

Expand Down
4 changes: 3 additions & 1 deletion genmypy/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def generate_service_stub(package, spec):
module.add_element(EmptyLinesElement())
module.add_element(message_classes[1]) # response class
module.add_element(EmptyLinesElement())
module.add_element(convert_service_class(spec)) # service class
module.add_element(
convert_service_class(spec, message_classes[0], message_classes[1])
) # service class

for line in module.generate():
yield line
Expand Down
11 changes: 11 additions & 0 deletions genmypy/stub_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ def generate(self, indent):
yield _format("{}: {}".format(self.name, self.type), indent)


class AliasElement(StatementElementBase):
def __init__(self, name, alias):
# type: (str, str) -> None
self.name = name
self.alias = alias

def generate(self, indent):
# type: (int) -> Iterator[str]
yield _format("{} = {}".format(self.name, self.alias), indent)


class FunctionElement(StatementElementBase):
def __init__(self, name, return_type, params=None):
# type: (str, str, Optional[Sequence[ParameterElement]]) -> None
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/expected/nav_msgs/srv/_SetMap.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ class SetMapResponse(genpy.Message):
class SetMap(object):
_type: str
_md5sum: str
_request_class: str
_response_class: str
_request_class = SetMapRequest
_response_class = SetMapResponse
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ class SetCameraInfoResponse(genpy.Message):
class SetCameraInfo(object):
_type: str
_md5sum: str
_request_class: str
_response_class: str
_request_class = SetCameraInfoRequest
_response_class = SetCameraInfoResponse