Repository: python/mypy

Issue:
`TypeAlias` does not work when attribute-accessed from the `typing_extensions` package
**Bug Report**

`TypeAlias` does not trigger errors in `mypy` if accessed from the `typing_extensions` package (rather than being imported directly).

**To Reproduce**

```python
import typing as tp
import typing_extensions as tpx
from typing_extensions import TypeAlias

ASDF: TypeAlias = tp.Literal["A", "S", "D", "F"]
JKL: tpx.TypeAlias = tp.Literal["J", "K", "L"]

Int: tpx.TypeAlias = int
Str: TypeAlias = str
```

**Expected Behavior**

Everything below should throw errors in `mypy`:

```python
G: ASDF = "G"  # error: Incompatible types in assignment (expression has type "Literal['G']", variable has type "Union[Literal['A'], Literal['S'], Literal['D'], Literal['F']]")
H: JKL = "H"  # error: Incompatible types in assignment (expression has type "Literal['H']", variable has type "Union[Literal['J'], Literal['K'], Literal['L']]")
n: Int = "9"  # error: Incompatible types in assignment (expression has type "str", variable has type "int")
s: Str = 9  # error: Incompatible types in assignment (expression has type "int", variable has type "str")
```

**Actual Behavior**

The aliases `JKL` and `Int`, typed using `tpx.TypeAlias`, do not trigger errors with `mypy`:

```python
G: ASDF = "G"  # error: Incompatible types in assignment (expression has type "Literal['G']", variable has type "Union[Literal['A'], Literal['S'], Literal['D'], Literal['F']]")
H: JKL = "H"  # Expected error here; got none
n: Int = "9"  # Expected error here; got none
s: Str = 9  # error: Incompatible types in assignment (expression has type "int", variable has type "str")
```

**Your Environment**

- Mypy version used: `0.931`
- Mypy command-line flags: None
- Mypy configuration options from `mypy.ini` (and other config files):
  ```
  [mypy]
  plugins = numpy.typing.mypy_plugin
  ignore_missing_imports = True
  ```
- Python version used: `3.8.12`
- Operating system and version: Elementary OS 6.1 Jólnir (Based on Ubuntu 20.04 LTS)



Discussion:

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -2647,7 +2647,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
 
         pep_613 = False
         if s.unanalyzed_type is not None and isinstance(s.unanalyzed_type, UnboundType):
-            lookup = self.lookup(s.unanalyzed_type.name, s, suppress_errors=True)
+            lookup = self.lookup_qualified(s.unanalyzed_type.name, s, suppress_errors=True)
             if lookup and lookup.fullname in TYPE_ALIAS_NAMES:
                 pep_613 = True
         if not pep_613 and s.unanalyzed_type is not None:


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -107,7 +107,7 @@ jobs:
         ./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
         source $VENV/bin/activate
     - name: Install tox
-      run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.20.1
+      run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
     - name: Compiled with mypyc
       if: ${{ matrix.test_mypyc }}
       run: |
diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test
--- a/test-data/unit/check-type-aliases.test
+++ b/test-data/unit/check-type-aliases.test
@@ -57,7 +57,7 @@ Never = NoReturn
 a: Never  # Used to be an error here
 
 def f(a: Never): ...
-f(5)  # E: Argument 1 to "f" has incompatible type "int"; expected "NoReturn" 
+f(5)  # E: Argument 1 to "f" has incompatible type "int"; expected "NoReturn"
 [case testImportUnionAlias]
 import typing
 from _m import U
@@ -702,6 +702,30 @@ x: TypeAlias = list(int)  # E: Invalid type alias: expression is not a valid typ
 a: x
 [builtins fixtures/tuple.pyi]
 
+[case testAliasedImportPep613]
+import typing as tpp
+import typing_extensions as tpx
+from typing import TypeAlias as TPA
+from typing_extensions import TypeAlias as TXA
+import typing
+import typing_extensions
+
+Int1: tpp.TypeAlias = int
+Int2: tpx.TypeAlias = int
+Int3: TPA = int
+Int4: TXA = int
+Int5: typing.TypeAlias = int
+Int6: typing_extensions.TypeAlias = int
+
+x1: Int1 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+x2: Int2 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+x3: Int3 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+x4: Int4 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+x5: Int5 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+x6: Int6 = "str"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
+[builtins fixtures/tuple.pyi]
+[typing fixtures/typing-medium.pyi]
+
 [case testFunctionScopePep613]
 from typing_extensions import TypeAlias