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
Type aliases can be used in expressions without issues:
from typing import Union
x = Union[int, str]
x.foo # No error
x = [1] # No error
I think using a Union type alias in an expression should probably be disallowed, or it should have a narrow type. Aliases to simple types such as "x = int" should be equivalent to the alias target.
The use of the alias t in the arg annotation for foo() causes a crash in mypy:
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 169, in <module>
main()
File "/usr/local/bin/mypy", line 34, in main
type_check_only(path, module, bin_dir, options)
File "/usr/local/bin/mypy", line 78, in type_check_only
python_path=options.python_path)
File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 164, in build
result = manager.process(UnprocessedFile(info, program_text))
File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 342, in process
next.process()
File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 802, in process
self.semantic_analyzer_pass3().visit_file(self.tree, self.tree.path)
File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 1881, in visit_file
file_node.accept(self)
File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 139, in accept
return visitor.visit_mypy_file(self)
File "/usr/local/lib/python3.5/site-packages/mypy/traverser.py", line 33, in visit_mypy_file
d.accept(self)
File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 324, in accept
return visitor.visit_func_def(self)
File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 1885, in visit_func_def
self.analyze(fdef.type)
File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 1922, in analyze
type.accept(analyzer)
File "/usr/local/lib/python3.5/site-packages/mypy/types.py", line 294, in accept
return visitor.visit_callable_type(self)
File "/usr/local/lib/python3.5/site-packages/mypy/typeanal.py", line 291, in visit_callable_type
arg_type.accept(self)
File "/usr/local/lib/python3.5/site-packages/mypy/types.py", line 453, in accept
return visitor.visit_union_type(self)
File "/usr/local/lib/python3.5/site-packages/mypy/typeanal.py", line 299, in visit_union_type
item.accept(self)
AttributeError: 'NoneType' object has no attribute 'accept'
(This can be avoided by writing out the annotation as Union[int, A]). I think it should just work.
It's a known issue that type aliases are flakey and incomplete: #298, #330
The intention is to have support for complex type aliases. The current prototype implementation of type aliases was implemented as part of a hack week project, and nobody has finished the implementation.
I'm going to look at it today and fix some issues.
@JukkaL This issue seems to be fixed now. I get these errors:
testold1.py:3: error: "Type alias to Union" has no attribute "foo"
testold1.py:4: error: Incompatible types in assignment (expression has type List[int], variable has type "Type alias to Union")
Probably, I fixed this accidentally when implemented generic aliases.
Activity
[-]Uniont type aliases valid as expressions[/-][+]Union type aliases valid as expressions[/+]jwbaugh commentedon Jan 29, 2015
So the following is not supposed to work, correct? Is there any way to define aliases for more complex types? Thanks. -John
gvanrossum commentedon Jan 29, 2015
The use of the alias t in the arg annotation for foo() causes a crash in mypy:
(This can be avoided by writing out the annotation as Union[int, A]). I think it should just work.
JukkaL commentedon Jan 31, 2015
It's a known issue that type aliases are flakey and incomplete: #298, #330
The intention is to have support for complex type aliases. The current prototype implementation of type aliases was implemented as part of a hack week project, and nobody has finished the implementation.
I'm going to look at it today and fix some issues.
JukkaL commentedon Jan 31, 2015
Type aliases now work but the this issue is till open (type aliases have type
Any).Two more small upstream typing.py changes by Ivan Levkivskyi (#329, #330
ilevkivskyi commentedon Feb 1, 2017
@JukkaL This issue seems to be fixed now. I get these errors:
Probably, I fixed this accidentally when implemented generic aliases.
JukkaL commentedon Feb 2, 2017
Yeah, this seems to work now.