Skip to content
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

Replace most usages of datatype() with @dataclass #8362

Merged
merged 2 commits into from Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/go/src/python/pants/contrib/go/tasks/BUILD
Expand Up @@ -6,6 +6,7 @@ python_library(
dependencies=[
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:ansicolors',
'3rdparty/python:dataclasses',
'contrib/go/src/python/pants/contrib/go/subsystems',
'contrib/go/src/python/pants/contrib/go/targets',
'src/python/pants/base:build_environment',
Expand Down
10 changes: 5 additions & 5 deletions contrib/go/src/python/pants/contrib/go/tasks/go_test.py
Expand Up @@ -2,12 +2,12 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from contextlib import contextmanager
from dataclasses import dataclass

from pants.base.build_environment import get_buildroot
from pants.base.workunit import WorkUnitLabel
from pants.task.testrunner_task_mixin import PartitionedTestRunnerTaskMixin, TestResult
from pants.util.memo import memoized_property
from pants.util.objects import datatype
from pants.util.process_handler import SubprocessProcessHandler
from pants.util.strutil import create_path_env_var, safe_shlex_join, safe_shlex_split

Expand Down Expand Up @@ -44,10 +44,10 @@ def _test_target_filter(self):
def _validate_target(self, target):
self.ensure_workspace(target)

class _GoTestTargetInfo(datatype([
('import_path', str),
('gopath', str),
])): pass
@dataclass(frozen=True)
class _GoTestTargetInfo:
import_path: str
gopath: str

def _generate_args_for_targets(self, targets):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/BUILD
Expand Up @@ -122,6 +122,7 @@ python_library(
sources = ['bundle_create.py'],
dependencies = [
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:dataclasses',
':classpath_products',
':jvm_binary_task',
'src/python/pants/backend/jvm/targets:jvm',
Expand All @@ -132,7 +133,6 @@ python_library(
'src/python/pants/fs',
'src/python/pants/util:dirutil',
'src/python/pants/util:fileutil',
'src/python/pants/util:objects',
],
)

Expand Down
13 changes: 11 additions & 2 deletions src/python/pants/backend/jvm/tasks/bundle_create.py
Expand Up @@ -2,6 +2,8 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
from dataclasses import dataclass
from typing import Any

from twitter.common.collections import OrderedSet

Expand All @@ -15,7 +17,6 @@
from pants.build_graph.target_scopes import Scopes
from pants.fs import archive
from pants.util.dirutil import safe_mkdir
from pants.util.objects import datatype


class BundleCreate(BundleMixin, JvmBinaryTask):
Expand Down Expand Up @@ -51,8 +52,16 @@ def prepare(cls, options, round_manager):
def product_types(cls):
return ['jvm_archives', 'jvm_bundles', 'deployable_archives']

class App(datatype(['address', 'binary', 'bundles', 'id', 'deployjar', 'archive', 'target'])):
@dataclass(frozen=True)
class App:
"""A uniform interface to an app."""
address: Any
binary: Any
bundles: Any
id: Any
deployjar: Any
archive: Any
target: Any

@staticmethod
def is_app(target):
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/coverage/BUILD
Expand Up @@ -4,6 +4,7 @@
python_library(
dependencies = [
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:dataclasses',
'src/python/pants/backend/jvm/targets:java',
'src/python/pants/backend/jvm/targets:scala',
'src/python/pants/backend/jvm/subsystems:jvm_tool_mixin',
Expand All @@ -13,7 +14,6 @@ python_library(
'src/python/pants/util:contextutil',
'src/python/pants/util:dirutil',
'src/python/pants/util:meta',
'src/python/pants/util:objects',
'src/python/pants/util:strutil',
],
)
8 changes: 6 additions & 2 deletions src/python/pants/backend/jvm/tasks/coverage/engine.py
Expand Up @@ -2,11 +2,12 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any

from pants.backend.jvm.targets.annotation_processor import AnnotationProcessor
from pants.backend.jvm.targets.java_library import JavaLibrary
from pants.backend.jvm.targets.scala_library import ScalaLibrary
from pants.util.objects import datatype


class CoverageEngine(ABC):
Expand All @@ -26,7 +27,8 @@ class CoverageEngine(ABC):
is instantiated and used exactly once per `JUnitRun` task execution.
"""

class RunModifications(datatype(['classpath_prepend', 'extra_jvm_options'])):
@dataclass(frozen=True)
class RunModifications:
"""Modifications that should be made to the java command where code coverage is collected.

The `classpath_prepend` field should be an iterable of classpath elements to prepend to java
Expand All @@ -35,6 +37,8 @@ class RunModifications(datatype(['classpath_prepend', 'extra_jvm_options'])):
The `extra_jvm_options` should be a list of jvm options to include when executing the java
command.
"""
classpath_prepend: Any
extra_jvm_options: Any

@classmethod
def create(cls, classpath_prepend=None, extra_jvm_options=None):
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/jvm/tasks/jvm_compile/rsc/BUILD
@@ -1,6 +1,7 @@
python_library(
dependencies = [
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:dataclasses',
'src/python/pants/backend/jvm/subsystems:java',
'src/python/pants/backend/jvm/subsystems:jvm_platform',
'src/python/pants/backend/jvm/subsystems:rsc',
Expand Down
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import re
from dataclasses import dataclass

from pants.backend.jvm.subsystems.dependency_context import DependencyContext # noqa
from pants.backend.jvm.subsystems.rsc import Rsc
Expand All @@ -31,7 +32,7 @@
from pants.util.contextutil import Timer
from pants.util.dirutil import fast_relpath, fast_relpath_optional, safe_mkdir
from pants.util.memo import memoized_method, memoized_property
from pants.util.objects import datatype, enum
from pants.util.objects import enum
from pants.util.strutil import safe_shlex_join


Expand Down Expand Up @@ -552,10 +553,10 @@ def record(k, v):
all_jobs = cache_doublecheck_jobs + rsc_jobs + zinc_jobs + [write_to_cache_job]
return (all_jobs, len(compile_jobs))

class RscZincMergedCompileContexts(datatype([
('rsc_cc', RscCompileContext),
('zinc_cc', CompileContext),
])): pass
@dataclass(frozen=True)
class RscZincMergedCompileContexts:
rsc_cc: RscCompileContext
zinc_cc: CompileContext

def select_runtime_context(self, merged_compile_context):
return merged_compile_context.zinc_cc
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/reports/BUILD
Expand Up @@ -3,12 +3,12 @@

python_library(
dependencies=[
'3rdparty/python:dataclasses',
'src/python/pants/backend/jvm/tasks/reports/templates',
'src/python/pants/base:mustache',
'src/python/pants/util:dirutil',
'src/python/pants/util:memo',
'src/python/pants/util:meta',
'src/python/pants/util:objects',
'src/python/pants/util:strutil',
],
)
16 changes: 10 additions & 6 deletions src/python/pants/backend/jvm/tasks/reports/junit_html_report.py
Expand Up @@ -8,12 +8,13 @@
import xml.etree.ElementTree as ET
from abc import ABC, abstractmethod
from collections import defaultdict
from dataclasses import dataclass
from functools import total_ordering
from typing import Any, Optional

from pants.base.mustache import MustacheRenderer
from pants.util.dirutil import safe_mkdir_for, safe_walk
from pants.util.memo import memoized_property
from pants.util.objects import datatype


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -140,11 +141,14 @@ def as_dict(self):
return d


class ReportTestCase(datatype(['name', 'time', 'failure', 'error', 'skipped'])):
@dataclass(frozen=True, order=True)
class ReportTestCase:
"""Data object for a JUnit test case"""

def __new__(cls, name, time, failure=None, error=None, skipped=False):
return super().__new__(cls, name, float(time), failure, error, skipped)
name: Any
time: float
failure: Optional[Any] = None
error: Optional[Any] = None
skipped: bool = False

@memoized_property
def icon_class(self):
Expand Down Expand Up @@ -244,7 +248,7 @@ def _parse_xml_file(xml_file):

testcases.append(ReportTestCase(
testcase.attrib['name'],
testcase.attrib.get('time', 0),
float(testcase.attrib.get('time', 0)),
failure,
error,
skipped
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/native/subsystems/BUILD
Expand Up @@ -4,6 +4,7 @@

python_library(
dependencies=[
'3rdparty/python:dataclasses',
'src/python/pants/backend/native/config',
'src/python/pants/backend/native/subsystems/binaries',
'src/python/pants/backend/native/subsystems/utils',
Expand Down
47 changes: 33 additions & 14 deletions src/python/pants/backend/native/subsystems/native_toolchain.py
@@ -1,6 +1,8 @@
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from dataclasses import dataclass
from typing import Any

from pants.backend.native.config.environment import (
Assembler,
Expand All @@ -21,7 +23,6 @@
from pants.engine.selectors import Get
from pants.subsystem.subsystem import Subsystem
from pants.util.memo import memoized_property
from pants.util.objects import datatype


class NativeToolchain(Subsystem):
Expand Down Expand Up @@ -69,7 +70,9 @@ def _libc_dev(self):
return LibcDev.scoped_instance(self)


class LibcObjects(datatype(['crti_object_paths'])): pass
@dataclass(frozen=True)
class LibcObjects:
crti_object_paths: Any


class LinkerWrapperMixin:
Expand All @@ -83,22 +86,34 @@ def for_compiler(self, compiler, platform):
.copy(exe_filename=compiler.exe_filename))


class GCCLinker(datatype([('linker', Linker)]), LinkerWrapperMixin): pass
@dataclass(frozen=True)
class GCCLinker(LinkerWrapperMixin):
linker: Linker


class LLVMLinker(datatype([('linker', Linker)]), LinkerWrapperMixin): pass
@dataclass(frozen=True)
class LLVMLinker(LinkerWrapperMixin):
linker: Linker


class GCCCToolchain(datatype([('c_toolchain', CToolchain)])): pass
@dataclass(frozen=True)
class GCCCToolchain:
c_toolchain: CToolchain


class GCCCppToolchain(datatype([('cpp_toolchain', CppToolchain)])): pass
@dataclass(frozen=True)
class GCCCppToolchain:
cpp_toolchain: CppToolchain


class LLVMCToolchain(datatype([('c_toolchain', CToolchain)])): pass
@dataclass(frozen=True)
class LLVMCToolchain:
c_toolchain: CToolchain


class LLVMCppToolchain(datatype([('cpp_toolchain', CppToolchain)])): pass
@dataclass(frozen=True)
class LLVMCppToolchain:
cpp_toolchain: CppToolchain


@rule
Expand All @@ -120,11 +135,13 @@ def select_assembler(platform: Platform, native_toolchain: NativeToolchain) -> A
yield assembler


class BaseLinker(datatype([('linker', Linker)])):
@dataclass(frozen=True)
class BaseLinker:
"""A Linker which is not specific to any compiler yet.

This represents Linker objects provided by subsystems, but may need additional information to be
usable by a specific compiler."""
linker: Linker


# TODO: select the appropriate `Platform` in the `@rule` decl using variants!
Expand Down Expand Up @@ -153,12 +170,14 @@ def select_llvm_linker(base_linker: BaseLinker) -> LLVMLinker:
return LLVMLinker(base_linker.linker)


class GCCInstallLocationForLLVM(datatype(['toolchain_dir'])):
@dataclass(frozen=True)
class GCCInstallLocationForLLVM:
"""This class is convertible into a list of command line arguments for clang and clang++.

This is only used on Linux. The option --gcc-toolchain stops clang from searching for another gcc
on the host system. The option appears to only exist on Linux clang and clang++.
"""
toolchain_dir: Any

@property
def as_clang_argv(self):
Expand Down Expand Up @@ -302,10 +321,10 @@ def select_gcc_cpp_toolchain(
yield GCCCppToolchain(CppToolchain(working_cpp_compiler, working_linker))


class ToolchainVariantRequest(datatype([
('toolchain', NativeToolchain),
('variant', ToolchainVariant),
])): pass
@dataclass(frozen=True)
class ToolchainVariantRequest:
toolchain: NativeToolchain
variant: ToolchainVariant


@rule
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/native/targets/BUILD
Expand Up @@ -9,6 +9,7 @@ python_library(
'src/python/pants/base:payload',
'src/python/pants/base:payload_field',
'src/python/pants/build_graph',
'src/python/pants/util:objects',
'src/python/pants/util:memo',
],
)
1 change: 1 addition & 0 deletions src/python/pants/backend/native/tasks/BUILD
Expand Up @@ -4,6 +4,7 @@

python_library(
dependencies=[
'3rdparty/python:dataclasses',
'src/python/pants/backend/native/config',
'src/python/pants/backend/native/subsystems',
'src/python/pants/backend/native/subsystems/packaging',
Expand Down