You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Forward reference to a type alias results in an "Invalid type ..." error #2241
Hm, interesting. Looks like aliases can't be used in forward references.
This is unfortunate, but shouldn't be hard to work around, right? I don't
know yet what it would take to fix (both aliases and forward references are
somewhat murky corners of mypy).
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]
@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.
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).
Activity
gvanrossum commentedon Oct 11, 2016
JukkaL commentedon Oct 11, 2016
I think that type aliases also don't work reliably across different modules in import cycles.
TRManderson commentedon Dec 1, 2016
As far as I can tell, it is currently not possible to represent the output of
json.load/swith 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'
JelleZijlstra commentedon May 23, 2017
Here's another example I ran into:
linclelinkpart5 commentedon May 30, 2017
I'm also seeing this occur with new Python 3.6-style NamedTuple type hint declarations.
gvanrossum commentedon May 31, 2017
I think we have enough examples at this point. :-)
refi64 commentedon May 31, 2017
@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.
ilevkivskyi commentedon May 31, 2017
@kirbyfan64
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).
refi64 commentedon May 31, 2017
cough I...uhh... totally knew that... cough
Fix crashes and fails in forward references (#3952)