diff --git a/pytype/pyi/parser_test.py b/pytype/pyi/parser_test.py index 5b8f35e6e..66546c309 100644 --- a/pytype/pyi/parser_test.py +++ b/pytype/pyi/parser_test.py @@ -2852,11 +2852,11 @@ def g(x: Alias[str]) -> str: ...""", """ from typing import TypeVar - Alias = list[list[T]] - S = TypeVar('S') T = TypeVar('T') + Alias = list[list[T]] + def f(x: list[list[S]]) -> S: ... def g(x: list[list[str]]) -> str: ...""", ) @@ -2871,10 +2871,10 @@ def f(x: DictAlias[str]) -> None: ...""", """ from typing import TypeVar - DictAlias = dict[int, V] - V = TypeVar('V') + DictAlias = dict[int, V] + def f(x: dict[int, str]) -> None: ...""", ) @@ -2889,11 +2889,11 @@ def f(x: Alias[K, V]) -> Dict[K, V]: ...""", """ from typing import TypeVar - Alias = list[dict[K, V]] - K = TypeVar('K') V = TypeVar('V') + Alias = list[dict[K, V]] + def f(x: list[dict[K, V]]) -> dict[K, V]: ...""", ) @@ -2908,11 +2908,11 @@ def f(x: Alias[S, T]) -> Union[S, T]: ...""", """ from typing import TypeVar, Union - Alias = Union[list[T], list[S]] - S = TypeVar('S') T = TypeVar('T') + Alias = Union[list[T], list[S]] + def f(x: Union[list[S], list[T]]) -> Union[S, T]: ...""", ) @@ -2926,10 +2926,10 @@ def f(x: Alias[str]) -> None: ...""", """ from typing import TypeVar - Alias = dict[T, T] - T = TypeVar('T') + Alias = dict[T, T] + def f(x: dict[str, str]) -> None: ...""", ) @@ -3710,11 +3710,11 @@ class X(Generic[T, P]): """ from typing import Any, Callable, Generic, ParamSpec, TypeVar - x: X[int, Any] - P = ParamSpec('P') T = TypeVar('T') + x: X[int, Any] + class X(Generic[T, P]): f: Callable[P, int] x: T diff --git a/pytype/pytd/printer.py b/pytype/pytd/printer.py index 94900c555..1fbec3858 100644 --- a/pytype/pytd/printer.py +++ b/pytype/pytd/printer.py @@ -319,9 +319,9 @@ def VisitTypeDeclUnit(self, node): sections = [ imports, + self._FormatTypeParams(self.old_node.type_params), aliases, constants, - self._FormatTypeParams(self.old_node.type_params), node.classes, node.functions, ] diff --git a/pytype/pytd/visitors_test.py b/pytype/pytd/visitors_test.py index 379018b94..a1c2f9c16 100644 --- a/pytype/pytd/visitors_test.py +++ b/pytype/pytd/visitors_test.py @@ -1224,19 +1224,19 @@ class X(Generic[T]): ... expected_one = textwrap.dedent(""" from typing import Any, TypeVar - foo.x: Any - T = TypeVar('T') + foo.x: Any + class foo.X(Generic[T]): ... """).strip() expected_two = textwrap.dedent(""" from typing import Any, TypeVar - x: Any - T = TypeVar('T') + x: Any + class X(Generic[T]): ... """).strip() tree = self.Parse(src)