Skip to content

Commit

Permalink
Make is_integer a type guard, move to typing
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Oct 3, 2024
1 parent 63c9bc0 commit 60741e5
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
3 changes: 1 addition & 2 deletions loopy/kernel/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
from pytools.tag import Tag, Taggable

from loopy.diagnostic import LoopyError
from loopy.tools import is_integer
from loopy.types import LoopyType
from loopy.typing import ExpressionT, ShapeType, auto
from loopy.typing import ExpressionT, ShapeType, auto, is_integer


if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions loopy/target/c/codegen/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
from loopy.expression import dtype_to_type_context
from loopy.symbolic import TypeCast
from loopy.target.c import CExpression
from loopy.tools import is_integer
from loopy.type_inference import TypeReader
from loopy.types import LoopyType
from loopy.typing import ExpressionT
from loopy.typing import ExpressionT, is_integer


__doc__ = """
Expand Down
4 changes: 0 additions & 4 deletions loopy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
logger = logging.getLogger(__name__)


def is_integer(obj):
return isinstance(obj, (int, np.integer))


def update_persistent_hash(obj, key_hash, key_builder):
"""
Custom hash computation function for use with
Expand Down
2 changes: 1 addition & 1 deletion loopy/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
SubstitutionRuleMappingContext,
parse_tagged_name,
)
from loopy.tools import is_integer
from loopy.translation_unit import (
CallablesInferenceContext,
TranslationUnit,
make_clbl_inf_ctx,
)
from loopy.types import NumpyType
from loopy.typing import is_integer


logger = logging.getLogger(__name__)
Expand Down
6 changes: 5 additions & 1 deletion loopy/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from typing import Optional, Tuple, TypeVar

import numpy as np
from typing_extensions import TypeAlias
from typing_extensions import TypeAlias, TypeIs

from pymbolic.primitives import Expression
from pymbolic.typing import ExpressionT, IntegerT
Expand All @@ -61,6 +61,10 @@ def not_none(obj: Optional[T]) -> T:
return obj


def is_integer(obj: object) -> TypeIs[int | np.integer]:
return isinstance(obj, (int, np.integer))


def integer_or_err(expr: ExpressionT) -> IntegerT:
if isinstance(expr, (int, np.integer)):
return expr
Expand Down

0 comments on commit 60741e5

Please sign in to comment.