[issue43609] ast.unparse-ing a FunctionType gives ambiguous result

2021-03-23 Thread midori


New submission from midori :

Hi all, this is probably my first issue here, so don't blame me if I do 
something wrong lol

The ast.FunctionType gives syntax like (a, b) -> c for function types, this is 
ok, and also since Python 3.10 we can use X | Y to denote unions, this is ok. 
So Given the following two trees:

fun1 = ast.FunctionType(
argtypes=[],
returns=ast.BinOp(
left=ast.Name(id='int'),
op=ast.BitOr(),
right=ast.Name(id='float'),
)
)
fun2 = ast.BinOp(
left=ast.FunctionType(
argtypes=[],
returns=ast.Name(id='int'),
),
op=ast.BitOr(),
right=ast.Name(id='float'),
)

Calling:

print(ast.unparse(fun1))
print(ast.unparse(fun2))

The results are these:

() -> int | float
() -> int | float

So there is some ambiguity. By feeding this string to 
ast.parse(mode='func_type'), I know that it means "returning a union".

Don't know if there is any impact to simply add a pair of parens, or does this 
problem even matters at all.

I tested it using Python 3.10 a6 and Python 3.9.2.

--
components: Library (Lib)
messages: 389429
nosy: Batuhan Taskaya, cleoold
priority: normal
severity: normal
status: open
title: ast.unparse-ing a FunctionType gives ambiguous result
type: behavior
versions: Python 3.10

___
Python tracker 
<https://bugs.python.org/issue43609>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43609] ast.unparse-ing a FunctionType gives ambiguous result

2021-03-25 Thread midori


midori  added the comment:

@BTaskaya I've seen this in third party ides and type checker. For example they 
are referring to "Union[Callable[[], Union[int, str]], None]" as "() -> (int | 
str) | None" where there are parentheses around the returns.

--

___
Python tracker 
<https://bugs.python.org/issue43609>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com