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

Enable fastenv to manage environment variables and .env files #2

Merged
merged 9 commits into from
Jul 27, 2021

Conversation

br3ndonland
Copy link
Owner

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

  • Add AnyIO to dependencies (efd1815)
  • Implement DotEnv class for environment variables (512b36d)
  • Implement async file I/O with AnyIO (9164f98)
  • Add logging to find_dotenv and load_dotenv (5f28508)
  • Update to anyio@'^3.3' (742543d)
  • Remove AnyIO file I/O # type: ignore[arg-type] (347da2a)
  • Require AnyIO (609d968)
  • Use anyio.Path for asynchronous path operations (9eb8dc4)
  • Update project aims after implementing dotenv code (72c9c89)

Related

Trio docs: Asynchronous filesystem I/O
AnyIO docs: File I/O
agronholm/anyio#327
theskumar/python-dotenv#263

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`.
@vercel
Copy link

vercel bot commented Jul 27, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/br3ndonland/fastenv/4NfkRmxhkkq7vLoRcg7XLaHW75eE
✅ Preview: https://fastenv-git-dotenv-br3ndonland.vercel.app

@br3ndonland br3ndonland changed the title Enable fastenv to manage environment variables and _.env_ files Enable fastenv to manage environment variables and .env files Jul 27, 2021
@codecov
Copy link

codecov bot commented Jul 27, 2021

Codecov Report

Merging #2 (72c9c89) into develop (ef9ba4c) will not change coverage.
The diff coverage is 100.0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop       #2    +/-   ##
=========================================
  Coverage    100.0%   100.0%            
=========================================
  Files            1        3     +2     
  Lines            1      104   +103     
=========================================
+ Hits             1      104   +103     
Flag Coverage Δ
unit 100.0% <100.0%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
fastenv/__init__.py 100.0% <100.0%> (ø)
fastenv/dotenv.py 100.0% <100.0%> (ø)
fastenv/utilities.py 100.0% <100.0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef9ba4c...72c9c89. Read the comment docs.

@br3ndonland br3ndonland merged commit ab10ad6 into develop Jul 27, 2021
@br3ndonland br3ndonland deleted the dotenv branch July 27, 2021 05:35
br3ndonland added a commit that referenced this pull request Nov 8, 2021
#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant