Skip to content

Commit 7092f55

Browse files
committed
Cleanup imports in graph/rewriting/basic.py
1 parent 2d46d60 commit 7092f55

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

pytensor/graph/rewriting/basic.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
from collections.abc import Callable, Iterable, Sequence
1414
from functools import _compose_mro, partial # type: ignore
1515
from itertools import chain
16-
from typing import TYPE_CHECKING, Literal
16+
from typing import Literal
1717

18-
import pytensor
1918
from pytensor.configdefaults import config
2019
from pytensor.graph import destroyhandler as dh
2120
from pytensor.graph.basic import (
@@ -30,15 +29,12 @@
3029
from pytensor.graph.features import AlreadyThere, Feature
3130
from pytensor.graph.fg import FunctionGraph, Output
3231
from pytensor.graph.op import Op
32+
from pytensor.graph.rewriting.unify import Var, convert_strs_to_vars
3333
from pytensor.graph.utils import AssocList, InconsistencyError
3434
from pytensor.misc.ordered_set import OrderedSet
3535
from pytensor.utils import flatten
3636

3737

38-
if TYPE_CHECKING:
39-
from pytensor.graph.rewriting.unify import Var
40-
41-
4238
_logger = logging.getLogger("pytensor.graph.rewriting.basic")
4339

4440
RemoveKeyType = Literal["remove"]
@@ -1406,8 +1402,6 @@ def __init__(
14061402
frequent `Op`, which will prevent the rewrite from being tried as often.
14071403
14081404
"""
1409-
from pytensor.graph.rewriting.unify import convert_strs_to_vars
1410-
14111405
var_map: dict[str, Var] = {}
14121406
self.in_pattern = convert_strs_to_vars(in_pattern, var_map=var_map)
14131407
self.out_pattern = convert_strs_to_vars(out_pattern, var_map=var_map)
@@ -1449,9 +1443,6 @@ def transform(self, fgraph, node, get_nodes=True):
14491443
if ret is not False and ret is not None:
14501444
return dict(zip(real_node.outputs, ret, strict=True))
14511445

1452-
if node.op != self.op:
1453-
return False
1454-
14551446
if len(node.outputs) != 1:
14561447
# PatternNodeRewriter doesn't support replacing multi-output nodes
14571448
return False
@@ -1480,11 +1471,13 @@ def transform(self, fgraph, node, get_nodes=True):
14801471

14811472
[old_out] = node.outputs
14821473
if not old_out.type.is_super(ret.type):
1474+
from pytensor.tensor.type import TensorType
1475+
14831476
# Type doesn't match
14841477
if not (
14851478
self.allow_cast
1486-
and isinstance(old_out.type, pytensor.tensor.TensorType)
1487-
and isinstance(ret.type, pytensor.tensor.TensorType)
1479+
and isinstance(old_out.type, TensorType)
1480+
and isinstance(ret.type, TensorType)
14881481
):
14891482
return False
14901483

@@ -2736,10 +2729,12 @@ def check_stack_trace(f_or_fgraph, ops_to_check="last", bug_print="raise"):
27362729
otherwise.
27372730
27382731
"""
2739-
if isinstance(f_or_fgraph, pytensor.compile.function.types.Function):
2740-
fgraph = f_or_fgraph.maker.fgraph
2741-
elif isinstance(f_or_fgraph, pytensor.graph.fg.FunctionGraph):
2732+
from pytensor.compile.function.types import Function
2733+
2734+
if isinstance(f_or_fgraph, FunctionGraph):
27422735
fgraph = f_or_fgraph
2736+
elif isinstance(f_or_fgraph, Function):
2737+
fgraph = f_or_fgraph.maker.fgraph
27432738
else:
27442739
raise ValueError("The type of f_or_fgraph is not supported")
27452740

0 commit comments

Comments
 (0)