def test_protobuf_mypy(rule_runner: RuleRunner) -> None: rule_runner = RuleRunner( rules=[*rule_runner.rules, *protobuf_rules()], target_types=[*rule_runner.target_types, ProtobufLibrary], ) rule_runner.add_to_build_file( "", dedent("""\ python_requirement_library( name="protobuf", requirements=["protobuf==3.13.0"], ) """), ) rule_runner.create_file(f"{PACKAGE}/__init__.py") rule_runner.create_file( f"{PACKAGE}/proto.proto", dedent("""\ syntax = "proto3"; package project; message Person { string name = 1; int32 id = 2; string email = 3; } """), ) rule_runner.create_file( f"{PACKAGE}/app.py", dedent("""\ from project.proto_pb2 import Person x = Person(name=123, id="abc", email=None) """), ) rule_runner.add_to_build_file( PACKAGE, dedent("""\ python_library(dependencies=[':proto']) protobuf_library(name='proto') """), ) tgt = rule_runner.get_target(Address(PACKAGE)) result = run_mypy( rule_runner, [tgt], additional_args=[ "--backend-packages=pants.backend.codegen.protobuf.python", "--python-protobuf-mypy-plugin", ], ) assert len(result) == 1 assert 'Argument "name" to "Person" has incompatible type "int"' in result[ 0].stdout assert 'Argument "id" to "Person" has incompatible type "str"' in result[ 0].stdout assert result[0].exit_code == 1
def rules(cls): return ( *super().rules(), *python_sources_rules(), *protobuf_rules(), RootRule(PythonSourceFilesRequest), )
def rules(cls): return ( *super().rules(), *python_sources_rules(), *protobuf_rules(), QueryRule(PythonSourceFiles, (PythonSourceFilesRequest, )), QueryRule(StrippedPythonSourceFiles, (PythonSourceFilesRequest, )), )
def rules(cls): return ( *super().rules(), *protobuf_rules(), *additional_fields.rules(), *source_files.rules(), *stripped_source_files.rules(), RootRule(GeneratePythonFromProtobufRequest), )
def rules(cls): return ( *super().rules(), *protobuf_rules(), *archive.rules(), *determine_source_files.rules(), *external_tool.rules(), RootRule(GeneratePythonFromProtobufRequest), )
def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *python_sources_rules(), *protobuf_rules(), QueryRule(PythonSourceFiles, [PythonSourceFilesRequest]), QueryRule(StrippedPythonSourceFiles, [PythonSourceFilesRequest]), ], target_types=[PythonTarget, NonPythonTarget, ProtobufLibrary], )
def rules(cls): return ( *super().rules(), *protobuf_rules(), *additional_fields.rules(), *source_files.rules(), *stripped_source_files.rules(), QueryRule(HydratedSources, (HydrateSourcesRequest, )), QueryRule(GeneratedSources, (GeneratePythonFromProtobufRequest, )), )
def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *protobuf_rules(), *additional_fields.rules(), *stripped_source_files.rules(), QueryRule(HydratedSources, [HydrateSourcesRequest]), QueryRule(GeneratedSources, [GeneratePythonFromProtobufRequest]), ], target_types=[ProtobufLibrary], )
def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *python_sources_rules(), *additional_fields.rules(), *protobuf_rules(), *protobuf_subsystem_rules(), QueryRule(PythonSourceFiles, [PythonSourceFilesRequest]), QueryRule(StrippedPythonSourceFiles, [PythonSourceFilesRequest]), ], target_types=[PythonTarget, NonPythonTarget, ProtobufSourceTarget], )
def test_protobuf_mypy(rule_runner: RuleRunner) -> None: rule_runner = RuleRunner( rules=[ *rule_runner.rules, *protobuf_rules(), *protobuf_subsystem_rules() ], target_types=[*rule_runner.target_types, ProtobufLibrary], ) rule_runner.write_files({ "BUILD": ("python_requirement_library(name='protobuf', requirements=['protobuf==3.13.0'])" ), f"{PACKAGE}/__init__.py": "", f"{PACKAGE}/proto.proto": dedent("""\ syntax = "proto3"; package project; message Person { string name = 1; int32 id = 2; string email = 3; } """), f"{PACKAGE}/f.py": dedent("""\ from project.proto_pb2 import Person x = Person(name=123, id="abc", email=None) """), f"{PACKAGE}/BUILD": dedent("""\ python_library(dependencies=[':proto']) protobuf_library(name='proto') """), }) tgt = rule_runner.get_target(Address(PACKAGE, relative_file_path="f.py")) result = run_mypy( rule_runner, [tgt], extra_args=[ "--backend-packages=pants.backend.codegen.protobuf.python", "--python-protobuf-mypy-plugin", ], ) assert len(result) == 1 assert 'Argument "name" to "Person" has incompatible type "int"' in result[ 0].stdout assert 'Argument "id" to "Person" has incompatible type "str"' in result[ 0].stdout assert result[0].exit_code == 1
def rule_runner() -> RuleRunner: return RuleRunner( rules=[ *protobuf_rules(), *protobuf_subsystem_rules(), *additional_fields.rules(), *stripped_source_files.rules(), *target_types_rules(), *module_mapper.rules(), QueryRule(HydratedSources, [HydrateSourcesRequest]), QueryRule(GeneratedSources, [GeneratePythonFromProtobufRequest]), ], target_types=[ProtobufSourcesGeneratorTarget], )