Skip to content

Forward reference to a type alias results in an "Invalid type ..." error #2241

Closed

Description

@jstasiak

mypy 0.4.5 and 0.4.4, CPython 3.5.1 and 3.5.2.

Code:

# code.py
def f(p: 'Alias') -> None:
    print(p)

Alias = int

Expected result:

% mypy code.py
%

Actual result:

% mypy code.py
code.py: note: In function "f":
code.py:1: error: Invalid type "code.Alias"
%

Activity

  1. gvanrossum commented on Oct 11, 2016

    @gvanrossum
    Member
  2. JukkaL commented on Oct 11, 2016

    @JukkaL
    Collaborator

    I think that type aliases also don't work reliably across different modules in import cycles.

  3. added this to the milestone on Oct 13, 2016
  4. TRManderson commented on Dec 1, 2016

    @TRManderson
    Contributor

    As far as I can tell, it is currently not possible to represent the output of json.load/s with types due to this.

    JSON = Union[None, str, int, float, bool, List['JSON'], Dict[str, 'JSON']]

    Adding newtypes in various bits didn't help the situation, I'm still getting 'Invalid type'

  5. removed this from the milestone on Mar 29, 2017
  6. JelleZijlstra commented on May 23, 2017

    @JelleZijlstra
    Member

    Here's another example I ran into:

    from typing import List
    
    class A(object):
        b: 'B'  # E: Invalid type "recurse.B"
    
    B = List[A]
  7. linclelinkpart5 commented on May 30, 2017

    @linclelinkpart5

    I'm also seeing this occur with new Python 3.6-style NamedTuple type hint declarations.

    import typing as typ
    
    class EdgeDef(typ.NamedTuple):
        id: int
        stack_cmd: 'StackCommand' # ERROR: Invalid type "test.StackCommand"
    
    # Other defs that depend on `EdgeDef`
    
    StackCommand = typ.Tuple[bool, int]
    
  8. gvanrossum commented on May 31, 2017

    @gvanrossum
    Member

    I think we have enough examples at this point. :-)

  9. refi64 commented on May 31, 2017

    @refi64
    Contributor

    @ThatsGobbles Is that the same issue though? This issue is with recursive type aliases, but your example doesn't actually have any recursion, just and out-of-order alias.

  10. ilevkivskyi commented on May 31, 2017

    @ilevkivskyi
    Member

    @kirbyfan64

    This issue is with recursive type aliases

    The original example does not have any recursion. Moreover, I am quite sure that there is no big difference between recursive and non-recursive situations with classes. The problem is very simple: type aliases are not analysed during first pass, but solution is not easy and requires refactoring semantic analyser (see roadmap).

  11. refi64 commented on May 31, 2017

    @refi64
    Contributor

    cough I...uhh... totally knew that... cough

  12. added a commit that references this issue on Sep 27, 2017
    a611b11
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