Skip to content

tc-build: Add typing annotations and enable ty#326

Merged
msfjarvis merged 21 commits intoClangBuiltLinux:mainfrom
nathanchance:ty
Apr 9, 2026
Merged

tc-build: Add typing annotations and enable ty#326
msfjarvis merged 21 commits intoClangBuiltLinux:mainfrom
nathanchance:ty

Conversation

@nathanchance
Copy link
Copy Markdown
Member

This pull request adds typing annotations throughout tc-build and enables checking them with ty. See the individual changes for the full details.

As pointed out by ty:

  error[unresolved-attribute]: Unresolved attribute `tarball_remote_checksum_name` on type `BinutilsSourceManager`
     --> build-binutils.py:101:5
      |
   99 |     bsm.tarball.base_download_url = 'https://sourceware.org/pub/binutils/releases'
  100 |     bsm.tarball.local_location = bsm.location.with_name(f"{folder_name}.tar.xz")
  101 |     bsm.tarball_remote_checksum_name = 'sha512.sum'
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  102 |     bsm.prepare()

Fixes: 8bc3594 ("tc-build: Modular rewrite")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Clears up the following ty warning:

  error[invalid-assignment]: Invalid subscript assignment with key of type `Literal["loongarch64"]` and value of type `<class 'LoongArchBinutilsBuilder'>` on object of type `dict[str, <class 'ArmBinutilsBuilder'> | <class 'AArch64BinutilsBuilder'> | <class 'MipsBinutilsBuilder'> | ... omitted 7 union elements]`
     --> build-binutils.py:128:5
      |
  126 | }
  127 | if 'loongarch64' in default_targets:
  128 |     targets_to_builder['loongarch64'] = tc_build.binutils.LoongArchBinutilsBuilder
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------------
      |                                         |
      |                                         Expected value of type `<class 'ArmBinutilsBuilder'> | <class 'AArch64BinutilsBuilder'> | <class 'MipsBinutilsBuilder'> | ... omitted 7 union elements`, got `<class 'LoongArchBinutilsBuilder'>`
  129 | for item in targets:
  130 |     target = item.split('-', maxsplit=1)[0]
      |

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Clears up several warnings from ty along the lines of

  error[invalid-argument-type]: Argument to bound method `add_argument` is incorrect
    --> build-llvm.py:50:5
     |
  49 |                     '''),
  50 |     **BOOL_ARGS,
     |     ^^^^^^^^^^^ Expected `str | type[Action]`, found `<class 'BooleanOptionalAction'> | bool | str`
  51 | )
  52 | parser.add_argument(
     |
  info: Element `bool` of this union is not assignable to `str | type[Action]`
  info: Method defined here
     --> stdlib/argparse.pyi:150:9
      |
  148 |     def set_defaults(self, **kwargs: Any) -> None: ...
  149 |     def get_default(self, dest: str) -> Any: ...
  150 |     def add_argument(
      |         ^^^^^^^^^^^^
  151 |         self,
  152 |         *name_or_flags: str,
  153 |         # str covers predefined actions ("store_true", "count", etc.)
  154 |         # and user registered actions via the `register` method.
  155 |         action: str | type[Action] = ...,
      |         -------------------------------- Parameter declared here
  156 |         # more precisely, Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="],
  157 |         # but using this would make it hard to annotate callers that don't use a

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
While it is a little shorter, ty complains:

  error[no-matching-overload]: No overload of bound method `join` matches arguments
     --> build-llvm.py:546:31
      |
  544 | …     if (linux_version := lsm.get_version()) < KernelBuilder.MINIMUM_SUPPORTED_VERSION:
  545 | …         found_version = '.'.join(map(str, linux_version))
  546 | …         minimum_version = '.'.join(map(str, KernelBuilder.MINIMUM_SUPPORTED_VERSION))
      |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  547 | …         raise RuntimeError(
  548 | …             f"Supplied kernel source version ('{found_version}') is older than the minimum required version ('{minimum_version}'), …
      |
  info: First overload defined here
      --> stdlib/builtins.pyi:1109:9
       |
  1108 |     @overload
  1109 |     def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString:
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1110 |         """Concatenate any number of strings.
       |
  info: Possible overloads for bound method `join`:
  info:   (self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString
  info:   (self, iterable: Iterable[str], /) -> str
  info: rule `no-matching-overload` is enabled by default

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
While this is nice for the truthiness tests, it causes ty warnings
without a check before every single usage:

  error[invalid-argument-type]: Argument to function `__new__` is incorrect
     --> tc_build/llvm.py:142:36
      |
  140 |             # Redirect the output to a log file in case it ever needs to be
  141 |             # inspected.
  142 |             merge_fdata_log = Path(self.folders.build, 'merge-fdata.log')
      |                                    ^^^^^^^^^^^^^^^^^^ Expected `str | PathLike[str]`, found `Unknown | None`
  143 |
  144 |             with (
      |
  info: Element `None` of this union is not assignable to `str | PathLike[str]`

Add a Path variable with /uninitialized as its value, which is used as
the default value and checked against before use.

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
…ocess.run()

To help annotate wrapper functions and address ty warnings such as:

  error[unsupported-operator]: Unsupported `+=` operation
     --> tc_build/source.py:116:9
      |
  114 |             if ref != 'main':
  115 |                 git_clone.append('--no-single-branch')
  116 |         git_clone += [self._repo_url, self.repo]
      |         ---------^^^^---------------------------
      |         |            |
      |         |            Has type `list[Unknown | str | Path]`
      |         Has type `list[str]`
  117 |
  118 |         subprocess.run(git_clone, check=True)

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Clears up the following ty warning:

  error[invalid-assignment]: Invalid subscript assignment with key of type `Literal["LoongArch"]` and value of type `<class 'LoongArchKernelBuilder'>` on object of type `dict[str, <class 'Arm64KernelBuilder'> | <class 'ArmKernelBuilder'> | <class 'HexagonKernelBuilder'> | ... omitted 4 union elements]`
     --> tc_build/kernel.py:411:13
      |
  409 |         # the compiler).
  410 |         if lsm.get_version() >= (6, 6, 8):
  411 |             allconfig_capable_builders['LoongArch'] = LoongArchKernelBuilder
      |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------
      |                                                       |
      |                                                       Expected value of type `<class 'Arm64KernelBuilder'> | <class 'ArmKernelBuilder'> | <class 'HexagonKernelBuilder'> | ... omitted 4 union elements`, got `<class 'LoongArchKernelBuilder'>`
  412 |
  413 |         # This is a little convoluted :/

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Solves ty warnings along the lines of:

  error[invalid-assignment]: Invalid subscript assignment with key of type `Literal["CMAKE_AR"]` and value of type `Path` on object of type `dict[str, str]`
     --> tc_build/llvm.py:301:13
      |
  300 |         if tc_build.utils.path_is_set(self.tools.ar):
  301 |             self.cmake_defines['CMAKE_AR'] = self.tools.ar
      |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------
      |                                              |
      |                                              Expected value of type `str`, got `Path`
  302 |         if tc_build.utils.path_is_set(self.tools.ranlib):
  303 |             self.cmake_defines['CMAKE_RANLIB'] = self.tools.ranlib

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Resolves ty warning:

  error[unsupported-operator]: Unsupported `+` operation
     --> tc_build/kernel.py:287:41
      |
  285 |         # ClangBuiltLinux/linux#1260
  286 |         if self.get_toolchain_version() < (12, 0, 0):
  287 |             self.make_variables['LD'] = self.cross_compile + 'ld'
      |                                         ------------------^^^----
      |                                         |                    |
      |                                         |                    Has type `Literal["ld"]`
      |                                         Has type `Unknown | Literal["powerpc64le-linux-gnu-", "powerpc64-linux-gnu-"] | None`
  288 |
  289 |         super().build()

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Clears up several ty warnings along the lines of:

  error[invalid-assignment]: Object of type `Literal[True]` is not assignable to attribute `bolt_instrumentation` on type `Unknown | None`
     --> tc_build/llvm.py:180:17
      |
  178 |                 # This option changes CC when building Linux, which is only
  179 |                 # needed when just clang is instrumented.
  180 |                 self.bolt_builder.bolt_instrumentation = True
      |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  181 |
  182 |         if mode == 'sampling':

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
As pointed out by ty:

  error[unresolved-attribute]: Object of type `Self@download` has no attribute `local_destination`
    --> tc_build/source.py:66:45
     |
  64 | …     if computed_checksum != expected_checksum:
  65 | …         raise RuntimeError(
  66 | …             f"Computed checksum of {self.local_destination} ('{computed_checksum}') differs from expected checksum ('{expected_check…
     |                                       ^^^^^^^^^^^^^^^^^^^^^^
  67 | …         )

Fixes: 8bc3594 ("tc-build: Modular rewrite")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
@nathanchance nathanchance requested a review from msfjarvis as a code owner April 9, 2026 18:48
@msfjarvis msfjarvis merged commit 0f2c7f4 into ClangBuiltLinux:main Apr 9, 2026
8 checks passed
@msfjarvis msfjarvis deleted the ty branch April 9, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants