This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author BTaskaya
Recipients BTaskaya, cleoold
Date 2021-03-26.19:08:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1616785730.66.0.0641826988554.issue43609@roundup.psfhosted.org>
In-reply-to
Content
> @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.

Though you can not simple parse this and expect to get the AST you gave as an example (fun2). This would be parsed like
FunctionType(
    argtypes=[],
    returns=BinOp(
        left=BinOp(
            left=Name(id='int', ctx=Load()),
            op=BitOr(),
            right=Name(id='str', ctx=Load())),
        op=BitOr(),
        right=Constant(value=None)))

and we would roundtrip it;
>>> source = '() -> (int | str) | None'
>>> ast1 = ast.parse(source, mode='func_type')
>>> ast2 = ast.parse(ast.unparse(ast1), mode='func_type')
>>> ast.dump(ast1) == ast.dump(ast2)
True


I get what you mean (like 2 separate nodes connected with Union[]) though you can not simply parse the func_type syntax ( () -> ... ) with the normal parser, so you can't use it with binary or. It seems like this is not a bug on ourside, but rather a bug on the creator of this source (like union of func type and binary or).
History
Date User Action Args
2021-03-26 19:08:50BTaskayasetrecipients: + BTaskaya, cleoold
2021-03-26 19:08:50BTaskayasetmessageid: <1616785730.66.0.0641826988554.issue43609@roundup.psfhosted.org>
2021-03-26 19:08:50BTaskayalinkissue43609 messages
2021-03-26 19:08:50BTaskayacreate