What would it take to implement type mapping for generics? (original) (raw)

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?

oscarbenjamin (Oscar Benjamin) March 6, 2025, 7:57pm 2

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.

Jadiker (London L.) March 7, 2025, 3:11am 3

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?

yangdanny97 (Danny Y) March 7, 2025, 3:59am 4

Does there need to be a PEP for it?

It’s a big change so yes

mezuzza (Prasanth Somasundar) March 24, 2025, 7:41pm 5

What you’re looking for is support for type families. There are not many languages that do this in convenient syntax outside of haskell/scala. Swift, Rust, and Kotlin have some support as well as I understand it.

Not sure what python’s stance on more advanced type features is, but it would require a PEP to support.

jorenham (Joren Hammudoglu) March 25, 2025, 5:02am 6

I posted a similar idea a while back: Make `@overload` less verbose - #10 by jorenham, and in false positive with `divmod` and overloaded `__divmod__` · Issue #9835 · microsoft/pyright · GitHub I explained it in a bit more detail.

Maybe we’re talking about the same thing here?

The “type mappings” I had in mind can do at least everything that overloads can do, i.e. they generalize overloads. Overloads in python are notoriously difficult, and very underspecified.

So if we’re indeed talking about the same thing here, then the PEP for these type-mappings is going to be a beefy one, and considering the current state of overloads, likely to be contentious.

I don’t mean to discourage you, but if you’re seriously considering this, then you should know what you’re getting yourself into.

Jadiker (London L.) April 27, 2025, 1:37am 8

This is wonderful feedback, thank you! I’ll look into these.