Skip to content
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

[refactor] Cleanup python imports #2231

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/taichi/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from taichi.core.settings import *
from taichi.core.record import *
from taichi.core.logging import *
from taichi.core.primitive_types import *
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about this... It's bad to do wildcard import. On the other hand, there are arguments saying that doing this in __init__.py is an exception, in order to lift the symbols into the package level. If not written this way, we would have to write from taichi.x import float32, f32, float64, f64, ..., u64 twice, which is quite repetitive (and equally hard to maintain). What's your opinion :) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current solution should be good, given taichi.core.primitive_types already clearly defined the __all__ variable, listing the names to export :-)


ti_core.build = build
ti_core.load_module = load_module
Expand Down
60 changes: 60 additions & 0 deletions python/taichi/core/primitive_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from taichi.core.util import ti_core
yuanming-hu marked this conversation as resolved.
Show resolved Hide resolved

# Real types

float32 = ti_core.DataType_f32
f32 = float32
float64 = ti_core.DataType_f64
f64 = float64

real_types = [f32, f64, float]
real_type_ids = [id(t) for t in real_types]

# Integer types

int8 = ti_core.DataType_i8
i8 = int8
int16 = ti_core.DataType_i16
i16 = int16
int32 = ti_core.DataType_i32
i32 = int32
int64 = ti_core.DataType_i64
i64 = int64

uint8 = ti_core.DataType_u8
u8 = uint8
uint16 = ti_core.DataType_u16
u16 = uint16
uint32 = ti_core.DataType_u32
u32 = uint32
uint64 = ti_core.DataType_u64
u64 = uint64

integer_types = [i8, i16, i32, i64, u8, u16, u32, u64, int]
integer_type_ids = [id(t) for t in integer_types]

types = real_types + integer_types
type_ids = [id(t) for t in types]

__all__ = [
'float32',
'f32',
'float64',
'f64',
'int8',
'i8',
'int16',
'i16',
'int32',
'i32',
'int64',
'i64',
'uint8',
'u8',
'uint16',
'u16',
'uint32',
'u32',
'uint64',
'u64',
]
6 changes: 5 additions & 1 deletion python/taichi/lang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from copy import deepcopy as _deepcopy

from taichi.lang.impl import *
from taichi.lang.kernel_arguments import ext_arr, template
from taichi.lang.kernel_impl import (KernelArgError, KernelDefError,
data_oriented, func, kernel, pyfunc)
from taichi.lang.matrix import Matrix, Vector
from taichi.lang.ndrange import GroupedNDRange, ndrange
from taichi.lang.ops import *
from taichi.lang.quant_impl import quant
from taichi.lang.runtime_ops import async_flush, sync
from taichi.lang.transformer import TaichiSyntaxError
from taichi.lang.type_factory_impl import type_factory
from taichi.lang.util import deprecated
from taichi.lang.util import *
yuanming-hu marked this conversation as resolved.
Show resolved Hide resolved

core = taichi_lang_core

Expand Down
5 changes: 3 additions & 2 deletions python/taichi/lang/expr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from taichi.lang import impl
from taichi.lang.common_ops import TaichiOperations
from taichi.lang.core import taichi_lang_core
from taichi.lang.util import (deprecated, is_taichi_class, python_scope,
to_numpy_type, to_pytorch_type)
from taichi.lang.util import (is_taichi_class, python_scope, to_numpy_type,
to_pytorch_type)
from taichi.misc.util import deprecated

import taichi as ti

Expand Down
Loading