Skip to content

Strict null checking part 2 - #15957

Draft
dcbaker wants to merge 24 commits into
mesonbuild:masterfrom
dcbaker:submit/strict-null-checking-part2
Draft

Strict null checking part 2#15957
dcbaker wants to merge 24 commits into
mesonbuild:masterfrom
dcbaker:submit/strict-null-checking-part2

Conversation

@dcbaker

@dcbaker dcbaker commented Jun 25, 2026

Copy link
Copy Markdown
Member

Here's a second set of strict typing fixes, This is a small subset of the work I have toward this, but this bit seemed like already a lot of commits, and is pretty self contained.

dcbaker added 24 commits June 22, 2026 15:27
This was purely a bad annotation situation.
Which could return None via a default value, but the annotation (and the
uses) don't allow for it.
This value isn't actually null if `yielding` is true.
…u_family

This makes the typing system safer.
It's never not passed, so making it optional just complicates the
typing.
We need to use `.get(arg) or ''`, since the value can itself be `None`.
Since we have methods in BuildTarget that set it.
Which has the correct type internally, but the return type is missing an
optional annotation
This fixes a strict null where self.name is None for a short time.
Int he case of rename we can use an InitVar to ensure that `self.rename`
is never None. For data_type we just annotate that it can be `str |
None`.
It's passed into a function that requires the backend, and doesn't
accept None. So we need to get it in every case.
@dcbaker dcbaker added the typing label Jun 25, 2026
@dcbaker
dcbaker requested a review from jpakkane as a code owner June 25, 2026 16:47
@dcbaker
dcbaker marked this pull request as draft June 25, 2026 16:55
Comment thread mesonbuild/build.py
else:
self.extra_depends.append(t)
else:
path = self.generator.exe.get_path()

@bonzini bonzini Jun 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this be changed by swapping the if and the else like

if (path := self.generator.exe.get_path()) is not None:
    ...
else:
    assert isinstance(self.generator.exe, LocalProgram)

?

Comment thread mesonbuild/mparser.py

not_token.bytespan = (not_token.bytespan[0], in_token.bytespan[1])
not_token.value += temp_node.whitespaces.value + in_token.value
if temp_node.whitespaces is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is None cannot happen here because this is the whitespace between not and in.

Comment thread mesonbuild/build.py
d = d.get_target()
elif isinstance(d, programs.Program):
path = d.get_path()
path = unwrap(d.get_path())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As elsewhere, the three cases are:

  • not found (path is None)
  • LocalProgram (currently, either path is not None or exception)
  • ExternalProgram (path is not None)

If LocalProgram can be changed to return None instead of raising an exception, then get_path() can be used first and followed by an assert isinstance(d, LocalProgram).

Comment thread mesonbuild/build.py
raise MesonBugException('Cannot call get_command() on program that is a build target.')

def get_path(self) -> str:
def get_path(self) -> str | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This currently can never happen - a LocalProgram can only wrap an ExternalProgram that was found, and therefore whose path is not None. But this change is okay if the raise below is replaced by return None as detailed elsewhere.

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