-
Notifications
You must be signed in to change notification settings - Fork 0
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
Enable fastenv to manage environment variables and .env files #2
Conversation
https://anyio.readthedocs.io/en/stable/index.html https://anyio.readthedocs.io/en/stable/testing.html https://anyio.readthedocs.io/en/stable/fileio.html https://github.com/encode/starlette/releases/tag/0.15.0 AnyIO provides tools for asynchronous Python operations. To start, AnyIO will be used for asyncio support with pytest. The file I/O features may also be used when loading .env files. Of note, AnyIO is the first external dependency for Starlette. AnyIO will be an optional dependency, so the GitHub Actions workflow will be updated to ensure that it is installed.
This commit will add `class DotEnv`, and associated unit tests and docs, for management of environment variables. The class is intended to be a superset of `os.environ`, so it also inherits from `MutableMapping`.
- Add a `DotEnv.source` attribute, which will be populated with the path from which a .env file was read - Add a `DotEnv.__str__()` method, which will deserialize `DotEnv` instances into strings for dumping to files - Implement `find_dotenv`, `dump_dotenv`, `load_dotenv`, `dotenv_values` - Add documentation explaining how to work with the above methods - Update documentation comparing fastenv with python-dotenv - Update pytest fixtures in conftest.py to use async file I/O with AnyIO
This commit will add logger messages to `find_dotenv` and `load_dotenv`. A utilities module will be added, and the `fastenv` logger will be used. Successes will be logged at the `logging.INFO` level, and errors will be logged at the `logging.ERROR` level. Unit tests will be updated to check that log messages have the correct contents and log level.
Typing was corrected by agronholm/anyio#338 in version 3.3.0.
https://anyio.readthedocs.io/en/stable/index.html https://anyio.readthedocs.io/en/stable/versionhistory.html https://github.com/encode/starlette/releases/tag/0.15.0 AnyIO was initially installed as an optional dependency. Late imports and exception handling were used to allow functions to do file I/O with AnyIO, while handling `ImportError`s if the package was not installed. This commit will make AnyIO a required dependency. This is reasonable, given the current usefulness of the file I/O features, the potential usefulness of the `anyio.Path` features added in version 3.3.0, and the fact that AnyIO is the first required external dependency of Starlette. It also simplifies the logic in the `dotenv` module and tests. The `all` Poetry extras list will likely be populated with optional dependencies as the project progresses, so it will be retained.
agronholm/anyio#327 https://anyio.readthedocs.io/en/stable/fileio.html https://anyio.readthedocs.io/en/stable/versionhistory.html AnyIO 3.3.0 provides support for asynchronous path operations. This commit will refactor use of `pathlib.Path` to `anyio.Path`.
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/br3ndonland/fastenv/4NfkRmxhkkq7vLoRcg7XLaHW75eE |
Codecov Report
@@ Coverage Diff @@
## develop #2 +/- ##
=========================================
Coverage 100.0% 100.0%
=========================================
Files 1 3 +2
Lines 1 104 +103
=========================================
+ Hits 1 104 +103
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
#2 The `DotEnv().delenv()` method deletes one or more environment variables from the `DotEnv` instance. To avoid `KeyError`s when calling `del` on the instance, `delenv()` first checks that the variable is present on both the `DotEnv` instance and `os.environ` with an `if` statement (`if self.getenv(key) and os.getenv(key)`). If a variable is empty, like `EMPTY_VARIABLE=""`, `self.getenv(key)` and `os.getenv(key)` will return `""`, The statement will evaluate to `False`, and the variable will not be deleted. This commit will improve the relevant test in `tests/test_dotenv.py` (`tests/test_dotenv.py::TestDotEnvClass::test_delete_variables`) by replacing the over-simplified `example_dict` with more test parameters, revealing the lack of deletion of empty variables in the previous code, and will update `class DotEnv` to properly delete empty variables.
Description
This PR will enable fastenv to manage environment variables and .env files, completing two of the five project aims.
Of particular note, the code in this PR will make fastenv a suitable replacement for python-dotenv, addressing its many limitations. For context, see the comparisons docs and theskumar/python-dotenv#263.
Special thanks to AnyIO for providing useful utilities that power this project, especially the pytest plugin and the new asynchronous
anyio.Path
class (added in agronholm/anyio#327 and released in version 3.3.0). The availability of asynchronous path operations is very helpful.Changes
DotEnv
class for environment variables (512b36d)find_dotenv
andload_dotenv
(5f28508)anyio@'^3.3'
(742543d)# type: ignore[arg-type]
(347da2a)anyio.Path
for asynchronous path operations (9eb8dc4)Related
Trio docs: Asynchronous filesystem I/O
AnyIO docs: File I/O
agronholm/anyio#327
theskumar/python-dotenv#263