-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Support stub packages #17204
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
286 changes: 286 additions & 0 deletions
286
crates/red_knot_python_semantic/resources/mdtest/import/stub_packages.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| # Stub packages | ||
|
|
||
| Stub packages are packages named `<package>-stubs` that provide typing stubs for `<package>`. See | ||
| [specification](https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages). | ||
|
|
||
| ## Simple stub | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/packages"] | ||
| ``` | ||
|
|
||
| `/packages/foo-stubs/__init__.pyi`: | ||
|
|
||
| ```pyi | ||
| class Foo: | ||
| name: str | ||
| age: int | ||
| ``` | ||
|
|
||
| `/packages/foo/__init__.py`: | ||
|
|
||
| ```py | ||
| class Foo: ... | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from foo import Foo | ||
|
|
||
| reveal_type(Foo().name) # revealed: str | ||
| ``` | ||
|
|
||
| ## Stubs only | ||
|
|
||
| The regular package isn't required for type checking. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/packages"] | ||
| ``` | ||
|
|
||
| `/packages/foo-stubs/__init__.pyi`: | ||
|
|
||
| ```pyi | ||
| class Foo: | ||
| name: str | ||
| age: int | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from foo import Foo | ||
|
|
||
| reveal_type(Foo().name) # revealed: str | ||
| ``` | ||
|
|
||
| ## `-stubs` named module | ||
|
|
||
| A module named `<module>-stubs` isn't a stub package. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/packages"] | ||
| ``` | ||
|
|
||
| `/packages/foo-stubs.pyi`: | ||
|
|
||
| ```pyi | ||
| class Foo: | ||
| name: str | ||
| age: int | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from foo import Foo # error: [unresolved-import] | ||
|
|
||
| reveal_type(Foo().name) # revealed: Unknown | ||
| ``` | ||
|
|
||
| ## Namespace package in different search paths | ||
|
|
||
| A namespace package with multiple stub packages spread over multiple search paths. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/stubs1", "/stubs2", "/packages"] | ||
| ``` | ||
|
|
||
| `/stubs1/shapes-stubs/polygons/pentagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Pentagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/stubs2/shapes-stubs/polygons/hexagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Hexagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/pentagon.py`: | ||
|
|
||
| ```py | ||
| class Pentagon: ... | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/hexagon.py`: | ||
|
|
||
| ```py | ||
| class Hexagon: ... | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from shapes.polygons.hexagon import Hexagon | ||
| from shapes.polygons.pentagon import Pentagon | ||
|
|
||
| reveal_type(Pentagon().sides) # revealed: int | ||
| reveal_type(Hexagon().area) # revealed: int | float | ||
| ``` | ||
|
|
||
| ## Inconsistent stub packages | ||
|
|
||
| Stub packages where one is a namespace package and the other is a regular package. Module resolution | ||
| should stop after the first non-namespace stub package. This matches Pyright's behavior. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/stubs1", "/stubs2", "/packages"] | ||
| ``` | ||
|
|
||
| `/stubs1/shapes-stubs/__init__.pyi`: | ||
|
|
||
| ```pyi | ||
| ``` | ||
|
|
||
| `/stubs1/shapes-stubs/polygons/__init__.pyi`: | ||
|
|
||
| ```pyi | ||
| ``` | ||
|
|
||
| `/stubs1/shapes-stubs/polygons/pentagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Pentagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/stubs2/shapes-stubs/polygons/hexagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Hexagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/pentagon.py`: | ||
|
|
||
| ```py | ||
| class Pentagon: ... | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/hexagon.py`: | ||
|
|
||
| ```py | ||
| class Hexagon: ... | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from shapes.polygons.pentagon import Pentagon | ||
| from shapes.polygons.hexagon import Hexagon # error: [unresolved-import] | ||
|
|
||
| reveal_type(Pentagon().sides) # revealed: int | ||
| reveal_type(Hexagon().area) # revealed: Unknown | ||
| ``` | ||
|
|
||
| ## Namespace stubs for non-namespace package | ||
|
|
||
| The runtime package is a regular package but the stubs are namespace packages. Pyright skips the | ||
| stub package if the "regular" package isn't a namespace package. I'm not aware that the behavior | ||
| here is specified, and using the stubs without probing the runtime package first requires slightly | ||
| fewer lookups. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/packages"] | ||
| ``` | ||
|
|
||
| `/packages/shapes-stubs/polygons/pentagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Pentagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/packages/shapes-stubs/polygons/hexagon.pyi`: | ||
|
|
||
| ```pyi | ||
| class Hexagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/packages/shapes/__init__.py`: | ||
|
|
||
| ```py | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/__init__.py`: | ||
|
|
||
| ```py | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/pentagon.py`: | ||
|
|
||
| ```py | ||
| class Pentagon: ... | ||
| ``` | ||
|
|
||
| `/packages/shapes/polygons/hexagon.py`: | ||
|
|
||
| ```py | ||
| class Hexagon: ... | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from shapes.polygons.pentagon import Pentagon | ||
| from shapes.polygons.hexagon import Hexagon | ||
|
|
||
| reveal_type(Pentagon().sides) # revealed: int | ||
| reveal_type(Hexagon().area) # revealed: int | float | ||
| ``` | ||
|
|
||
| ## Stub package using `__init__.py` over `.pyi` | ||
|
|
||
| It's recommended that stub packages use `__init__.pyi` files over `__init__.py` but it doesn't seem | ||
| to be an enforced convention. At least, Pyright is fine with the following. | ||
|
|
||
| ```toml | ||
| [environment] | ||
| extra-paths = ["/packages"] | ||
| ``` | ||
|
|
||
| `/packages/shapes-stubs/__init__.py`: | ||
|
|
||
| ```py | ||
| class Pentagon: | ||
| sides: int | ||
| area: float | ||
|
|
||
| class Hexagon: | ||
| sides: int | ||
| area: float | ||
| ``` | ||
|
|
||
| `/packages/shapes/__init__.py`: | ||
|
|
||
| ```py | ||
| class Pentagon: ... | ||
| class Hexagon: ... | ||
| ``` | ||
|
|
||
| `main.py`: | ||
|
|
||
| ```py | ||
| from shapes import Hexagon, Pentagon | ||
|
|
||
| reveal_type(Pentagon().sides) # revealed: int | ||
| reveal_type(Hexagon().area) # revealed: int | float | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, I have no opinion at all on this 😆 I'd be fine if we didn't support it, but I'm also fine if we do! It seems very unlikely to come up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not supporting it is harder, that's why I went with supporting it :)