Skip to main content

LibCST codemod to transform typing imports

Project description

import-typing-as-t

LibCST Codemod to transform relative typing import (from typing import...) or generic typing import (import typing) and their refs to import typing as t + t. prefixed refs respectively.

Install

  • pip install typing-as-t
  • Help: typing-as-t --help

Examples

Before:

    from typing import Callable, Optional, Generator, cast, Any

    a : Callable[..., Any] = lambda : "test"
    def func(c: Optional[int] = None) -> Generator:
        return cast(Generator, "blabla")

After:

    import typing as t

    a : t.Callable[..., t.Any] = lambda : "test"
    def func(c: t.Optional[int] = None) -> t.Generator:
        return t.cast(t.Generator, "blabla")

Before:

    from typing import cast, List, Dict, Any, Sequence, TypeAlias, Sequence, Generator

    float_list : TypeAlias = list[float]
    def func(z: float_list, c: Sequence[tuple[tuple[str, int], List[Dict[str, Any]]]] = None) -> Generator:
        return cast(Generator, "blabla")

After:

    import typing as t

    float_list : t.TypeAlias = list[float]
    def func(z: float_list, c: t.Sequence[tuple[tuple[str, int], t.List[t.Dict[str, t.Any]]]] = None) -> t.Generator:
        return t.cast(t.Generator, "blabla")

Before:

    import typing

    a : typing.Callable[..., typing.Any] = lambda : "test"
    def func(c: typing.Optional[int] = None) -> typing.Generator:
        return typing.cast(typing.Generator, "blabla")

After:

    import typing as t

    a : t.Callable[..., t.Any] = lambda : "test"
    def func(c: t.Optional[int] = None) -> t.Generator:
        return t.cast(t.Generator, "blabla")

Before:

from typing import Any, TYPE_CHECKING as TC
from typing import TypeAlias as TA

if TC:
    foo: dict[str, Any]

Vector: TA = list[float]

After:

import typing as t

if t.TYPE_CHECKING:
    a: dict[str, t.Any]

Vector: t.TypeAlias = list[float]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typing_as_t-1.0.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

typing_as_t-1.0.0-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page