microsoft / pyright

Static Type Checker for Python
Other
13.12k stars 1.4k forks source link

Hover may not work for certain type stub import patterns #8725

Closed ethereon closed 1 month ago

ethereon commented 1 month ago

Consider the following toy typestub module:

strawberry/__init__.pyi:

from .jam import buy # Variant 1
# from .jam import * # Variant 2

strawberry/jam.pyi :

def buy(amount: int) -> str:
    """Buy some delicious strawberry jam"""
    ...

Pyright seems to provide different hover results depending on whether Variant 1 (explicit import by name) or Variant 2 (wildcard import) is used in __init__.pyi.

For instance, requesting hover results at buy in the usage from strawberry import buy:

This only seems to occur with .pyi files. For regular Python modules, both variants seem to work correctly.

Tested with Pyright language server 1.1.375 / recent main (https://github.com/microsoft/pyright/commit/de0386d690a85d844e721032fc517c4403a98984)

erictraut commented 1 month ago

Yes, this is by design and in conformance with the Python typing spec. The rules for .pyi files are defined here. If you use the from .jam import buy, a type checker assumes that buy was imported for local use in __init__.pyi and is not intended to be re-exported. If you want to re-export it, you need to use a redundant import form (from .jam import buy as buy).