From e21e269c388b14ba061e188a1d9154399dad9659 Mon Sep 17 00:00:00 2001 From: antazoey Date: Tue, 15 Oct 2024 10:53:51 -0500 Subject: [PATCH] feat: properly support project-level settings (#2316) --- docs/userguides/config.md | 40 +++++++++++++++++++++++++++++++- tests/functional/test_config.py | 13 +++++++++++ tests/functional/test_project.py | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/userguides/config.md b/docs/userguides/config.md index e61ecb6cdc..24182fffa8 100644 --- a/docs/userguides/config.md +++ b/docs/userguides/config.md @@ -8,8 +8,9 @@ There are two locations you can place an `ape-config.yaml` file. Project settings take precedent, but global settings allow you to configure preferences across all projects, such as your default mainnet provider (e.g. Alchemy versus running your own node). -This guide serves as an index of the settings you can include in any `ape-config.yaml` file. +This guide serves as an index of some settings you can include in any `ape-config.yaml` file. This guide is **PURPOSELY** alphabetized to facilitate easier look-up of keys. +Plugins for Ape may define their own configs. Most of the features in this guide are documented more-fully elsewhere in the user-guides. @@ -242,3 +243,40 @@ test: mnemonic: test test test test test test test test test test test junk number_of_accounts: 5 ``` + +## Plugin Settings + +To configure a plugin, use the name of the plugin followed by any of the plugin's settings. +For example, to configure the `ape-solidity` plugin, you would do: + +```yaml +solidity: + evm_version: paris # Or any other setting defined in `ape-solidity`. +``` + +## Non-plugin settings + +Projects can use their own settings. +Meaning, you can put whatever data you want in an `ape-config.yaml` file and read it in Ape. + +```{note} +These types of settings lack sophisticated Pydantic validation and are limited in that respect. +Simple validation, however, will occur, such as if it the value `isnumeric()`, it will be converted to an int, or if the value is a boolean name it will convert it to a `bool`. +``` + +```yaml +my_project_key: + my_string: "my_value" + my_int: 123 + my_bool: True +``` + +Then, to access it (or any setting for that matter): + +```python +from ape import project + +my_str = project.config.my_project_key.my_string # "my_value" +my_int = project.config.my_project_key.my_int # 123 +my_bool = project.config.my_project_key.my_bool # True +``` diff --git a/tests/functional/test_config.py b/tests/functional/test_config.py index 130a7500ae..1cfef6716b 100644 --- a/tests/functional/test_config.py +++ b/tests/functional/test_config.py @@ -494,3 +494,16 @@ def test_dependencies_list_of_non_dicts(project): expected = "Expecting mapping for dependency. Received: int." with pytest.raises(ConfigError, match=expected): _ = ApeConfig.model_validate(data) + + +def test_project_level_settings(project): + """ + Settings can be configured in an ape-config.yaml file + that are not part of any plugin. This test ensures that + works. + """ + # NOTE: Using strings for the values to show simple validation occurs. + with project.temp_config(my_string="my_string", my_int=123, my_bool=True): + assert project.config.my_string == "my_string" + assert project.config.my_int == 123 + assert project.config.my_bool is True diff --git a/tests/functional/test_project.py b/tests/functional/test_project.py index 77693e6324..5a6c0fa6b4 100644 --- a/tests/functional/test_project.py +++ b/tests/functional/test_project.py @@ -901,7 +901,7 @@ def clean(): ): actual = pm.sources.lookup(closest) expected = nested_source_a - assert actual == expected, f"Failed to lookup {closest}" + assert actual.stem == expected.stem, f"Failed to lookup {closest}" # Nested: 2nd level for closest in (