Skip to content

Feature request: accessing fields of TypeVars #8833

Closed
@mikolajz

Description

@mikolajz

In ROS (www.ros.org) a service (=an RPC method) is defined by a class that has request_type and response_type fields with the types of the argument and return value an calling the service. One passes the service class to ServiceProxy constructor to create a service proxy object:

# Trigger.request_type == TriggerRequest, Trigger.response_type == TriggerResponse
update_service = rospy.ServiceProxy("/some/service/name", Trigger)  # Create proxy.
response: TriggerResponse = update_service(TriggerRequest(...))  # Perform the RPC.

To annotate it, I would like to be able to do:

class ServiceProxy(Generic[T]):
    def __init__(name: str, service_type: Type[T]): ...
    def __call__(request: T.request_type) -> T.response_type: ...

However, it seems I can't access subfields of a TypeVar ("name T.RequestType is not defined").

Of course, any other solution that will solve this problem (or is better than having a proxy function that takes the request and response types as parameters as well, which we will likely do in our project) is good as well.

PS: I've noticed that I can't use the type defined in the service class directly since as explained in #7866, it will be considered a variable, but I control the type stub generation so I can add there a 1-element Union which seems to work, so it's not a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions