-
Notifications
You must be signed in to change notification settings - Fork 6
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
Use stdlib toml provider on Python >= 3.11. Switch to tomli otherwise #20
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abc5d68
Use stdlib toml provider on Python >= 3.11. Switch to tomli otherwise
wimglenn 2192c75
drop 3.6?
wimglenn c663369
address the coverage miss
wimglenn b10c0b0
wrap tomllib.load to handle text streams
wimglenn 195f0bf
positional only args require 3.8
wimglenn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import io | ||
|
||
try: | ||
import tomllib | ||
except ImportError: | ||
import tomli as tomllib | ||
|
||
|
||
def load(f, *, parse_float=float): | ||
# wrapper around tomllib.load to be more forgiving of streams opened in text mode | ||
if isinstance(f, io.TextIOWrapper): | ||
return tomllib.load(f.buffer, parse_float=parse_float) | ||
elif isinstance(f, io.StringIO): | ||
return tomllib.loads(f.getvalue(), parse_float=parse_float) | ||
else: | ||
return tomllib.load(f, parse_float=parse_float) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pytest | ||
from testfixtures import compare | ||
|
||
from configurator import Config | ||
|
||
|
||
def test_load_toml_from_path_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
config = Config.from_path(path) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_path_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
config = Config.from_path(path, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_byte_stream_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
with path.open(mode="rb") as stream: | ||
config = Config.from_stream(stream) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_byte_stream_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
with path.open(mode="rb") as stream: | ||
config = Config.from_stream(stream, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text_stream_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
with path.open(mode="rt") as stream: | ||
config = Config.from_stream(stream) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text_stream_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
with path.open(mode="rt") as stream: | ||
config = Config.from_stream(stream, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text(): | ||
parser = Config.parsers['toml'] | ||
config = Config.from_text('k = "v"', parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_bytes(): | ||
parser = Config.parsers['toml'] | ||
config = Config.from_text(b'k = "v"', parser) | ||
compare(config.k, "v") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!