Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Argument Clinic: DSLParser.parse_converter() can return an incorrect kwargs dict #106359

Closed
@erlend-aasland

Description

@erlend-aasland

Originally posted by@AlexWaygood in#106354 (comment):

Tools/clinic/clinic.py:5039: error: Incompatible return value type (got"tuple[str, bool, dict[str | None, Any]]", expected"tuple[str, bool, dict[str, Any]]")  [return-value]                    return name, False, kwargs                           ^~~~~~~~~~~~~~~~~~~

I think mypy is flagging a real bug in the code here. The issue is this block of code here:

caseast.Call(func=ast.Name(name)):
symbols=globals()
kwargs= {
node.arg:eval_ast_expr(node.value,symbols)
fornodeinannotation.keywords
}
returnname,False,kwargs

The function is annotated as returntuple[str, bool, KwargDict], andKwargDict is a type alias fordict[str, Any]. It's important that the dictionary that's the third element of the tuple only has strings as keys. If it doesn't, then this will fail:

return_converter=return_converters[name](**kwargs)

The code as written, however, doesn't guarantee that all the keys in thekwargs dictionary will be strings. In the dictionary comprehension, we can see thatannotation is anast.Call instance. That means thatannotation.keywords is of typelist[ast.keyword] -- we can see this from typeshed's stubs for theast module (which are an invaluable reference if you're working with ASTs in Python!):https://github.com/python/typeshed/blob/18d45d62aabe68fce78965c4920cbdeddb4b54db/stdlib/_ast.pyi#L324-L329. Ifannotation.keywords is of typelist[ast.keyword], that means that thenode variable in the dictionary comprehension is of typekeyword, which means (again using typeshed'sast stubs), thatnode.arg is of typestr | None:https://github.com/python/typeshed/blob/18d45d62aabe68fce78965c4920cbdeddb4b54db/stdlib/_ast.pyi#L516-L520. AKA, the keys in this dictionary are not always guaranteed to be strings -- there's a latent bug in this code!

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp