Skip to content

[red-knot] Unify setup_db() functions, add TestDb builder#14777

Merged
sharkdp merged 3 commits intomainfrom
david/test-db-builder
Dec 4, 2024
Merged

[red-knot] Unify setup_db() functions, add TestDb builder#14777
sharkdp merged 3 commits intomainfrom
david/test-db-builder

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Dec 4, 2024

Summary

  • Instead of seven (more or less similar) setup_db functions, use just one in a single central place.
  • For every test that needs customization beyond that, offer a TestDbBuilder that can control the Python target version, custom typeshed, and pre-existing files.

The main motivation for this is that we're soon going to need customization of the Python version, and I didn't feel like adding this to each of the existing setup_db functions.

It feels like there is much more we can do, especially around writing files, but let's first see if everyone is happy with this first step.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Dec 4, 2024
Comment on lines +3820 to +3822
let db = TestDbBuilder::new()
.with_python_version(PythonVersion::PY313)
.build()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only functional change, without any consequences. But it feels okay, since typing.NoDefault is only available since 3.13). I will need this — and similar things — soon in the statically-know branch, so I'm putting it into place already.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think once we have the statically-known branch, we will probably want to add more tests around some of these "exists in typing in recent versions and in typing_extensions as fallback" known symbols, in terms of how they behave on older and newer Pythons. But this change seems fine for this PR and this particular test.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Thank you! Much nicer. This always bugged me slightly but never enough to fix it :)

Comment on lines +3820 to +3822
let db = TestDbBuilder::new()
.with_python_version(PythonVersion::PY313)
.build()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think once we have the statically-known branch, we will probably want to add more tests around some of these "exists in typing in recent versions and in typing_extensions as fallback" known symbols, in terms of how they behave on older and newer Pythons. But this change seems fine for this PR and this particular test.

#[test]
fn typing_vs_typeshed_no_default() {
let db = setup_db();
fn typing_vs_typeshed_no_default() -> anyhow::Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

@AlexWaygood previously expressed a preference for using unwrap() in tests, rather than having tests return a Result and then using ? in the test. We have some existing tests using both styles. I don't have strong feelings, but I think I shared Alex's preference after writing tests in both styles; the need to end the test with Ok(()) is irritating.

Do you have a preference in the other direction?

Copy link
Contributor Author

@sharkdp sharkdp Dec 4, 2024

Choose a reason for hiding this comment

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

Do you have a preference in the other direction?

I do not. I usually also use .unwrap() liberally in tests. Sometimes the code looks a lot nicer if there are many .unwrap()s that can be replaced with ?, so I guess it's okay to not have a strict guideline(?).

For TestDbBuilder specifically, I first wrote a version that didn't return a Result. I then changed it because I thought there might be more complex test setups in the future where it would be useful for the developer to see a good error message if they did something wrong in the setup of the test.

And note that setup_db() still returns a plain TestDb, not a Result. It's just the builder that returns a Result.

For the particular example here... nothing can go wrong when just setting the Python version, so I'll change this back to .unwrap() style.

@sharkdp sharkdp merged commit bd27bfa into main Dec 4, 2024
@sharkdp sharkdp deleted the david/test-db-builder branch December 4, 2024 20:36
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.

2 participants