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

Cannot import packages added using sys.modules.update #3097

Closed
artificial-aidan opened this issue Feb 21, 2022 · 11 comments
Closed

Cannot import packages added using sys.modules.update #3097

artificial-aidan opened this issue Feb 21, 2022 · 11 comments
Labels
as designed Not a bug, working as intended

Comments

@artificial-aidan
Copy link

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

Describe the bug
GRPC imports an aio package using the following code in grpc/__init__.py:

# Prevents import order issue in the case of renamed path.
if sys.version_info >= (3, 6) and __name__ == "grpc":
    from grpc import aio  # pylint: disable=ungrouped-imports
    sys.modules.update({'grpc.aio': aio})

This makes pyright fail to find the package grpc.aio

To Reproduce

Create a python file like so:

import grpc.aio

Expected behavior

Pyright should find the import

@artificial-aidan
Copy link
Author

Not sure if this is related, but they also import packages like this:

try:
    import grpc_reflection
    sys.modules.update({'grpc.reflection': grpc_reflection})
except ImportError:
    pass

And that doesn't work either.

@erictraut
Copy link
Collaborator

Which package is this? I tried pip install grpc, but I don't think that's the package you're talking about here.

@artificial-aidan
Copy link
Author

Sorry, grpcio

@erictraut
Copy link
Collaborator

It looks like grpcio is not a typed library. Do you have type stubs installed for it?

Are you using all default settings for pyright, or do you have a pyrightconfig.json file that overrides some of the defaults? In particular, do you have useLibraryCodeForTypes enabled?

When you say "cannot import packages", what behavior are you seeing? Are you seeing the import interpreted as an Unknown type? Are you receiving an error message? If so, which one?

I tried to repro the problem as reported, and I'm not seeing anything unexpected.

@artificial-aidan
Copy link
Author

I do not have stubs for it installed. Looks like there is some additional work going on to generate stubs for the aio aspect of it. While I'd love to have full type stubs, even just not having to ignore it for now would be great.

I am using it through Pylance and standalone. Get the error with both.

I tried with and without the useLibraryCodeForTypes enabled.

I'm getting the error message: error: Import "grpc.aio" could not be resolved

@artificial-aidan
Copy link
Author

Hmm, I also just realized in a different project I don't have this issue. Let me see if I can sort out why i'm only seeing it in one project.

@artificial-aidan
Copy link
Author

Ok now I'm stumped. I can't reproduce this in a standalone project either. Only in this virtual env. I don't believe this to be a pyright bug, but I will report back here for the next time someone runs into this.

@artificial-aidan
Copy link
Author

Any debug output I should look at? grpc imports fine, but grpc.aio does not.

@erictraut
Copy link
Collaborator

Yeah, I'm pretty sure this isn't a bug in pyright.

You could enable verboseOutput in the pyrightconfig.json file. This will emit additional diagnostics related to import resolution.

@erictraut erictraut added the as designed Not a bug, working as intended label Feb 21, 2022
@artificial-aidan
Copy link
Author

Ok. So I finally sorted out the problem.

I had grpc-stubs installed, which are not compatible with grpc.aio.

If I remove the grpc-stubs library, then I can resolve the import again.

Is there a way to annotate that a package should only provide stubs for part of another package, or some other way to maintain the default type inference.

Basically, pylance gives me some amount of type info when I don't have the stubs installed, but once the stubs are installed it can't find grpc.aio module at all.

@erictraut
Copy link
Collaborator

If you believe this is a bug in the stub package, you could contact the maintainer of that package and report the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants