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

Pylance doesn't respect __all__ in imports #1277

Closed
stolarczyk opened this issue May 13, 2021 · 9 comments
Closed

Pylance doesn't respect __all__ in imports #1277

stolarczyk opened this issue May 13, 2021 · 9 comments
Labels
needs investigation Could be an issue - needs investigation

Comments

@stolarczyk
Copy link

stolarczyk commented May 13, 2021

The following is a valid setup, but Pylance marks the variables as undefined:

const.py submodule:

FOO = 1
BAR = 2

__all__ = ["FOO", "BAR"]

file.py submodule:

from .const import *

FOO # Pylance marks as not defined
@erictraut
Copy link
Contributor

I'm not able to repro the problem as defined above. Could someone else on the pylance team give it a try as well? Perhaps I'm missing something here.

@bschnurr
Copy link
Member

Wasn't able to repro either. What is your folder structure?

@jakebailey
Copy link
Member

Yeah; I think we need some more info for this. Is this a reproduction on a larger project that we could look at?

@jakebailey jakebailey added the waiting for user response Requires more information from user label May 13, 2021
@github-actions github-actions bot removed the triage label May 13, 2021
@stolarczyk
Copy link
Author

thank you all for the prompt replies. Can't explain that, but either restarting VSCode or reinstalling the plugin (I did both) helped.

@stolarczyk
Copy link
Author

stolarczyk commented May 19, 2021

I take it back, I experienced this again. I have constructed a minimal example this time: example_python_package.tar.gz

That's what I see in vscode:

image

But consts imports seem to work as I expected:

Python 3.9.5 (default, May  4 2021, 03:36:27) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from packagename import MyClass

In [2]: mc = MyClass()

In [3]: print(mc)
fav: ['FOO', 'BAR']
foo;bar;baz

@stolarczyk stolarczyk reopened this May 19, 2021
@jakebailey jakebailey added needs investigation Could be an issue - needs investigation and removed waiting for user response Requires more information from user labels May 19, 2021
@erictraut
Copy link
Contributor

In your example, you're using an expression form to compute __all__ that is not supported by pylance or other static type checkers.

__all__ = FAV_CONSTS + ["BAZ", "FAV_CONSTS"]

The supported forms that were agreed upon by Python type checkers is documented here.

If you change your code to one of the following, it will work:

__all__ = ["FOO", "BAR", "BAZ", "FAV_CONSTS"]

or

__all__ = ["FOO", "BAR"]
__all__.extend(["BAZ", "FAV_CONSTS"])

@rayzchen
Copy link

rayzchen commented Jul 6, 2021

I made a module called pyunity and in the __init__.py, I have a gigantic slab of code that defines __all__. However, all the submodules in pyunity are .pyd files, so no classes are actually loaded. However, they are defined in __all__, and do exist in the .pyd files, but i guess its just because pylance doesnt load C extensions. How can I make pylance not think that I have missing imports that actually exist and are in __all__?

__all__ in my module:
image

Behaviour is not imported:
image

Behaviour does exist:
image

Steps to reproduce (if needed):
Install wheel from this build job (https://ci.appveyor.com/project/rayzchen/pyunity/builds/39889374)
Paste this code

from pyunity import *

class Rotator(Behaviour):
    def Update(self, dt):
        self.transform.eulerAngles += Vector3(0, 90, 135) * dt

Click Behaviour
image

@jakebailey
Copy link
Member

@rayzchen I think what you're describing is different than the original issue, though similar.

Normally, the solution here would be that the compiled modules themselves need stubs, then we analyze those and the variables are no longer undefined.

I don't think simply including things in __all__ means we declare them, though that could be a thought (if __all__ lists something we can't find, allow it as an Unknown externally), but I'd be more inclined to get the library fully stubbed out.

@jakebailey
Copy link
Member

I'm going to close this, as I believe the original issue has been addressed.

@rayzchen I would open a dedicated issue for that case; i.e. if adding something to __all__ should "declare" it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation Could be an issue - needs investigation
Projects
None yet
Development

No branches or pull requests

5 participants