Clarifying the typing spec regarding polymorphic protocols

The rescoping rule you’re quoting here from the pyright documentation isn’t currently part of the typing spec. I implemented it this way in pyright to mirror mypy’s (undocumented) behavior. There are numerous libraries and stubs that rely on this behavior, so deviating from it was problematic. I’m not confident that pyright’s behavior is 100% consistent with mypy’s in this case, but it’s close. Whatever behavior we decide is correct here, I’d like to see it properly specified in the typing spec.

When we were working on the proposal for PEP 695, I explored ways to scope type variables to callables, but ultimately decided not to tackle that issue because the PEP was already ambitious and risked not being approved. This topic was also discussed in conversations related to PEP 677, which was ultimately rejected by the steering council.

2 Likes