New submission from midori <cleo...@member.fsf.org>:

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 <rep...@bugs.python.org>
<https://bugs.python.org/issue43609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to