Repository: pandas-dev/pandas
File: pandas/core/arrays/_arrow_string_mixins.py (Python)

Code before:

    from pandas._typing import Scalar


class ArrowStringArrayMixin:
    _pa_array: pa.ChunkedArray

    def __init__(self, *args, **kwargs) -> None:
        raise NotImplementedError

    def _from_pyarrow_array(self, pa_array) -> Self:
        raise NotImplementedError

    def _convert_bool_result(self, result, na=lib.no_default, method_name=None):
        # Convert a bool-dtype result to the appropriate result type
        raise NotImplementedError

    def _convert_int_result(self, result):
        # Convert an integer-dtype result to the appropriate result type
        raise NotImplementedError

    def _apply_elementwise(self, func: Callable) -> list[list[Any]]:
        raise NotImplementedError

    @staticmethod
    def _has_regex_lookaround(pat: str | re.Pattern) -> bool:
        """
        Determine if regex pattern contains a lookahead or lookbehind.

        Parameters
        ----------
        pat: str | re.Pattern
            Regex pattern.

        Returns
        -------
        bool
            Whether `pat` contains a lookahead or lookbehind.
        """
        # error: Module "re" has no attribute "_parser"
        from re import _parser  # type: ignore[attr-defined]

        def has_assert(tokens):
            # For certain op codes we need to recurse.
            for op_code, argument in tokens:
                if (
                    (op_code == _parser.SUBPATTERN and has_assert(argument[3]))
                    or (
                        op_code == _parser.BRANCH
                        and any(has_assert(tokens) for tokens in argument[1])
                    )

Review comment (suggestion):
Couldn't find a better spot for this. Maybe stash in `core.common` or `core.utils`? Could also just have it as a free function in this module.

cc @jbrockmendel if you have any throughts.

Code after:

    from pandas._typing import Scalar


class ArrowStringArrayMixin:
    _pa_array: pa.ChunkedArray

    def __init__(self, *args, **kwargs) -> None:
        raise NotImplementedError

    def _from_pyarrow_array(self, pa_array) -> Self:
        raise NotImplementedError

    def _convert_bool_result(self, result, na=lib.no_default, method_name=None):
        # Convert a bool-dtype result to the appropriate result type
        raise NotImplementedError

    def _convert_int_result(self, result):
        # Convert an integer-dtype result to the appropriate result type
        raise NotImplementedError

    def _apply_elementwise(self, func: Callable) -> list[list[Any]]:
        raise NotImplementedError

    @staticmethod
    def _has_regex_lookaround(pat: str | re.Pattern) -> bool:
        """
        Determine if regex pattern contains a lookahead or lookbehind.

        Parameters
        ----------
        pat: str | re.Pattern
            Regex pattern.

        Returns
        -------
        bool
            Whether `pat` contains a lookahead or lookbehind.
        """
        try:
            # error: Module "re" has no attribute "_parser"
            from re import _parser  # type: ignore[attr-defined]

            regex_parser = _parser.parse
        except Exception as err:
            raise type(err)(
                "Incompatible version for regex; you will need to upgrade pandas "
                "or downgrade Python"
            ) from err

        def has_assert(tokens):