Skip to content

Commit

Permalink
Fix config.get for iterable sections (#2020)
Browse files Browse the repository at this point in the history
* Fix config.get for iterable segments

* Add unit tests for coverage

* Lint fixes
  • Loading branch information
jpy-git committed Dec 2, 2021
1 parent 6bc8791 commit 117740c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/sqlfluff/core/config.py
Expand Up @@ -615,7 +615,11 @@ def get(
self, val: str, section: Union[str, Iterable[str]] = "core", default: Any = None
):
"""Get a particular value from the config."""
return self._configs[section].get(val, default)
section_dict = self.get_section(section)
if section_dict is None:
return default

return section_dict.get(val, default)

def get_section(self, section: Union[str, Iterable[str]]) -> Union[dict, None]:
"""Return a whole section of config as a dict.
Expand Down
31 changes: 31 additions & 0 deletions test/core/config_test.py
Expand Up @@ -23,6 +23,11 @@
"bar": {"foo": "barbar"},
}

config_b = {
"core": {"rules": "L007"},
"rules": {"L007": {"operator_new_lines": "before"}},
}


@pytest.fixture
def mock_xdg_home(monkeypatch):
Expand Down Expand Up @@ -248,3 +253,29 @@ def test__config__glob_include_config_tests():
assert ("L052", 12, 9) in violations[k]
assert ("L027", 10, 8) in violations[k]
assert "L044" not in [c[0] for c in violations[k]]


def test__config__get_section():
"""Test FluffConfig.get_section method."""
cfg = FluffConfig(config_b)

assert cfg.get_section("core").get("rules", None) == "L007"
assert cfg.get_section(["rules", "L007"]) == {"operator_new_lines": "before"}
assert cfg.get_section("non_existent") is None


def test__config__get():
"""Test FluffConfig.get method."""
cfg = FluffConfig(config_b)

assert cfg.get("rules") == "L007"
assert cfg.get("rulez") is None
assert cfg.get("rulez", section="core", default=123) == 123
assert (
cfg.get("operator_new_lines", section=["rules", "L007"], default=None)
== "before"
)
assert (
cfg.get("operator_new_lines", section=["rules", "ASDFSDG007"], default=None)
is None
)

0 comments on commit 117740c

Please sign in to comment.