def real_docker(): def configurator(binder): binder.bind_to_constructor(IDockerClient, lambda: DockerTwistedClient()) inject.clear_and_configure(configurator) yield
def make_app(): # configure our database inject.clear_and_configure(configure) app = Flask(__name__) # API api = Api(app)
def configure_dependencies(evg_api, evg_expansions, evg_project_config, selected_test_client, test_suites_dir=under_test.DEFAULT_TEST_SUITE_DIR): start_date = datetime.utcnow() end_date = start_date - timedelta(weeks=2) def dependencies(binder: inject.Binder) -> None: binder.bind(EvgExpansions, evg_expansions) binder.bind(_evergreen.EvergreenProjectConfig, evg_project_config) binder.bind( SuiteSplitConfig, evg_expansions.build_suite_split_config(start_date, end_date)) binder.bind(SplitStrategy, greedy_division) binder.bind(FallbackStrategy, round_robin_fallback) binder.bind(GenTaskOptions, evg_expansions.build_gen_task_options()) binder.bind(EvergreenApi, evg_api) binder.bind( GenerationConfiguration, GenerationConfiguration.from_yaml_file( under_test.GENERATE_CONFIG_FILE)) binder.bind(ResmokeProxyConfig, ResmokeProxyConfig(resmoke_suite_dir=test_suites_dir)) binder.bind(SelectedTestsClient, selected_test_client) inject.clear_and_configure(dependencies)
def configure_dependencies(evg_api, split_config): gen_task_options = build_mock_gen_task_options() def dependencies(binder: inject.Binder) -> None: binder.bind(SuiteSplitConfig, split_config) binder.bind(SplitStrategy, greedy_division) binder.bind(FallbackStrategy, round_robin_fallback) binder.bind(GenTaskOptions, gen_task_options) binder.bind(EvergreenApi, evg_api) binder.bind( GenerationConfiguration, GenerationConfiguration.from_yaml_file(GENERATE_CONFIG_FILE)) binder.bind( ResmokeProxyConfig, ResmokeProxyConfig( resmoke_suite_dir=under_test.DEFAULT_TEST_SUITE_DIR)) binder.bind(EvergreenFileChangeDetector, None) binder.bind(EvergreenProjectConfig, MagicMock()) binder.bind( under_test.BurnInConfig, under_test.BurnInConfig(build_id="build_id", build_variant="build variant", revision="revision")) inject.clear_and_configure(dependencies)
async def logging_support(hax_state: HaxGlobalState): def configure(binder: inject.Binder): binder.bind(HaxGlobalState, hax_state) inject.clear_and_configure(configure) yield '' inject.clear()
def backend_and_solver(_backend, _solver): def conf(binder): binder.install(_backend) binder.install(_solver) inject.clear_and_configure(conf) yield inject.clear()
def bind(): inject.clear_and_configure(lambda binder: binder.bind( records.UserRec, records.mock.MockUserRec() ).bind(records.AnalysisRec, records.mock.MockAnalysisRec()).bind( records.AnswerRec, records.mock.MockAnswerRec() ).bind(presenters.UserPresenter, presenters.mock.MockUserPresenter()).bind( presenters.AnalysisPresenter, presenters.mock.MockAnalysisPresenter() ).bind(presenters.AnswerPresenter, presenters.mock.MockAnswerPresenter()))
def configure_fake_injects(uow=None, shortifier=None): inject.clear_and_configure( lambda binder: binder.bind( 'UnitOfWorkManager', uow or FakeUnitOfWorkManager() ).bind( 'Shortifier', shortifier or FakeShortifier() ) )
def setup(self): inject.clear_and_configure(configure_test) app = make_app() app.config["TESTING"] = True with app.test_client() as client: self.client = client self.user_route = "/users" self.good_route = "/goods" self.store_route = "/stores"
def dependency_injection_config( auctions_repo_mock: Mock, email_gateway_mock: Mock, placing_bid_output_boundary_mock: Mock) -> None: def configure(binder: inject.Binder) -> None: binder.bind(AuctionsRepository, auctions_repo_mock) binder.bind(EmailGateway, email_gateway_mock) binder.bind(PlacingBidOutputBoundary, placing_bid_output_boundary_mock) inject.clear_and_configure(configure)
def injector(bind_config): def configurator(binder): for key, val in bind_config.items(): binder.bind(key, val) inject.clear_and_configure(configurator) yield inject.clear()
def dependency_injection_config( users_repo_mock: Mock, volunteer_profiles_repo_mock: Mock, remove_user_output_boundary_mock: Mock) -> None: def configure(binder: inject.Binder) -> None: binder.bind(UsersRepository, users_repo_mock) binder.bind(VolunteerProfilesRepository, volunteer_profiles_repo_mock) binder.bind(RemoveUserOutputBoundary, remove_user_output_boundary_mock) inject.clear_and_configure(configure)
def set_inject(): inject.clear_and_configure(lambda x: x.bind(ChangelogRepo, Mock()).bind( ContactsRepo, Mock()).bind(DiscountsRepo, Mock( )).bind(GeoRepo, Mock()).bind(HousesRepo, Mock()).bind( MembersRepo, Mock()).bind(OccupancyRepo, Mock()).bind( PricesRepo, Mock()).bind(ReservationsCacheRepo, Mock()).bind( ReservationsRepo, Mock()).bind(RoomsRepo, Mock()).bind( RoomTypesRepo, Mock()).bind(PoliciesRepo, Mock())) yield inject.clear()
def test_dotted_path_annotation_local_type_def( python_version, import_collision_policy, expected_import, comment, remove_type ): content = ''' class serializers: # why would you do this pass def no_op(arg1): """ Args: arg1 (serializers.Serializer): blah """ pass ''' if comment: comment = f"{comment}\n " docstring_type = "" if remove_type else " (serializers.Serializer)" expected = f'''{expected_import} class serializers: # why would you do this pass def no_op(arg1): {comment}""" Args: arg1{docstring_type}: blah """ pass ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( PYTHON_VERSION=python_version, ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=import_collision_policy, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_decorated_function(): content = ''' @whatever(param=val) def decorated(arg1): """ Args: arg1 (Tuple[str, ...]): blah Returns: Tuple[int, ...]: blah """ return tuple( int(arg) for arg in arg1 ) ''' expected = '''from typing import Tuple @whatever(param=val) def decorated(arg1): # type: (Tuple[str, ...]) -> Tuple[int, ...] """ Args: arg1: blah Returns: blah """ return tuple( int(arg) for arg in arg1 ) ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_require_return_type(require_return_type): """ NOTE: here is an example of a function where omitting the "Returns" block from the docstring and setting `REQUIRE_RETURN_TYPE=False` will give the wrong result (...an argument for `REQUIRE_RETURN_TYPE=True`) TODO: I don't know if there is any check for return statements we can do via Bowler? """ content = ''' def identity(arg1): """ Args: arg1 (Tuple[str, ...]): blah """ return arg1 ''' if not require_return_type: expected = '''from typing import Tuple def identity(arg1): # type: (Tuple[str, ...]) -> None """ Args: arg1: blah """ return arg1 ''' else: expected = content with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=require_return_type, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def make_app(): # configure our database inject.clear_and_configure(configure) app = Flask(__name__) # register blueprints and error handlers app.register_blueprint(users_bl) app.register_blueprint(goods_bl) app.register_blueprint(stores_bl) app.register_error_handler(NoSuchUserError, my_error_handler) app.register_error_handler(NoSuchStoreError, my_error_handler) return app
def test_allow_untyped_args(allow_untyped_args): content = ''' def identity(arg1): """ Args: arg1: blah Returns: Tuple[str, ...]: blah """ return arg1 ''' if allow_untyped_args: expected = '''from typing import Tuple def identity(arg1): # type: (...) -> Tuple[str, ...] """ Args: arg1: blah Returns: blah """ return arg1 ''' else: expected = content with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=allow_untyped_args, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_staticmethod(): """ First arg *is* annotatable """ content = ''' class SomeClass: @staticmethod def method(obj, whatever): """ Args: obj (object) whatever (Any) """ pass ''' expected = '''from typing import Any class SomeClass: @staticmethod def method(obj, whatever): # type: (object, Any) -> None """ Args: obj whatever """ pass ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_yields_generator(): content = ''' def generator(arg1): """ Args: arg1 (Iterable[int]): blah Yields: int: blah """ for val in arg1: yield val ''' expected = '''from typing import Generator, Iterable def generator(arg1): # type: (Iterable[int]) -> Generator[int, None, None] """ Args: arg1: blah Yields: blah """ for val in arg1: yield val ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def make_app(): # configure our database inject.clear_and_configure(configure) app = Flask(__name__) # API api = Api(app) api.add_resource(User, "/users", "/users/<int:user_id>") api.add_resource(Good, "/goods", "/goods/<int:good_id>") api.add_resource(Store, "/stores", "/stores/<int:store_id>") # error handlers app.register_error_handler(NoSuchUserError, my_error_handler) app.register_error_handler(NoSuchStoreError, my_error_handler) app.register_error_handler(NoSuchGoodError, my_error_handler) return app
def test_dotted_path_decorator(): # NOTE: this example is an instance method, so first arg is not annotatable content = ''' class SomeClass: @some_package.decorator def method(cls, obj): """ Args: obj (object) Returns: int """ return 1 ''' expected = ''' class SomeClass: @some_package.decorator def method(cls, obj): # type: (object) -> int """ Args: obj """ return 1 ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_find_local_types(python_version): expected = LocalTypes.factory( type_defs={ "T", "TopLevel", "InnerClass", "SomeTuple", "SomeTypedTuple", "SomeTypedDict", "NewClass", }, star_imports={"serious"}, names_to_packages={ "Irrelevant": "..sub", "Nonsense": "..sub", "ReallyUnused": "..sub", "Product": "other.module", "Imported": "some.module", "ConditionallyImported": "some.module", "InnerImported": "some.module", "namedtuple": "collections", "NamedTuple": "typing", "TypedDict": "typing", "TypeVar": "typing", "Union": "typing", }, package_imports={"logging", "nott.so.serious"}, signatures={ 34: ("static", ("cls", "self")), 39: ("clsmethod", ("self", )), 49: ("method", ("cls", )), 53: ("conditionally_defined_method", ("cls", )), 57: ("first", ("products", "getter")), 77: ("second", ("products", "getter")), 97: ("second_inner", ("product", "key", "default")), 118: ("third", ("product_ids", "user_id")), 133: ("fourth", ("product_id", "user_id")), 141: ("fifth", ("product_ids", "user_id")), 158: ("sixth", ("product_ids", )), }, ) test_settings = override_settings(PYTHON_VERSION=python_version) inject.clear_and_configure(configuration_factory(test_settings)) result = find_local_types("tests/fixtures/napoleon.py") assert result == expected
def __init__(self, config_obj="ProductionConfig", urls=None, settings=None): config_obj = getattr(common_settings, config_obj) logger.info( "creating new last recent used with config {}".format(config_obj)) self._config = Config(config_obj()) self._app = app self._db = self.init_database() self._redis = self.init_redis() inject.clear_and_configure(self.configure) logger.info("LRU loaded")
def test_returns_none(python_version): content = ''' def no_op(arg1): """ Args: arg1 (Tuple[str, ...]): blah Returns: None """ pass ''' # "Returns" block omitted since there was no description expected = '''from typing import Tuple def no_op(arg1): # type: (Tuple[str, ...]) -> None """ Args: arg1: blah """ pass ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( PYTHON_VERSION=python_version, ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=True, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def init_provider(): inject.clear_and_configure( lambda binder: binder.bind_to_provider(UserRepository, UserRepository)) inject.clear_and_configure(lambda binder: binder.bind_to_provider( BoardRepository, BoardRepository)) inject.clear_and_configure(lambda binder: binder.bind_to_provider( ReportRepository, ReportRepository)) inject.clear_and_configure(lambda binder: binder.bind_to_provider( NotificationRepository, NotificationRepository))
def test_allow_missing_args_section_no_args_func(allow_untyped_args): """ If func has no args but 'Returns' is given then we should be able to annotate it. """ content = ''' def identity(): """ Returns: Tuple[str, ...]: blah """ return arg1 ''' expected = '''from typing import Tuple def identity(): # type: () -> Tuple[str, ...] """ Returns: blah """ return arg1 ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=allow_untyped_args, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_handle_splat_args(): content = ''' def no_op(arg1, *args, **kwargs): """ Args: arg1 (str): blah *args (int) **kwargs (Tuple[bool, ...]) """ return ''' expected = '''from typing import Tuple def no_op(arg1, *args, **kwargs): # type: (str, *int, **Tuple[bool, ...]) -> None """ Args: arg1: blah *args **kwargs """ return ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_property(): """ First arg is not annotatable """ content = ''' class SomeClass: @property def method(obj): """ Returns: int """ return 1 ''' expected = ''' class SomeClass: @property def method(obj): # type: () -> int """ """ return 1 ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_package_imports(python_version, import_line, arg_type): content = f'''{import_line} def no_op(arg1): """ Args: arg1 ({arg_type}): blah """ pass ''' expected = f'''{import_line} def no_op(arg1): # type: ({arg_type}) -> None """ Args: arg1: blah """ pass ''' with tempfile.NamedTemporaryFile(suffix=".py") as f: with open(f.name, "w") as fw: fw.write(content) test_settings = override_settings( PYTHON_VERSION=python_version, ALLOW_UNTYPED_ARGS=False, REQUIRE_RETURN_TYPE=False, IMPORT_COLLISION_POLICY=ImportCollisionPolicy.IMPORT, UNPATHED_TYPE_POLICY=UnpathedTypePolicy.FAIL, ) inject.clear_and_configure(configuration_factory(test_settings)) annotate( f.name, in_process=True, interactive=False, write=True, silent=True, ) with open(f.name, "r") as fr: annotated = fr.read() assert annotated == expected
def test_error_message(self): class SomeClass: def __init__(self, missing_arg): self._missing_arg = missing_arg class AnotherClass(SomeClass): pass def create_some_class(missing_arg): return AnotherClass(missing_arg) def config(binder): binder.bind_to_constructor(SomeClass, autoparams()(SomeClass)) binder.bind_to_constructor(AnotherClass, create_some_class) inject.configure() # covers case when no constructor provided try: inject.instance(SomeClass) except ConstructorTypeError as err: assert 'SomeClass' in str(err) assert 'missing_arg' in str(err) inject.clear_and_configure(config) # covers case with provided constructor try: inject.instance(SomeClass) except ConstructorTypeError as err: assert 'SomeClass' in str(err) assert 'missing_arg' in str(err) try: inject.instance(AnotherClass) except TypeError as err: assert not isinstance(err, ConstructorTypeError) assert 'create_some_class' in str(err) assert 'missing_arg' in str(err)
def test_clear_and_configure(self): injector0 = inject.configure() injector1 = inject.clear_and_configure() # No exception. assert injector0 assert injector1 assert injector1 is not injector0
def fake_inject(services): def configurator(binder): for key, item in services.items(): binder.bind(key, item) inject.clear_and_configure(configurator)
def mock_docker(): mock = flexmock(DockerTwistedClient()) inject.clear_and_configure(lambda binder: binder.bind(IDockerClient, mock)) yield mock
def inject_services(configurator): inject.clear_and_configure(configurator) yield inject.clear()