Skip to content

Wrong inference with default argument values #2899

@jkmnt

Description

@jkmnt

Got the unexpected result: default value of function argument is treated as the 'only' value.

Minimal example:

from astroid import extract_node

bad, good = extract_node("""
def bad(foo: str | None = None):
    a = 1 if foo else ""  #@

def good(foo: str):
    a = 1 if foo else ""  #@
""")

print(bad.value.inferred())
print(good.value.inferred())

Result:

[<Const.str l.3 at 0x27945e7eae0>]
[<Const.int l.6 at 0x27945e7ec90>, <Const.str l.6 at 0x27945e7ecc0>]

I expected the first .inferred to return two results. The second one is ok.

Astroid version: 4.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪳Needs PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions