-
Notifications
You must be signed in to change notification settings - Fork 258
Add a import type
feature (like typescript)
#928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
+1 to this feature, it would help the code to be much readable to the alternative that we have right now |
I was personally interested in this but if it gave you lazy imports so it'd still be introspectable |
An easy solution is to have from foo import type Baz be syntactic sugar for type Baz[_] = __import__('foo').Baz In other words, create a (generic) type alias whose |
Would be nice to avoid needing to specify Python named imports ( Current TypeScript import examples (restricted to syntax with appropriate Python analogue) // region named imports
import { foo, bar } from "..." // value-only named imports
import { foo, bar } from "..." // as above, with 'rename'
import type { Foo, Bar } from "..." // type-only named imports
import type { Foo: FooByAnotherName, Bar } from "..." // as above, with 'rename'
import { type Foo, type Bar } from "..." // type-only named imports, specified per-name
import { type Foo: FooByAnotherName, type Bar } from "..." // as above, with 'rename'
import { foo, type Foo } from "..." // mixed named imports, specified per-name
import { foo as fooByAnotherName, type Foo } from "..." // as above, with 'rename'
// endregion
// region namespace imports
import * as module from "..." // value namespace import with 'rename'
import type * as Module from "..." // type namespace import with 'rename'
// endregion Here's the Python syntax I propose, covering Python's various 'flavours' of # region named imports
from ... import foo, bar # value-only named imports (unparenthesised)
from ... import foo as foo_by_another_name, bar # as above, with 'rename'
from ... import (foo, bar) # value-only named imports (parenthesised)
from ... import (foo as foo_by_another_name, bar) # as above, with 'rename'
from ... import type (Foo, Bar) # type-only named imports (parenthesis required)
from ... import type (Foo as FooByAnotherName, Bar) # as above, with 'rename'
from ... import type Foo, type Bar # type-only named imports, specified per-name (unparenthesised)
from ... import type Foo as FooByAnotherName, type Bar # as above, with 'rename'
from ... import (type Foo, type Bar) # type-only named imports, specified per-name (parenthesised)
from ... import (type Foo as FooByAnotherName, type Bar) # as above, with 'rename'
from ... import foo, type Foo # mixed named imports, specified per-name (unparenthesised)
from ... import foo as foo_by_another_name, type Foo # as above, with 'rename'
from ... import (foo, type Foo) # mixed named imports, specified per-name (parenthesised)
from ... import (foo as foo_by_another_name, type Foo # as above, with 'rename'
# endregion
# region module imports
import ... # value module import
import ... as module # as above, with 'rename'
import ..., ... # value multiple module import
import ... as module, ... # as above, with 'rename'
import type ... # type module import
import type ... as Module # as above, with 'rename'
import type ..., type ... # type multiple module import
import type ... as Module, type ... # as above, with 'rename'
# endregion
# region wildcard imports
from ... import * # value wildcard import
from ... import type * # type wildcard import
# endregion Here's a modified An alternative to the 'parenthesis required' rule would be to use |
Importing types can lead to circular imports, longer start up times and unwanted side effects.
I want something like:
Such that
foo
,foo.bar
andfoo.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:
This is mega boilerplate, gross, and leads to messy wacky 'type-time' side effects (python/mypy#11503).
The text was updated successfully, but these errors were encountered: