Repository: python/mypy

Issue:
Can not find "numpy.core._multiarray_umath" if numpy is installed via pip with --target argument.
<!--
  If you're new to mypy and you're not sure whether what you're experiencing is a mypy bug, please see the "Question and Help" form
  instead.
  Please also consider:

  - checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html
  - searching our issue tracker: https://github.com/python/mypy/issues to see if
    it's already been reported
  - asking on gitter chat: https://gitter.im/python/typing
-->

**Bug Report**

<!--
Note: If the problem you are reporting is about a specific library function, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
-->


When `numpy` is installed with a non-user or a non-system directory the is specified in `MYPYPATH` path mypy fails with some missing modules.

**To Reproduce**


1. `python3 -m pip install --user mypy`
2. `python3 -m pip install --target=$HOME/tmp/site-packages numpy`
3.
```
cat > $HOME/tmp/test.py <<EOF
from typing import Any, TYPE_CHECKING
import numpy as np
import numpy.typing as npt

a: npt.NDArray[Any] = np.ndarray((2,3))
if TYPE_CHECKING:
    reveal_type(a)
EOF
````
4. `MYPYPATH=$HOME/tmp/site-packages python3 -m mypy $HOME/tmp/test.py`
```
tmp/site-packages/numpy/core/umath.py:10: error: Cannot find implementation or library stub for module named "numpy.core._multiarray_umath"
tmp/site-packages/numpy/core/umath.py:10: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
tmp/site-packages/numpy/typing/_generic_alias.py:66: error: "object" has no attribute "__parameters__"
...
tmp/site-packages/numpy/typing/_ufunc.pyi:2937: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
tmp/site-packages/numpy/typing/_ufunc.pyi:3034: error: Class numpy.flexible has abstract attributes "__init__"
tmp/site-packages/numpy/typing/_ufunc.pyi:3034: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
tmp/site-packages/numpy/typing/_ufunc.pyi:3060: error: Class numpy.character has abstract attributes "__init__"
tmp/site-packages/numpy/typing/_ufunc.pyi:3060: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
Found 166 errors in 21 files (checked 1 source file)
```

**Expected Behavior**

<!--
  How did you expect your project to behave?
  It’s fine if you’re not sure your understanding is correct.
  Write down what you thought would happen. If you just expected no errors, you can delete this section.
-->

I would expect that mypy at step 4 works similar to 
```
python3 -m pip install --user numpy
python3 -m mypy $HOME/tmp/test.py
tmp/test.py:7: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[Any]]"
```

**Actual Behavior**

<!--
  Did something go wrong?
  Is something broken, or not behaving as you expected?
-->

```
MYPYPATH=$HOME/tmp/site-packages python3 -m mypy $HOME/tmp/test.py
tmp/site-packages/numpy/core/umath.py:10: error: Cannot find implementation or library stub for module named "numpy.core._multiarray_umath"
tmp/site-packages/numpy/core/umath.py:10: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
tmp/site-packages/numpy/typing/_generic_alias.py:66: error: "object" has no attribute "__parameters__"
tmp/site-packages/numpy/typing/_generic_alias.py:67: error: Value of type "object" is not indexable
tmp/site-packages/numpy/typing/_generic_alias.py:170: error: Argument 2 to "_reconstruct_alias" has incompatible type "Iterator[object]"; expected "Iterator[TypeVar]"
tmp/site-packages/numpy/typing/_add_docstring.py:38: error: Need type annotation for "new_lines" (hint: "new_lines: List[<type>] = ...")
tmp/site-packages/numpy/lib/polynomial.pyi:59: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
tmp/site-packages/numpy/lib/polynomial.pyi:59: error: Overloaded function signatures 1 and 3 overlap with incompatible return types
...
tmp/site-packages/numpy/typing/_ufunc.pyi:3034: error: Class numpy.flexible has abstract attributes "__init__"
tmp/site-packages/numpy/typing/_ufunc.pyi:3034: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
tmp/site-packages/numpy/typing/_ufunc.pyi:3060: error: Class numpy.character has abstract attributes "__init__"
tmp/site-packages/numpy/typing/_ufunc.pyi:3060: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
Found 166 errors in 21 files (checked 1 source file)
```

**Your Environment**

<!-- Include as many relevant details about the environment you experienced the bug in -->

- Mypy version used: 0.942
- Mypy command-line flags: `MYPYPATH=$HOME/tmp/site-packages mypy $HOME/tmp/test.py`
- Mypy configuration options from `mypy.ini` (and other config files): --
- Python version used: 3.8.10
- Operating system and version: Ubuntu 20.04



Discussion:

Resolving patch:
diff --git a/mypy/types.py b/mypy/types.py
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -2760,6 +2760,7 @@ def get_proper_types(
     TypeTranslator as TypeTranslator,
     TypeVisitor as TypeVisitor,
 )
+from mypy.typetraverser import TypeTraverserVisitor
 
 
 class TypeStrVisitor(SyntheticTypeVisitor[str]):
@@ -3122,6 +3123,18 @@ def visit_type_var(self, typ: TypeVarType) -> Type:
         return typ
 
 
+class LocationSetter(TypeTraverserVisitor):
+    # TODO: Should we update locations of other Type subclasses?
+    def __init__(self, line: int, column: int) -> None:
+        self.line = line
+        self.column = column
+
+    def visit_instance(self, typ: Instance) -> None:
+        typ.line = self.line
+        typ.column = self.column
+        super().visit_instance(typ)
+
+
 def replace_alias_tvars(
     tp: Type, vars: List[str], subs: List[Type], newline: int, newcolumn: int
 ) -> Type:
@@ -3130,6 +3143,7 @@ def replace_alias_tvars(
     """
     replacer = InstantiateAliasVisitor(vars, subs)
     new_tp = tp.accept(replacer)
+    new_tp.accept(LocationSetter(newline, newcolumn))
     new_tp.line = newline
     new_tp.column = newcolumn
     return new_tp


Test patch:
diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test
--- a/test-data/unit/check-generics.test
+++ b/test-data/unit/check-generics.test
@@ -648,6 +648,30 @@ reveal_type(f3()) # N: Revealed type is "Union[builtins.int, __main__.Node[built
 
 [builtins fixtures/list.pyi]
 
+[case testGenericTypeAliasesWithNestedArgs]
+# flags: --pretty --show-error-codes
+import other
+a: other.Array[float]
+reveal_type(a) # N: Revealed type is "other.array[Any, other.dtype[builtins.float]]"
+
+[out]
+main:3: error: Type argument "float" of "dtype" must be a subtype of "generic"  [type-var]
+    a: other.Array[float]
+       ^
+[file other.py]
+from typing import Any, Generic, TypeVar
+
+DT = TypeVar("DT", covariant=True, bound=dtype[Any])
+DTS = TypeVar("DTS", covariant=True, bound=generic)
+S = TypeVar("S", bound=Any)
+ST = TypeVar("ST", bound=generic, covariant=True)
+
+class common: pass
+class generic(common): pass
+class dtype(Generic[DTS]): pass
+class array(common, Generic[S, DT]): pass
+Array = array[Any, dtype[ST]]
+
 [case testGenericTypeAliasesAny]
 from typing import TypeVar, Generic
 T = TypeVar('T')