Python Forum
How can I create this type hint.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I create this type hint.
#1
I am trying to create a type string using type hint information I get using inspect.signature(). I have this working for most types:
Output:
Type Type String ---- ----------- int "I" float "F" str "S" list[int] ">II" list[float] ">IF"
My problem is that I actually have two list types that differ by "direction": TO and FROM. A TO list is used to pass values from a sender to a receiver. A FROM list is used to return values from the receiver back to the sender. TO and FROM lists are part of a messaging protocol that was originally used to communicate between C programs.
example receiver: function(to_size, to_values, from_size, from_values) { for(int i = 0; i < MIN(to_size, from_size); i++) from_values[i] = to_values[i]; } example sender: int *to_values = {1, 2, 3, 4}; int *from_values[4]; typestr = remote.typestring("function") // returns ">II<II" remote.call("function", typestr, 4, to_values, 4, from_values)
I still need to communicate to and between C programs, plus there is much legacy code that uses this protocol. Changes to the protocol is off the table unless they are backward compatible. I need help making a type hint that will let me write something like this in Python
# example receiver function(to_values: list[int], from_values: return_list[int]): for i in range(min(len(to_values), len(from_values))): from_values[i] = to_values[i] # example sender to_values = [1, 2, 3, 4] from_values = numpy.zeros(4, int32) typestr = remote.typestring("function") remote.call("function", typestr, to_values, 4, from_values) # and the calling mechanism will expand to remote.call("function", typestr, len(to_values), to_values, len(from_values), from_values)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question how to type hint a function in a dataclass? Calab 3 1,834 Feb-27-2025, 04:40 AM
Last Post: Calab
  Import a module for use in type hint? Milosz 0 2,394 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Type hinting - return type based on parameter micseydel 2 3,762 Jan-14-2020, 01:20 AM
Last Post: micseydel
  __getattr__ and type hint itaybardugo 0 3,712 Jul-04-2019, 09:50 PM
Last Post: itaybardugo
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 6,485 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana
  Type hint produces Syntax error Digamma 4 13,137 Apr-27-2018, 06:50 AM
Last Post: Digamma

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.