Skip to content

[red-knot] feat: Inference for BytesLiteral comparisons#13746

Merged
sharkdp merged 2 commits intomainfrom
david/infer-bytes-literal-comparisons
Oct 14, 2024
Merged

[red-knot] feat: Inference for BytesLiteral comparisons#13746
sharkdp merged 2 commits intomainfrom
david/infer-bytes-literal-comparisons

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Oct 14, 2024

Summary

Implements inference for BytesLiteral comparisons along the lines of #13634. A refactoring that unifies some code with what we do for StringLiteral (and possibly other cases, see also #13712) is planned for a later PR.

closes #13687

Test Plan

Added tests using the new Markdown framework. I added the tests in mdtest/comparison/byte_literals.md, following the convention proposed in this comment. This is somewhat in conflict with the proposed structure in #13719 which puts the StringLiteral tests under mdtest/boolean.md, so this will have to be sorted out after we agreed on a naming/grouping convention for these tests.

Implements inference for `BytesLiteral` comparisons along the lines of
#13634.

closes #13687
@sharkdp sharkdp added the ty Multi-file analysis & type inference label Oct 14, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Oct 14, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 14, 2024

ℹ️ ecosystem check detected linter changes. (+0 -1693 violations, +0 -0 fixes in 5 projects; 49 projects unchanged)

Um, you can ignore that. It reports some spurious results sometimes when a PR branch is a bit behind main (we should probably fix it at some point 😄)

There's no way this PR could have caused any of those changes, so it can be safely disregarded here!

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Comment on lines +7 to +8
reveal_type(b"abc" == b"abc") # revealed: Literal[True]
reveal_type(b"abc" == b"ab") # revealed: Literal[False]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work without reveal_type?

Suggested change
reveal_type(b"abc" == b"abc") # revealed: Literal[True]
reveal_type(b"abc" == b"ab") # revealed: Literal[False]
b"abc" == b"abc" # revealed: Literal[True]
b"abc" == b"ab" # revealed: Literal[False]

Is comment aligning part of mdtest's design guide? (is it allowed or preferred?)

Copy link
Member

@AlexWaygood AlexWaygood Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work without reveal_type?

no, the reveal_type call is necessary to trigger a "Revealed" assertion when using mdtest-based tests. This is documented here.

Is comment aligning part of mdtest's design guide? (is it allowed or preferred?)

The comment alignment seems fine to me. At some point maybe we might start running our scripts/check_docs_formatted.py CI script on our mdtest test files. But until we do that, or introduce autoformatting of these Python snippets some other way, I don't think we should be too picky about the formatting of the Python snippets in our test files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably something we have to align on. I recommend to not rely on comment alignment because it's something that will break if we ever decide to format our example code using ruff (which I think would be a nice addition).

@sharkdp sharkdp merged commit 93097f1 into main Oct 14, 2024
@sharkdp sharkdp deleted the david/infer-bytes-literal-comparisons branch October 14, 2024 12:01
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! I've only two nits. Up to you if you want to follow up on them or not.

Comment on lines +2674 to +2675
let b1 = salsa_b1.value(self.db).as_ref();
let b2 = salsa_b2.value(self.db).as_ref();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could have used &**salsa_b1.value(self.db) to aovid the as_ref

}

(Type::BytesLiteral(salsa_b1), Type::BytesLiteral(salsa_b2)) => {
let contains_subsequence = |needle: &[u8], haystack: &[u8]| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks. Did a grep for memmem , didn't find anything, and wasn't sure if it warranted adding a new dependency. Should have looked for memchr.

==> #13750

sharkdp added a commit that referenced this pull request Oct 14, 2024
This is a follow-up on #13746:

- Use `memmem::find` instead of rolling our own inferior version.
- Avoid `x.as_ref()` calls using `&**x`
@carljm
Copy link
Contributor

carljm commented Oct 14, 2024

This is excellent, thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[red-knot] Compare expression inference - BytesLiteral

5 participants