mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Support `TypeAliasType` explicit call

Open sobolevn opened this issue 2 years ago • 7 comments

The implementation turned out to be quite simple for the common cases. Note: Right now there's no check that type_params= is correct, I am not sure that this is even required for a type-checker (in any case, this can and should be done in a follow-up PR)

Refs https://github.com/python/mypy/issues/16614

sobolevn avatar Dec 10 '23 10:12 sobolevn

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Dec 10 '23 10:12 github-actions[bot]

I will try to find how to fix typevar representation change without code modifications, I think that this has to do with my typing fixtures.

sobolevn avatar Dec 10 '23 11:12 sobolevn

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar Dec 10 '23 11:12 github-actions[bot]

One big advantage of X = TypeAliasType("X", ...) over X: TypeAlias = ... is that you can choose the order of the type parameters:

from typing import TypeVar
from typing_extensions import TypeAliasType

K = TypeVar("K")
V = TypeVar("V")

InvertedDict = TypeAliasType("InvertedDict", dict[K, V], type_params=(V, K))

def f(x: InvertedDict[int, str]) -> None:
    reveal_type(x)

pyright correctly reveals the type as dict[str, int] (i.e., inverted), but this branch reveals:

main.py:10: note: Revealed type is "builtins.dict[builtins.int, builtins.str]"

tmke8 avatar Dec 11 '23 17:12 tmke8

I don't think we should claim support for TypeAliasType without checking the type_params. Explicit type_params are the main differentiating factor that sets the new syntax apart.

JelleZijlstra avatar Dec 11 '23 17:12 JelleZijlstra

Hello @sobolevn, I'd like to try and finish up this PR if that's OK with you.

hamdanal avatar Feb 17 '24 08:02 hamdanal

@hamdanal please, go ahead :) I forgot about it, sorry.

sobolevn avatar Feb 17 '24 11:02 sobolevn

Merged #16926 instead, thanks for your work here!

JelleZijlstra avatar Mar 11 '24 14:03 JelleZijlstra