Skip to content

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

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

Replace duck typing with protocols for target and CustomTargetIndex types#15930
bonzini wants to merge 8 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
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 added 8 commits June 18, 2026 13:01
... where CustomTargetIndexes or even CustomTargets are not possible.

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>
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>
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>
@bonzini bonzini added this to the 1.13 milestone Jun 19, 2026
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