Skip to content

Commit

Permalink
Move view without verification to Array to fix type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Sep 11, 2022
1 parent 0eacac3 commit 79655f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
25 changes: 25 additions & 0 deletions src/galois/_domains/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ def _convert_iterable_to_elements(cls, iterable: IterableLike) -> np.ndarray:
Convert an iterable (recursive) to a NumPy integer array. Convert any strings to integers along the way.
"""

###############################################################################
# View methods
###############################################################################

@classmethod
def _view(cls, array: np.ndarray) -> Array:
"""
View the input array to the Array subclass `A` using the `_view_without_verification()` context manager. This disables
bounds checking on the array elements. Instead of `x.view(A)` use `A._view(x)`. For internal library use only.
"""
with cls._view_without_verification():
array = array.view(cls)
return array

@classmethod
@contextlib.contextmanager
def _view_without_verification(cls):
"""
A context manager to disable verifying array element values are within [0, order). For internal library use only.
"""
prev_value = cls._verify_on_view
cls._verify_on_view = False
yield
cls._verify_on_view = prev_value

###############################################################################
# Alternate constructors
###############################################################################
Expand Down
24 changes: 0 additions & 24 deletions src/galois/_domains/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

import abc
import contextlib
import inspect
from typing import List, TYPE_CHECKING
from typing_extensions import Literal
Expand Down Expand Up @@ -93,29 +92,6 @@ def _assign_ufuncs(cls):
# This will be implemented in UFuncMixin and its children
return

###############################################################################
# View methods
###############################################################################

def _view(cls, array: np.ndarray) -> Array:
"""
View the input array to the Array subclass `A` using the `_view_without_verification()` context manager. This disables
bounds checking on the array elements. Instead of `x.view(A)` use `A._view(x)`. For internal library use only.
"""
with cls._view_without_verification():
array = array.view(cls)
return array

@contextlib.contextmanager
def _view_without_verification(cls):
"""
A context manager to disable verifying array element values are within [0, order). For internal library use only.
"""
prev_value = cls._verify_on_view
cls._verify_on_view = False
yield
cls._verify_on_view = prev_value

###############################################################################
# Class properties
###############################################################################
Expand Down

0 comments on commit 79655f2

Please sign in to comment.