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

Importing an entire google.cloud namespace package yields Unknown type, while specific imports are correctly typed #4507

Closed
squarebridges opened this issue Jan 22, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@squarebridges
Copy link

Describe the bug

Hey there, it's the guy with all the Google Cloud use cases.

In this case, my issue is that when importing a package from the google.cloud namespace, it is typed as Unknown. If I then import a specific object from the same package, the specific object is appropriately typed.

To Reproduce

Environment:

~> mkdir pyright-issue && cd pyright-issue
~> python -m venv .venv && source .venv/bin/activate && pip install pyright google-cloud-compute

successful.py:

from google.cloud.compute import Instance

reveal_type(Instance().advanced_machine_features.threads_per_core)

failing.py:

from google.cloud import compute

reveal_type(compute.Instance().advanced_machine_features.threads_per_core)

Output of pyright success.py:

No configuration file found.
No pyproject.toml file found.
stubPath /home/nash/pyright-issue/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.290
/home/nash/pyright-issue/success.py
  /home/nash/pyright-issue/success.py:3:13 - information: Type of "Instance().advanced_machine_features.threads_per_core" is "int"
0 errors, 0 warnings, 1 information 
Completed in 1.051sec

Output of pyright failing.py:

No configuration file found.
No pyproject.toml file found.
stubPath /home/nash/pyright-issue/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.290
/home/nash/pyright-issue/failing.py
  /home/nash/pyright-issue/failing.py:3:13 - information: Type of "compute.Instance().advanced_machine_features.threads_per_core" is "Unknown"
0 errors, 0 warnings, 1 information 
Completed in 0.614sec

Output of pyright success.py failing.py:

No configuration file found.
No pyproject.toml file found.
stubPath /home/nash/pyright-issue/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 2 source files
pyright 1.1.290
/home/nash/pyright-issue/success.py
  /home/nash/pyright-issue/success.py:3:13 - information: Type of "Instance().advanced_machine_features.threads_per_core" is "int"
/home/nash/pyright-issue/failing.py
  /home/nash/pyright-issue/failing.py:3:13 - information: Type of "compute.Instance().advanced_machine_features.threads_per_core" is "int"
0 errors, 0 warnings, 2 informations 
Completed in 1.035sec

It seems that any import in the entire Pyright process that accesses the specific object "unlocks" the type for the whole process.

Expected behavior

Whether importing the package as a whole, or particular objects from submodules, types should be built in the same way.

erictraut pushed a commit that referenced this issue Jan 22, 2023
…within a py.typed module that is contained with a non-py.typed namespace package. This addresses #4507.
@erictraut
Copy link
Collaborator

Thanks, this will be fixed in the next release.

The issue occurred in the specific case where a namespace package (in this case google.cloud) was not marked "py.typed" but one of its submodules (in this case, compute) was marked "py.typed".

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Jan 22, 2023
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.291, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants