File: rename_typing_generic_aliases.py

package info (click to toggle)
python-libcst 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,240 kB
  • sloc: python: 78,096; makefile: 15; sh: 2
file content (37 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
# pyre-strict
from functools import partial
from typing import cast, Generator

from libcst.codemod import Codemod, MagicArgsCodemodCommand
from libcst.codemod.commands.rename import RenameCommand


class RenameTypingGenericAliases(MagicArgsCodemodCommand):
    DESCRIPTION: str = (
        "Rename typing module aliases of builtin generics in Python 3.9+, for example: `typing.List` -> `list`"
    )

    MAPPING: dict[str, str] = {
        "typing.List": "builtins.list",
        "typing.Tuple": "builtins.tuple",
        "typing.Dict": "builtins.dict",
        "typing.FrozenSet": "builtins.frozenset",
        "typing.Set": "builtins.set",
        "typing.Type": "builtins.type",
    }

    def get_transforms(self) -> Generator[type[Codemod], None, None]:
        for from_type, to_type in self.MAPPING.items():
            yield cast(
                type[Codemod],
                partial(
                    RenameCommand,
                    old_name=from_type,
                    new_name=to_type,
                ),
            )