Skip to content

Commit

Permalink
utils: Fix nullability of PerMachine from default_missing
Browse files Browse the repository at this point in the history
A Defaultable PerMachine has a type of `None | T`, in other words,
they have a base type of `PerMachine[None | T]`, but the purpose of
`PerMachine.default_missing()` is to get a `PerMachine[T]` from that
`PerMachine[None | T]`, therefore we should ensure that and annotate
that.
  • Loading branch information
dcbaker committed Jan 30, 2025
1 parent c616f1e commit 84c03ea
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,14 @@ class PerMachineDefaultable(PerMachine[T.Optional[_T]]):
def __init__(self, build: T.Optional[_T] = None, host: T.Optional[_T] = None) -> None:
super().__init__(build, host)

def default_missing(self) -> "PerMachine[_T]":
def default_missing(self) -> PerMachine[_T]:
"""Default host to build
This allows just specifying nothing in the native case, and just host in the
cross non-compiler case.
"""
freeze = PerMachine(self.build, self.host)
if freeze.host is None:
freeze.host = freeze.build
return freeze
assert self.build is not None, 'Cannot fill in missing when all fields are empty'
return PerMachine(self.build, self.host if self.host is not None else self.build)

def __repr__(self) -> str:
return f'PerMachineDefaultable({self.build!r}, {self.host!r})'
Expand All @@ -611,19 +609,17 @@ class PerThreeMachineDefaultable(PerMachineDefaultable[T.Optional[_T]], PerThree
def __init__(self) -> None:
PerThreeMachine.__init__(self, None, None, None)

def default_missing(self) -> "PerThreeMachine[T.Optional[_T]]":
def default_missing(self) -> PerThreeMachine[_T]:
"""Default host to build and target to host.
This allows just specifying nothing in the native case, just host in the
cross non-compiler case, and just target in the native-built
cross-compiler case.
"""
freeze = PerThreeMachine(self.build, self.host, self.target)
if freeze.host is None:
freeze.host = freeze.build
if freeze.target is None:
freeze.target = freeze.host
return freeze
assert self.build is not None, 'Cannot default a PerMachine when all values are None'
host = self.host if self.host is not None else self.build
target = self.target if self.target is not None else host
return PerThreeMachine(self.build, host, target)

def __repr__(self) -> str:
return f'PerThreeMachineDefaultable({self.build!r}, {self.host!r}, {self.target!r})'
Expand Down

0 comments on commit 84c03ea

Please sign in to comment.