-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] List available members for a given type #18251
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
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
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,53 @@ | ||
| # Static binary operations using `in` | ||
|
|
||
| ## Basic functionality | ||
|
|
||
| This demonstrates type inference support for `<str-literal> in <tuple>`: | ||
|
|
||
| ```py | ||
| from ty_extensions import static_assert | ||
|
|
||
| static_assert("foo" in ("quux", "foo", "baz")) | ||
| static_assert("foo" not in ("quux", "bar", "baz")) | ||
| ``` | ||
|
|
||
| ## With variables | ||
|
|
||
| ```py | ||
| from ty_extensions import static_assert | ||
|
|
||
| x = ("quux", "foo", "baz") | ||
| static_assert("foo" in x) | ||
|
|
||
| x = ("quux", "bar", "baz") | ||
| static_assert("foo" not in x) | ||
| ``` | ||
|
|
||
| ## Statically unknown results in a type error | ||
|
|
||
| ```py | ||
| from ty_extensions import static_assert | ||
|
|
||
| def _(a: str, b: str): | ||
| static_assert("foo" in (a, b)) # error: [static-assert-error] | ||
| ``` | ||
|
|
||
| ## Values being unknown doesn't mean the result is unknown | ||
|
|
||
| For example, when the types are completely disjoint: | ||
|
|
||
| ```py | ||
| from ty_extensions import static_assert | ||
|
|
||
| def _(a: int, b: int): | ||
| static_assert("foo" not in (a, b)) | ||
| ``` | ||
|
|
||
| ## Failure cases | ||
|
|
||
| ```py | ||
| from ty_extensions import static_assert | ||
|
|
||
| # We don't support byte strings. | ||
| static_assert(b"foo" not in (b"quux", b"foo", b"baz")) # error: [static-assert-error] | ||
| ``` | ||
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.
I'm not sure what the "type error" being referred to here is -- the only error I see in this section is
static-assert-error? I would expect this header to say something more like "statically unknown returns bool"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, I was calling
static-assert-errora type error, which I guess is not quite right? Can you say more about "statically unknown returns bool"? ty is still returning an error here whenx in yis both unknown and used in a static context. What about, "Statically unknown results in error in static context" instead?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.
It seems like you're more commenting on the behavior of
static_asserthere -- "static context" isn't really a thing otherwise.static_assertis really just a testing tool for us to easily make assertions about things being statically known. It asserts that some expression evaluates toLiteral[True]and emitsstatic-assert-errorotherwise. But (outside of a different mdtest specifically for that purpose) I don't think mdtests should be commenting on the behavior ofstatic_assert; the focus should be on the type system behavior under test (which in this case is<literal-str> in <tuple>). And the relevant behavior being tested here is that we infer theinexpression asbool(not asLiteral[True]orLiteral[False]) if we can't statically determine the result of theintest.On second look I think what this really underscores is that
static_assertis just the wrong tool for this particular test; it would be clearer here to simply usereveal_typeand assert that the revealed type of the expression isbool.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.
Got it! Thank you for talking me through this. I really appreciate it!
I put up a new PR with these fixes: #18388