Skip to content

Add type annotations to ProductPipeline in build-script #82780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The ultimate tool for building Swift.
"""


from argparse import Namespace
import json
import os
import platform
Expand Down Expand Up @@ -187,7 +188,7 @@ def tar(source, destination):
# -----------------------------------------------------------------------------
# Argument Validation

def validate_arguments(toolchain, args):
def validate_arguments(toolchain, args: Namespace):
if toolchain.cc is None or toolchain.cxx is None:
fatal_error(
"can't find clang (please install clang-3.5 or a "
Expand Down
3 changes: 2 additions & 1 deletion utils/build_swift/build_swift/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""


from argparse import ArgumentParser
import itertools
import subprocess

Expand Down Expand Up @@ -97,7 +98,7 @@ def _process_disambiguation_arguments(args, unknown_args):
return args, unknown_args


def parse_args(parser, args, namespace=None):
def parse_args(parser: ArgumentParser, args, namespace=None):
"""Parses a given argument list with the given argparse.ArgumentParser.

Return a processed arguments object. Any unknown arguments are stored in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import platform

from typing import Optional
from .products.product import Product
from swift_build_support.swift_build_support import build_graph


Expand All @@ -23,14 +25,14 @@ class ProductPipeline(object):

This class is meant to just be state.
"""
def __init__(self, should_run_epilogue_operations, identity, is_impl):
def __init__(self, should_run_epilogue_operations: bool, identity: int, is_impl: bool):
assert isinstance(identity, int)
self.identity = identity
self.products = []
self.products: list[tuple[type[Product], bool]] = []
self.is_impl = is_impl
self.should_run_epilogue_operations = should_run_epilogue_operations

def append(self, product, is_enabled):
def append(self, product: type[Product], is_enabled: bool):
self.products.append((product, is_enabled))

def finalize(self):
Expand All @@ -41,7 +43,7 @@ def finalize(self):
def __iter__(self):
return iter(self.products)

def __getitem__(self, i):
def __getitem__(self, i) -> tuple[type[Product], bool]:
return self.products[i]

def __len__(self):
Expand All @@ -65,9 +67,9 @@ class ProductPipelineListBuilder(object):
def __init__(self, args):
self.args = args
self.current_count = 0
self.current_pipeline = None
self.current_pipeline: Optional[ProductPipeline] = None
self.is_current_pipeline_impl = False
self.pipeline_list = []
self.pipeline_list: list[ProductPipeline] = []

def begin_pipeline(self):
if self.current_pipeline is not None:
Expand All @@ -93,14 +95,14 @@ def reset(self):
self.is_current_pipeline_impl = False
self.pipeline_list = []

def add_product(self, product_cls, is_enabled):
def add_product(self, product_cls: type[Product], is_enabled: bool):
"""Add a non-impl product to the current pipeline begin constructed"""
assert self.current_pipeline is not None
assert not self.is_current_pipeline_impl
assert not product_cls.is_build_script_impl_product()
self.current_pipeline.append(product_cls, is_enabled)

def add_impl_product(self, product_cls, is_enabled):
def add_impl_product(self, product_cls: type[Product], is_enabled: bool):
"""Add an impl product to the current pipeline begin constructed"""
assert self.current_pipeline is not None
assert self.is_current_pipeline_impl
Expand All @@ -109,14 +111,16 @@ def add_impl_product(self, product_cls, is_enabled):

def infer(self):
products_to_generation_index = {}
enabled_products = set()
inferred_pipeline_list = []
enabled_products: set[type[Product]] = set()
inferred_pipeline_list: list[list[Optional[type[Product]]]] = []
last_impl_pipeline_index = None

for i in range(len(self.pipeline_list)):
pipeline = self.pipeline_list[i]
pipeline: ProductPipeline = self.pipeline_list[i]
if pipeline.is_impl:
last_impl_pipeline_index = i
final_pipeline = []

final_pipeline: list[Optional[type[Product]]] = []
for pipeline_i in range(len(pipeline)):
(p, is_enabled) = pipeline[pipeline_i]
# Make sure p has not been added multiple times to the builder.
Expand All @@ -137,11 +141,11 @@ def infer(self):
p.get_dependencies()))

for i in range(len(inferred_pipeline_list)):
pipeline = inferred_pipeline_list[i]
inferred_pipeline = inferred_pipeline_list[i]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

renaming pipeline here as otherwise we essentially rebound this variable with values of different type, which I don't think plays well with type annotations


# Filter out any of the pipelines that before inference were not
# selected.
enabled_pipeline = [p for p in pipeline if p is not None]
enabled_pipeline: list = [p for p in inferred_pipeline if p is not None]

if self.args.verbose_build:
print("-- Build Graph Inference --")
Expand Down Expand Up @@ -170,18 +174,18 @@ def infer(self):
inferred_pipeline_list[gen_offset][index] = p

filtered_results = []
for pipeline in inferred_pipeline_list:
filtered_results.append([p for p in pipeline if p is not None])
for inferred_pipeline in inferred_pipeline_list:
filtered_results.append([p for p in inferred_pipeline if p is not None])

if self.args.verbose_build:
print("Final Build Order:")
for pipeline in filtered_results:
for p in pipeline:
for filtered_pipeline in filtered_results:
for p in filtered_pipeline:
print(" {}".format(p.product_name()))

return (filtered_results, last_impl_pipeline_index)

def finalize(self, shouldInfer):
def finalize(self, shouldInfer: bool):
"""Produce a final schedule and return a list of our product pipelines. Resets
the builder when done so is a consuming operation.
"""
Expand Down
4 changes: 2 additions & 2 deletions utils/swift_build_support/swift_build_support/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
import time


from typing import NoReturn
from build_swift.build_swift.constants import SWIFT_BUILD_ROOT


Expand All @@ -30,7 +30,7 @@ def fatal_error(message, stream=sys.stderr):
sys.exit(1)


def exit_rejecting_arguments(message, parser=None):
def exit_rejecting_arguments(message, parser=None) -> NoReturn:
print(message, file=sys.stderr)
if parser:
parser.print_usage(sys.stderr)
Expand Down