Skip to content

Crash on numtype numpy stubs package related to property #18764

Closed

Description

@hauntsaninja
rm -rf tmp
mkdir tmp
cd tmp
git clone https://github.com/numpy/numtype.git
mypy numtype/src --show-traceback 

numtype commit 864cda2

Seems similar to #18648 cc @ilevkivskyi

...
  File "/Users/shantanu/dev/mypy/mypy/nodes.py", line 580, in accept
    return visitor.visit_overloaded_func_def(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 641, in visit_overloaded_func_def
    self._visit_overloaded_func_def(defn)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 697, in _visit_overloaded_func_def
    found_method_base_classes = self.check_method_override(defn)
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 2022, in check_method_override
    result = self.check_method_or_accessor_override_for_base(
        defn, base, check_override_compatibility
    )
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 2060, in check_method_or_accessor_override_for_base
    if self.check_method_override_for_base_with_name(defn, name, base):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 2194, in check_method_override_for_base_with_name
    self.check_setter_type_override(defn, base_attr, base)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 2084, in check_setter_type_override
    original_type, is_original_setter = get_raw_setter_type(base_node)
                                        ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/Users/shantanu/dev/mypy/mypy/checker.py", line 8919, in get_raw_setter_type
    assert var.type is not None
           ^^^^^^^^^^^^^^^^^^^^
AssertionError: 
numtype/src/numpy-stubs/ma/core.pyi:308: : note: use --pdb to drop into pdb

Activity

  1. hauntsaninja commented on Mar 7, 2025

    @hauntsaninja
    CollaboratorAuthor

    Partially minimised repro:

    rm -rf repro
    mkdir repro
    cd repro
    
    python -m venv env
    env/bin/python -m pip install numpy
    
    mkdir numpy-stubs
    
    echo '
    from typing import Any, Generic
    from typing_extensions import TypeVar
    
    from . import matlib as matlib  # load bearing since it imports numpy
    
    _DTypeT2_co = TypeVar("_DTypeT2_co", covariant=True)
    _ShapeT2 = TypeVar("_ShapeT2", bound=tuple[int, ...])
    _ShapeT2_co = TypeVar("_ShapeT2_co", bound=tuple[int, ...], covariant=True)
    _ScalarT2_co = TypeVar("_ScalarT2_co", covariant=True)
    
    class ndarray(Generic[_ShapeT2_co, _DTypeT2_co]):
        @property
        def shape(self) -> _ShapeT2_co: ...
        @shape.setter
        def shape(self: ndarray[_ShapeT2, Any], shape: _ShapeT2, /) -> None: ...
    ' > numpy-stubs/__init__.pyi
    
    echo '
    from typing_extensions import TypeVar
    
    import numpy as np
    
    _ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
    _ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], default=tuple[int, ...], covariant=True)
    _DTypeT_co = TypeVar("_DTypeT_co", covariant=True)
    
    class MaskedArray(np.ndarray[_ShapeT_co, _DTypeT_co]):
        @property
        def shape(self) -> _ShapeT_co: ...
        @shape.setter
        def shape(self: MaskedArray[_ShapeT], shape: _ShapeT, /) -> None: ...
    ' > numpy-stubs/core.pyi
    
    mypy . --python-executable env/bin/python --show-traceback
    
  2. ilevkivskyi commented on Mar 7, 2025

    @ilevkivskyi
    Member

    Oh well, I was wandering why some code paths (inconsistently) expose settable property type as Overloaded(...) with a single item. Ultimately, this is just another case of the checkmember.py mega-issue. I am still not sure what to do about this particular case: another band-aid, or try cleaning this up for real. Taking into account the 1.16 is coming soon it will likely be the former, but I want to think a bit more, before I do this.

  3. ilevkivskyi commented on Mar 8, 2025

    @ilevkivskyi
    Member

    @hauntsaninja could you please check that #18774 fixes the original crash? (I have a very minimalist example in the tests.)

  4. jorenham commented on Mar 10, 2025

    @jorenham
    Contributor

    I can confirm that latest mypy main (af5186e) now indeed runs without crashing on numtype (8598dc9): Thanks, @ilevkivskyi and @hauntsaninja!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions