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

Allow all yaml.safe_{load, dump} types #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions example/config_types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NONE: null
BOOL: True
INT: 42
FLOAT: 3.14
BINARY: !!binary YmluYXJ5
DATE: 2020-03-05
DATETIME: 2020-03-05T13:07:56Z
PAIRS: !!pairs [ one: 1, two: 2 ]
SET: !!set {1,2,3}
STRING: hello
# Check None overwrite
STR_NONE: hello
# Check overwrite by None
NONE_STR: null
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="yacs",
version="0.1.6",
version="0.2.0",
author="Ross Girshick",
author_email="ross.girshick@gmail.com",
description="Yet Another Configuration System",
Expand Down
13 changes: 10 additions & 3 deletions yacs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

import copy
import datetime
import io
import logging
import os
Expand All @@ -45,10 +46,13 @@
_FILE_TYPES = (io.IOBase,)

# CfgNodes can only contain a limited set of valid types
_VALID_TYPES = {tuple, list, str, int, float, bool}
# py2 allow for str and unicode
_VALID_TYPES = {tuple, list, str, int, float, bool, datetime.date, datetime.datetime, set, type(None)}
# py2 allow for unicode and long
if _PY2:
_VALID_TYPES = _VALID_TYPES.union({unicode}) # noqa: F821
_VALID_TYPES = _VALID_TYPES.union({unicode, long}) # noqa: F821
# py3 allow for bytes (py2 str)
else:
_VALID_TYPES = _VALID_TYPES.union({bytes})

# Utilities for importing modules from file paths
if _PY2:
Expand Down Expand Up @@ -482,6 +486,9 @@ def _check_and_coerce_cfg_value_type(replacement, original, key, full_key):
the right type. The type is correct if it matches exactly or is one of a few
cases in which the type can be easily coerced.
"""
if original is None or replacement is None:
return replacement

original_type = type(original)
replacement_type = type(replacement)

Expand Down
29 changes: 27 additions & 2 deletions yacs/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import tempfile
import unittest
from datetime import date, datetime

import yaml

import yacs.config
from yacs.config import CfgNode as CN
Expand Down Expand Up @@ -196,8 +199,13 @@ def test_nonexistant_key_from_list(self):
cfg.merge_from_list(opts)

def test_load_cfg_invalid_type(self):
# FOO.BAR.QUUX will have type None, which is not allowed
cfg_string = "FOO:\n BAR:\n QUUX:"
class CustomClass(yaml.YAMLObject):
"""A custom class that yaml.safe_load can load."""
yaml_loader = yaml.SafeLoader
yaml_tag = u'!CustomClass'

# FOO.BAR.QUUX will have type CustomClass, which is not allowed
cfg_string = "FOO:\n BAR:\n QUUX: !CustomClass {}"
with self.assertRaises(AssertionError):
yacs.config.load_cfg(cfg_string)

Expand Down Expand Up @@ -298,6 +306,23 @@ def test_new_allowed_bad(self):
with self.assertRaises(KeyError):
cfg.merge_from_file("example/config_new_allowed_bad.yaml")

def test_all_types(self):
cfg = CN()
cfg.NONE = None
cfg.BOOL = False
cfg.INT = 0
cfg.FLOAT = 2.72
cfg.BINARY = b"binary"
cfg.DATE = date(2020, 1, 1)
cfg.DATETIME = datetime(2020, 1, 1, 0, 0, 1)
cfg.PAIRS = [("zero", 0)]
cfg.SET = {1, }
cfg.STRING = "string"
cfg.STR_NONE = None
cfg.NONE_STR = "string"

cfg.merge_from_file("example/config_types.yaml")


class TestCfgNodeSubclass(unittest.TestCase):
def test_merge_cfg_from_file(self):
Expand Down