-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] Implement partial stubs #19931
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
[ty] Implement partial stubs #19931
Conversation
(I need to write some proper tests but this fixes |
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
@@ -796,7 +809,7 @@ fn resolve_name_in_search_path( | |||
context: &ResolverContext, | |||
name: &RelaxedModuleName, | |||
search_path: &SearchPath, | |||
) -> Result<(PackageKind, ResolvedName), PackageKind> { | |||
) -> Result<(PackageKind, PyTyped, ResolvedName), (PackageKind, PyTyped)> { |
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.
This is really gross and I considered factoring a proper type but literally only one function deals with this and immediately unpacks these so... eh?
/// This implementation implies that once a `py.typed` is specified | ||
/// all child packages inherit it, so they can never become Untyped. | ||
/// However they can override whether that's Full or Partial by | ||
/// redeclaring a `py.typed` file of their own. | ||
fn inherit_parent(self, parent: Self) -> Self { |
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.
No idea if this is correct but i like it conceptually
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.
The spec isn't totally clear here, it just says "This marker applies recursively: if a top-level package includes it, all its sub-packages MUST support type checking as well." It doesn't say anything about a sub-package going to partial
where its parent was "full", or whatever. But I think this behavior is sensible.
|
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.
Looks reasonable to me! But yeah, should add some tests before we land it.
/// This implementation implies that once a `py.typed` is specified | ||
/// all child packages inherit it, so they can never become Untyped. | ||
/// However they can override whether that's Full or Partial by | ||
/// redeclaring a `py.typed` file of their own. | ||
fn inherit_parent(self, parent: Self) -> Self { |
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.
The spec isn't totally clear here, it just says "This marker applies recursively: if a top-level package includes it, all its sub-packages MUST support type checking as well." It doesn't say anything about a sub-package going to partial
where its parent was "full", or whatever. But I think this behavior is sensible.
Added tests |
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.
Nice. This looks good to me.
Can you coordinate with @BurntSushi to make sure partial is also considered for completions (#19883)?
It would be nice if some of those module resolver tests could be mdtests. I ported some module resolver tests but haven't had the time to migrate all, but it would be nice if we can avoid adding new once (unless it uses a mdtest feature that we aren't supporting yet)
/// Testing stub packages can be missing modules and we don't fallthrough | ||
/// to the impl (when they don't declare py.typed partial) | ||
#[test] | ||
fn resolve_stub_module_missing_non_partial() { |
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.
Could this be an mdtest similar to
## 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 | |
``` |
I added astral-sh/ty#1042 to track this. |
Oh sure I can make these mdtests. |
Fixes astral-sh/ty#184