Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False "not accessed" positive #1096

Closed
memeplex opened this issue Mar 27, 2021 · 5 comments
Closed

False "not accessed" positive #1096

memeplex opened this issue Mar 27, 2021 · 5 comments
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@memeplex
Copy link

Environment data

  • Language Server version: 2021.3.3
  • OS and version: MacOS Big Sur 11.2
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.6

Expected behaviour

In the following code fragment:

        hess = np.eye(n + 1)
        res = sp.optimize.minimize(
            lambda w: 0.5 * ((w - weights) ** 2).sum(),
            weights,
            jac=lambda w: w - weights,
            hess=lambda w: hess,
            constraints=[sp.minimize.LinearConstraint(A, lb, ub)],
        )
        if not res.success:
            raise OptimizationError(res)
        weights = iter(res.x)
        c.weight = next(weights)
        for g in c.groups:
            g.weight = next(weights)

pylance is showing "hess" is not accessed for the. first line. The problem is related to the way I access the results of minimize: as an iterator on res.x. This seems to make pylance believe I'm not using the result at all.

Actual behaviour

Since I'm actually using the results of minimize and hess is indeed part of the input of minimize, the error is spurious and should not be shown.

@memeplex
Copy link
Author

Indeed, removing:

        weights = iter(res.x)
        c.weight = next(weights)
        for g in c.groups:
            g.weight = next(weights)

suppresses the spurious report. If I also remove:

        if not res.success:
            raise OptimizationError(res)

the report is now for res, which is all right since res is not used anymore.

I think the problem has to do with the assignment to weights in weights = iter(res.x). Using weights = res.x or assigning to a new variable like weights_ = iter(res.x) both remove the warning. It's really weird.

@memeplex
Copy link
Author

Here is a reproducible example:

import numpy as np
import scipy as sp


def f():
    weights = np.ones(10)
    hess = np.eye(10)
    res = sp.optimize.minimize(
        lambda w: 0.5 * ((w - weights) ** 2).sum(),
        weights,
        jac=lambda w: w - weights,
        hess=lambda w: hess,
    )
    weights = iter(res.x)
    return weights

@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Mar 27, 2021
@github-actions github-actions bot removed the triage label Mar 27, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.3.4, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202134-31-march-2021

@memeplex
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants