Skip to content

Descriptor decorated constructors lead to false positives in ignored imports #714

@ethereon

Description

@ethereon

Consider the following valid Python code:

import numpy

class Wrapper:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):
        return lambda **kwargs: self.func(instance, wrapped=True, **kwargs)

class Foo:
    @Wrapper
    def __init__(self, **kwargs):
        print(f'{kwargs}')

Foo(bar=numpy.nan)

Pyright (tested with 1.1.41) will incorrectly report the numpy import as unused.

While I'm not very familiar with Pyright's internals, a quick debug session indicates the following:

validateConstructorArguments invokes validateCallArguments for Foo(bar=np.nan). The inferred type of __init__ is an object (Wrapper) and it attempts to look up Wrapper.__call__. This failed call to getTypeFromObjectMember doesn't set argumentErrors to true (not sure if that's intentional), but consequently, the numpy reference in the argument doesn't get added to accessedSymbolMap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions