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

BUG: Fix npt.ArrayLike treating (builtin) scalar- and __array__-sequences as mutually exclusive #20903

Closed
wants to merge 1 commit into from

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Jan 26, 2022

cc @bashtage

npt.ArrayLike consists four components:

  • Builtin scalar types
  • __array__ implementation
  • Nested sequences of builtins scalar types
  • Nested sequences of __array__ implementation

The problem was that, previously, the latter two were considered mutually exclusive, e.g. list[list[int] | np.ndarray] was not considered a proper npt.ArrayLike subtype. This PR fixes aforementioned issue by shuffling around the relevant union a bit.

@charris
Copy link
Member

charris commented Jan 27, 2022

There are test failures.

@BvB93
Copy link
Member Author

BvB93 commented Jan 27, 2022

There are test failures.

Yes, and the failure appears to be real...
Marking as draft for now until I've time to do some debugging.

@BvB93 BvB93 marked this pull request as draft January 27, 2022 09:40
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 29, 2022
@BvB93
Copy link
Member Author

BvB93 commented Feb 21, 2022

Closed in favor of #21102.

Based on the error message (see below), it would something this change causes list[str] to be recognized as a subtype of npt._ArrayLikeInt_co, thus causing the structured array to hit an overload-path intended for integer-based indexing.

The only reasonable explanation I can think of is that mypy hits some sort of recursion limit, causing str to be expanded to either Any or Sequence[Any]. With Any being Any, this would explain why a string is all of a sudden considered a "valid" integer subtype.

Error message
 __________________________ test_reveal[ndarray_misc] ___________________________

path = '/home/runner/work/numpy/numpy/venv-for-wheel/lib/python3.8/site-packages/numpy/typing/tests/data/reveal/ndarray_misc.pyi'
expression = 'reveal_type(AR_V[["a", "b"]])'
reveal = 'ndarray[Any, dtype[void]]'
expected_reveal = '209: note: Revealed type is "Any"', lineno = 209

    def _test_reveal(
        path: str,
        expression: str,
        reveal: str,
        expected_reveal: str,
        lineno: int,
    ) -> None:
        """Error-reporting helper function for `test_reveal`."""
        stripped_reveal = _STRIP_PATTERN.sub(strip_func, reveal)
        stripped_expected_reveal = _STRIP_PATTERN.sub(strip_func, expected_reveal)
        if stripped_reveal not in stripped_expected_reveal:
>           raise AssertionError(
                _REVEAL_MSG.format(lineno,
                                   expression,
                                   stripped_expected_reveal,
                                   stripped_reveal)
            )
E           AssertionError: Reveal mismatch at line 209
E           
E           Expression: reveal_type(AR_V[["a", "b"]])
E           Expected reveal: '209: note: Revealed type is "Any"'
E           Observed reveal: 'ndarray[Any, dtype[void]]'

expected_reveal = '209: note: Revealed type is "Any"'
expression = 'reveal_type(AR_V[["a", "b"]])'
lineno     = 209
path       = '/home/runner/work/numpy/numpy/venv-for-wheel/lib/python3.8/site-packages/numpy/typing/tests/data/reveal/ndarray_misc.pyi'
reveal     = 'ndarray[Any, dtype[void]]'
stripped_expected_reveal = '209: note: Revealed type is "Any"'
stripped_reveal = 'ndarray[Any, dtype[void]]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants