Skip to content
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

Type safety for the VisualStudio Backend(s) #14219

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

dcbaker
Copy link
Member

@dcbaker dcbaker commented Feb 3, 2025

This gets most of the VS backend type safe, with the exception of some of the object extraction stuff where there seems to be a real potential for things to explode.

This gets all of the VS Backends type safe. Thanks @bonzini for for help with the object stuff.

This turned up a lot of issues in the build module and in the base backend module.

It should be possible to merge pieces of this series incrementally.

It should be noted that this is untested on the vs2010 backend. I expect to find issues at runtime.

@dcbaker dcbaker added the typing label Feb 3, 2025
@dcbaker dcbaker mentioned this pull request Feb 3, 2025
21 tasks
@dcbaker dcbaker force-pushed the submit/vs2010-backend-typing branch 3 times, most recently from 90d76d9 to a9ab54c Compare February 3, 2025 21:19
@bonzini

This comment was marked as resolved.

@dcbaker
Copy link
Member Author

dcbaker commented Feb 4, 2025

@bonzini: the extract_all_objects methods get used all over the place, in the Interpreter, the Ninja Backend, and within build itself. I don't think we can drop those.

@bonzini
Copy link
Collaborator

bonzini commented Feb 4, 2025

They aren't dropped, just unified to CustomTargetBase. The CustomTargetIndex case was wrong in that it returned all objects instead of just the required index; and they had the wrong type because they could return str.

@dcbaker
Copy link
Member Author

dcbaker commented Feb 4, 2025

Ah, I see what you did now. Okay, yeah, plus a little work in the ExtractedObjects class and we can make that work.

@dcbaker dcbaker force-pushed the submit/vs2010-backend-typing branch 2 times, most recently from 62c8924 to 2210da5 Compare February 4, 2025 21:38
@dcbaker dcbaker marked this pull request as ready for review February 4, 2025 21:38
@dcbaker dcbaker requested a review from jpakkane as a code owner February 4, 2025 21:38
Copy link
Member

@bruchar1 bruchar1 left a comment

Choose a reason for hiding this comment

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

I don't see any fancy stuff after the commit "[Fix a lot of low hanging type issues]". Is the description of that commit still relevant?

mesonbuild/build.py Outdated Show resolved Hide resolved
@@ -356,6 +356,7 @@ def get_target_deps(self, t: T.Dict[T.Any, T.Union[build.Target, build.CustomTar
if isinstance(ldep, build.CustomTargetIndex):
all_deps[ldep.get_id()] = ldep.target
elif isinstance(ldep, File):
# XXX: this this true? What if the file `is_built`?
Copy link
Member

Choose a reason for hiding this comment

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

Is there an action to take here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean, probably. But I don't have a windows machine handy to find out what happens if a generated file is used for a link_depends and fix it.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. I will try to look at this (maybe not in a near future, but someday...)

Copy link
Member Author

@dcbaker dcbaker Feb 6, 2025

Choose a reason for hiding this comment

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

I did reduce it from an XXX comment to just a comment. along with fixing the this this :)

This gives us a simpler way to annotate things returning an option
value, and gives us an easy single place to change that will affect
everywhere if the option value types are changed.
Which can return any of `ElementaryOptionValues`, but is currently typed
as if it only returns `str | int | bool`.
Which should be `Target | CustomTargetIndex`, not just `Target`
So that it can meet it's interface requirements.
dcbaker and others added 9 commits February 6, 2025 13:47
Which it needs in the vs2010 backend
Guess what the vs2010 backend can hit?
There are a couple of issues with the current implementation:
1. The API doesn't match the BuildTarget on
2. The CustomTargetIndex version returns all of the CustomTarget's
   objects, not just the one for that index

To fix that the extract_all_objects method is moved to the
CustomTargetBase class, and extended to implement the same API as the
BuildTarget implementaiton.

This also finds a bunch of previously incorrectly typed (and thus
unhandled) cases in the Backend base class around ExtractedObjects from
CustomTargets

Co-Authored-by: Paolo Bonzini <[email protected]>
This means it can be either `str`, `File`, or `File | str`, which gives
us the value of knowing that if the input is T, the output is
Dict[Compiler, List[T]]`
Which may be passed a BuildTarget, CustomTarget, CustomTargetIndex, or
GeneratedList

This requires using `get_subdir()` because `CustomTargetIndex` doesn't
have a `subdir` attribute.
Which can actually be a `PathLike[str] | str`, not just `str`
…le_serialisation

Which should take `Iterable[BuildTarget | CustomTarget | CustomTargetIndex]`, not
just `List[BuildTarget]`.
If this gets to the backend it will cause an uncaught exception, so
let's catch it here and just give a real error.
At least in the vs backend this will get called, and it will fail. So
not only do we need to update the type annotations, but we also need to
fix the implementaiton.
It's really only `BuildTarget | CustomTarget | CustomTargetIndex` that
can be used here, so let's do that correctly.

Also, use only `target.import_filename` since mypy doesn't know about
the getter function.
This is actually an Iterable, since we pass both lists and OrderedSets
in, but also it's not `Target`, it's `BuildTargetTypes`
which needs to take `Iterable[File | str]` not `str`
This is mostly easy, simple stuff to get out of the way before tackling
the harder issues
This is a little more invasive since we're currently using lists where
we should be using tuples. I've added a TypeAlias to make this code
clearer and cleaner.
This gets the vs2010 backend completely mypy compliant
It's almost never what you want
The latter of which creates a new list with references to all of the
objects in the inputs. In our case that's wasted as we just iterate
anyway.
@dcbaker dcbaker force-pushed the submit/vs2010-backend-typing branch from fb95fe7 to c5d0de9 Compare February 6, 2025 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants