Skip to content

Add a import type feature (like typescript) #928

Open
@KotlinIsland

Description

@KotlinIsland

Importing types can lead to circular imports, longer start up times and unwanted side effects.

I want something like:

from foo.bar import type Baz

def foo(baz: Baz):
    print(baz.bat)

Such that foo, foo.bar and foo.bar.Baz are not actually loaded at all.
It could 'import' the symbol as a forward-ref or some type machinery thing. (maybe just the string "Baz")

Alternatives:

from __future__ import annotations

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from foo.bar import Baz

def foo(baz: Baz):
    print(baz.bat)

This is mega boilerplate, gross, and leads to messy wacky 'type-time' side effects (python/mypy#11503).

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions