What would it take to implement type mapping for generics?

I recently came about a situation in my code where, as mentioned here, I have a clear mapping between input and output types that occurs in many different places throughout my code, and updating that mapping is just infeasible.

I’m a huge supporter of type-hinting for Python, and would be willing to look into implementing this functionality (see link below - as a new user to this forum I’m only able to include 2 links). However, I’m not sure what the process is like.

What steps would need to be taken in order for this to be implemented in a future version of Python?

1 Like

It is better to explain what you are talking about here in discourse. Not having clicked either of your links I have no idea what you are talking about.

Got it, I’ll copy/paste from the original source. I’d like to implement functionality like this:

TypeLookupTable = TypeMapping('TypeLookupTable', {
    FromType1: ToType1,
    FromType2: ToType2,
    FromType3: ToType3,
    ...
})

T = TypeVar('T', bound=any_type_present_as_a_key_in_that_lookup_table)

@dataclass
class SomeGeneric(Generic[T]):
     data: T

     def func(self) -> TypeLookupTable[T]:
         # This function should produce an instance of the corresponding type of `T` in
         # that lookup table.

It looks like there was some agreement in the thread I linked that this would be useful functionality, and I was looking for documentation on the process for actually implementing this and getting it integrated into python’s typing module, and the main page linked me here.

So here I am, asking what the process is for updating the typing functionality to allow for this sort of construction. Does there need to be a PEP for it? How does the proposal I linked to on GitHub actually turn into a real contribution / change to typing in Python?

Does there need to be a PEP for it?

It’s a big change so yes

1 Like