Skip to content

PEP 593 aliasing Annotated is not working (type of instance is TypeVar instead of the actual type) #7729

@jstasiak

Description

@jstasiak

I'm running mypy 222b14d and Python 3.7.4. I added the following code to the test suite:

diff --git a/test-data/unit/check-annotated.test b/test-data/unit/check-annotated.test
index 1eaec3805d63..239ba2f13961 100644
--- a/test-data/unit/check-annotated.test
+++ b/test-data/unit/check-annotated.test
@@ -63,3 +63,11 @@ reveal_type(x)  # N: Revealed type is 'Any'
 [case testAnnotatedNoImport]
 x: Annotated[int, ...]  # E: Name 'Annotated' is not defined
 reveal_type(x)  # N: Revealed type is 'Any'
+
+[case testAnnotatedAlias]
+from typing import TypeVar
+from typing_extensions import Annotated
+T = TypeVar('T')
+Alias = Annotated[T, ...]
+x: Alias[int]
+reveal_type(x)  # N: Revealed type is 'builtins.int'

The tests fail with:

mypy% pytest -k "Annotated"

============================================================================================================ test session starts =============================================================================================================
platform darwin -- Python 3.7.4, pytest-3.10.1, py-1.8.0, pluggy-0.9.0
rootdir: /Users/user/projects/mypy, inifile: pytest.ini
plugins: xdist-1.25.0, forked-0.2, cov-2.5.1
gw0 [32] / gw1 [32] / gw2 [32] / gw3 [32] / gw4 [32] / gw5 [32] / gw6 [32] / gw7 [32]
..............................F.                                                                                                                                                                                                       [100%]
================================================================================================================== FAILURES ==================================================================================================================
_____________________________________________________________________________________________________________ testAnnotatedAlias _____________________________________________________________________________________________________________
[gw6] darwin -- Python 3.7.4 /Users/user/.ve37/bin/python
data: /Users/user/projects/mypy/test-data/unit/check-annotated.test:67:
/Users/user/projects/mypy/mypy/test/testcheck.py:127: in run_case
    self.run_case_once(testcase)
/Users/user/projects/mypy/mypy/test/testcheck.py:222: in run_case_once
    assert_string_arrays_equal(output, a, msg.format(testcase.file, testcase.line))
/Users/user/projects/mypy/mypy/test/helpers.py:116: in assert_string_arrays_equal
    raise AssertionError(msg)
E   AssertionError: Unexpected type checker output (/Users/user/projects/mypy/test-data/unit/check-annotated.test, line 67)
------------------------------------------------------------------------------------------------------------ Captured stderr call ------------------------------------------------------------------------------------------------------------
Expected:
  main:6: note: Revealed type is 'builtins.int' (diff)
Actual:
  main:6: note: Revealed type is 'T?'           (diff)

Alignment of first line difference:
  E: ...te: Revealed type is 'builtins.int'
  A: ...te: Revealed type is 'T?'
                              ^

This is a bug I believe. I noticed this when I wanted to trigger mypy error when type-checking code that uses Annotated aliases and failed. I'd create a patch for this but I'm out of my depth when it comes to mypy type analysis, so the next best thing is to report this.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy