Skip to content

Comments

Move red_knot_python_semantic::PythonVersion to the ruff_python_ast crate#16147

Merged
ntBre merged 15 commits intomainfrom
brent/version-refactor
Feb 14, 2025
Merged

Move red_knot_python_semantic::PythonVersion to the ruff_python_ast crate#16147
ntBre merged 15 commits intomainfrom
brent/version-refactor

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 13, 2025

Summary

This PR moves the PythonVersion struct from the red_knot_python_semantic crate to the ruff_python_ast crate so that it can be used more easily in the syntax error detection work. Compared to that prototype these changes reduce us from 2 PythonVersion structs to 1.

This does not unify any of the PythonVersion enums, but I hope to make some progress on that in a follow-up.

Test Plan

Existing tests, this should not change any external behavior.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 13, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre added the internal An internal refactor or improvement label Feb 14, 2025
Self {
source_type: PySourceType::default(),
target_version: PythonVersion::default(),
target_version: PyVersion::default(),
Copy link
Member

Choose a reason for hiding this comment

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

I think it shouldn't be much effort to use PythonVersion here.

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.

Thanks.

I'd prefer to keep the API boundary Python version types used in the CLI and wasm because I don't want the parser to depend on clap or wasm bindgen (even optionally). It also allows us to add a new python version only to one tool but not the other.

I'd also prefer not to move PyVersion to the parser. I find that confusing. We should keep this type in the linter crate if migrating the linter is too much effort.

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.

I agree with unifying these enums/structs to some extent, but it does feel a bit odd to me that we'd have the common type in ruff_python_parser, since it's not really something specific to parsing Python code -- it's a general-purpose type that you could use for lots of things. ruff_python_ast might be a better fit -- or what about ruff_python_stdlib? If we put it there, we could use it in functions like this, which "should really" take a PythonVersion as their argument (it would make the signature much more strongly typed):

pub fn is_python_builtin(name: &str, minor_version: u8, is_notebook: bool) -> bool {

I think the disadvantage might be that we'd have to declare an explicit dependency on ruff_python_stdlib from more other crates -- but I doubt that this would be a huge problem in practice, since both Ruff and red-knot already depend on it, and it's a pretty minimal crate that doesn't depend on any other crates in the workspace. Curious for @MichaReiser's thoughts...

@MichaReiser
Copy link
Member

I don't think it should be ruff_python_stdlib because I don't want the parser to depend on that crate. I wouldn't mind the ast crate.

@AlexWaygood
Copy link
Member

ruff_python_ast works for me -- we can leave the ruff_python_stdlib APIs as-is for now.

This reverts commit 9a35763.
@ntBre
Copy link
Contributor Author

ntBre commented Feb 14, 2025

Sounds good, I already reverted everything except moving red_knot_python_semantic::PythonVersion to ruff_python_parser, so I will just move that to ruff_python_ast and then see if the formatter can use PythonVersion. I think I ran into serde issues with that yesterday because the (de)serialization needs to be different from red-knot's usage of PythonVersion, but I'll check again.

Thanks for the reviews!

@ntBre
Copy link
Contributor Author

ntBre commented Feb 14, 2025

PythonVersion is now in the ast crate, going to look at the formatter version now. I'll update the title and description if we just want to leave it here, though.

Co-authored-by: Alex Waygood <[email protected]>
@MichaReiser
Copy link
Member

Moving the type and doing the unification in separate PRs does make sense to me

@ntBre
Copy link
Contributor Author

ntBre commented Feb 14, 2025

Sounds good. I think I just started having some promising serde results, but it makes sense to open a separate PR. I'll push the change Alex recommended and then merge!

@ntBre ntBre changed the title Unify version structs and enums Move red_knot_python_semantic::PythonVersion to the ruff_python_ast crate Feb 14, 2025
@ntBre ntBre merged commit f58a54f into main Feb 14, 2025
21 checks passed
@ntBre ntBre deleted the brent/version-refactor branch February 14, 2025 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants