astroid icon indicating copy to clipboard operation
astroid copied to clipboard

Incorrect inference of decorated function

Open apmorton opened this issue 1 year ago • 0 comments

Using astroid 3.3.5

import astroid

module = astroid.parse(
    """
def decorate(obj=None):
    if obj is None:
        return lambda x: decorate(x)
    if isinstance(obj, property):
        return property()
    return obj


@decorate()
def func() -> str:
    return 'foo'


x = func()
"""
)

func = module.body[1]
print(func)
print(func.inferred()[0])

Astroid incorrectly infers this function as Property.func.

This causes the final line x = func() to emit an error under pylint:

E1111: Assigning result of a function call, where the function has no return (assignment-from-no-return)

If you change to:

@decorate
def func() -> str:
    return 'foo'

Astroid correctly infers this as FunctionDef.func.

This is a minimized repro of the error reported here https://github.com/dagster-io/dagster/issues/24009

apmorton avatar Nov 01 '24 15:11 apmorton