Skip to content

Replace duck typing with protocols for target and CustomTargetIndex types - #15930

Open
bonzini wants to merge 21 commits into
mesonbuild:masterfrom
bonzini:typing-protos
Open

Replace duck typing with protocols for target and CustomTargetIndex types#15930
bonzini wants to merge 21 commits into
mesonbuild:masterfrom
bonzini:typing-protos

Conversation

@bonzini

@bonzini bonzini commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

This PR continues the idea of limiting Union to cases where individual members are categorized with isinstance tests. Instead, cases where Meson mostly uses duck typing between CustomTargetBase and BuildTarget are now handled with protocols. While isinstance tests are occasionally used with types declared as *Proto types, this use is relatively rare, especially in the interpreter->build interface.

Comment thread mesonbuild/backend/backends.py
Comment thread mesonbuild/backend/backends.py Outdated
Comment thread mesonbuild/build.py
Comment thread mesonbuild/dependencies/base.py Outdated
@bonzini
bonzini marked this pull request as ready for review June 16, 2026 13:36
@bonzini
bonzini force-pushed the typing-protos branch 2 times, most recently from 93d177b to 81b1442 Compare June 18, 2026 11:03
@bonzini bonzini added this to the 1.13 milestone Jun 19, 2026
Comment thread mesonbuild/build.py
Comment thread mesonbuild/build.py
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
It can be handled simply like CustomTarget in eval_custom_target_command.  This
means that flatten_command does little more than collecting dependencies now.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
After the previous change, the only change made to the
command is the resolution of LocalPrograms, which in turn
is necessary to properly add their underlying Files to
depend_files or dependencies.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
It cannot include GeneratedList, but it can include other RunTargets.
While not allowed by the front-end, it is allowed by the RunTarget
class and used by modules.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This is called with isinstance checks on CommandTypes, so the right
type is BuildTargetTypes (coming from the Union).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
t.get_outputs()[0] returns the same as get_filename() for both
CustomTarget and CustomTargetIndex.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
__str__ is defined for target types as self.name, but not
for CustomTargetIndex.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
... where CustomTargetIndexes or even CustomTargets are not possible.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This will help with resolving BothLibraries recursively.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Document precisely what is required, and in particular that
AnyTargetProto is not hashable.  The old type union
is not needed anymore.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Document precisely what is required, the old type union
is not needed anymore outside the interpreter.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Replace the abstract class LinkableTarget and the LinkableTargetTypes
union with a protocol.  For the abstract class, in particular, there
are no concrete methods and there is no need for isinstance tests,
so they are interchangeable.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
It is expected by InternalDependency.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Document precisely what's needed, and remove the last two
union types.  Union types should be reserved for "faking" sum types
via isinstance.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Making a CommandTarget superclass out of RunTarget and CustomTarget is not
really possible because RunTarget allows non-BuildTargetProtos in dependencies;
or at least it would do nothing more than a protocol, and in particular it
would not be able to include flatten_command().

So use a protocol instead.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
@bonzini

bonzini commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

I plan to commit this in a couple of weeks, to give more time for review. Updates to the protocols on conflicts have been easy to apply, and never ballooned to the declarations. Furthermore, simplifications in the types (especially the removal of unions) will make it much easier to fix #16159. So I think this concept has more or less proved itself.

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.

2 participants