Skip to content

Commit

Permalink
Update linter config & reformat code with black (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 authored Feb 29, 2024
1 parent dc42c30 commit ed96010
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
exclude = .git,.nox,.tox,omegaconf/grammar/gen,build,omegaconf/vendor
max-line-length = 119
select = E,F,W,C
ignore=W503,E203
ignore=W503,E203,E701,E704
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ default_language_version:
# Hook versions should match those in requirements/dev.txt
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 23.7.0
rev: 24.2.0
hooks:
- id: black

Expand All @@ -19,7 +19,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.8.0
hooks:
- id: mypy
args: [--strict]
Expand Down
36 changes: 20 additions & 16 deletions benchmark/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from typing import Any, Dict, List

from pytest import fixture, lazy_fixture, mark, param
from pytest import fixture, mark, param

from omegaconf import OmegaConf
from omegaconf._utils import ValueKind, _is_missing_literal, get_value_kind
Expand Down Expand Up @@ -72,16 +72,17 @@ def small_listconfig(small_list: Any) -> Any:


@mark.parametrize(
"data",
"data_fixture",
[
lazy_fixture("small_dict"), # type: ignore
lazy_fixture("large_dict"), # type: ignore
lazy_fixture("small_dict_config"), # type: ignore
lazy_fixture("large_dict_config"), # type: ignore
lazy_fixture("dict_config_with_list_leaf"), # type: ignore
"small_dict",
"large_dict",
"small_dict_config",
"large_dict_config",
"dict_config_with_list_leaf",
],
)
def test_omegaconf_create(data: Any, benchmark: Any) -> None:
def test_omegaconf_create(data_fixture: str, benchmark: Any, request: Any) -> None:
data = request.getfixturevalue(data_fixture)
benchmark(OmegaConf.create, data)


Expand All @@ -97,24 +98,27 @@ def test_omegaconf_merge(merge_function: Any, merge_data: Any, benchmark: Any) -


@mark.parametrize(
"lst",
"lst_fixture",
[
lazy_fixture("small_list"), # type: ignore
lazy_fixture("small_listconfig"), # type: ignore
"small_list",
"small_listconfig",
],
)
def test_list_in(lst: List[Any], benchmark: Any) -> None:
def test_list_in(lst_fixture: str, benchmark: Any, request: Any) -> None:
lst: List[Any] = request.getfixturevalue(lst_fixture)
benchmark(lambda seq, val: val in seq, lst, 10)


@mark.parametrize(
"lst",
"lst_fixture",
[
lazy_fixture("small_list"), # type: ignore
lazy_fixture("small_listconfig"), # type: ignore
"small_list",
"small_listconfig",
],
)
def test_list_iter(lst: List[Any], benchmark: Any) -> None:
def test_list_iter(lst_fixture: str, benchmark: Any, request: Any) -> None:
lst: List[Any] = request.getfixturevalue(lst_fixture)

def iterate(seq: Any) -> None:
for _ in seq:
pass
Expand Down
44 changes: 16 additions & 28 deletions omegaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ def _format_and_raise(
assert False

@abstractmethod
def _get_full_key(self, key: Optional[Union[DictKeyType, int]]) -> str:
...
def _get_full_key(self, key: Optional[Union[DictKeyType, int]]) -> str: ...

def _dereference_node(self) -> "Node":
node = self._dereference_node_impl(throw_on_resolution_failure=True)
Expand Down Expand Up @@ -306,32 +305,27 @@ def _is_none(self) -> bool:
return self._value() is None

@abstractmethod
def __eq__(self, other: Any) -> bool:
...
def __eq__(self, other: Any) -> bool: ...

@abstractmethod
def __ne__(self, other: Any) -> bool:
...
def __ne__(self, other: Any) -> bool: ...

@abstractmethod
def __hash__(self) -> int:
...
def __hash__(self) -> int: ...

@abstractmethod
def _value(self) -> Any:
...
def _value(self) -> Any: ...

@abstractmethod
def _set_value(self, value: Any, flags: Optional[Dict[str, bool]] = None) -> None:
...
def _set_value(
self, value: Any, flags: Optional[Dict[str, bool]] = None
) -> None: ...

@abstractmethod
def _is_optional(self) -> bool:
...
def _is_optional(self) -> bool: ...

@abstractmethod
def _is_interpolation(self) -> bool:
...
def _is_interpolation(self) -> bool: ...

def _key(self) -> Any:
return self._metadata.key
Expand Down Expand Up @@ -414,8 +408,7 @@ def _get_child(
validate_key: bool = True,
throw_on_missing_value: bool = False,
throw_on_missing_key: bool = False,
) -> Union[Optional[Node], List[Optional[Node]]]:
...
) -> Union[Optional[Node], List[Optional[Node]]]: ...

@abstractmethod
def _get_node(
Expand All @@ -425,24 +418,19 @@ def _get_node(
validate_key: bool = True,
throw_on_missing_value: bool = False,
throw_on_missing_key: bool = False,
) -> Union[Optional[Node], List[Optional[Node]]]:
...
) -> Union[Optional[Node], List[Optional[Node]]]: ...

@abstractmethod
def __delitem__(self, key: Any) -> None:
...
def __delitem__(self, key: Any) -> None: ...

@abstractmethod
def __setitem__(self, key: Any, value: Any) -> None:
...
def __setitem__(self, key: Any, value: Any) -> None: ...

@abstractmethod
def __iter__(self) -> Iterator[Any]:
...
def __iter__(self) -> Iterator[Any]: ...

@abstractmethod
def __getitem__(self, key_or_index: Any) -> Any:
...
def __getitem__(self, key_or_index: Any) -> Any: ...

def _resolve_key_and_root(self, key: str) -> Tuple["Container", str]:
orig = key
Expand Down
9 changes: 3 additions & 6 deletions omegaconf/basecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ def __setstate__(self, d: Dict[str, Any]) -> None:
self.__dict__.update(d)

@abstractmethod
def __delitem__(self, key: Any) -> None:
...
def __delitem__(self, key: Any) -> None: ...

def __len__(self) -> int:
if self._is_none() or self._is_missing() or self._is_interpolation():
Expand Down Expand Up @@ -722,12 +721,10 @@ def _is_interpolation(self) -> bool:
return _is_interpolation(self.__dict__["_content"])

@abstractmethod
def _validate_get(self, key: Any, value: Any = None) -> None:
...
def _validate_get(self, key: Any, value: Any = None) -> None: ...

@abstractmethod
def _validate_set(self, key: Any, value: Any) -> None:
...
def _validate_set(self, key: Any, value: Any) -> None: ...

def _value(self) -> Any:
return self.__dict__["_content"]
Expand Down
3 changes: 1 addition & 2 deletions omegaconf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def validate_and_convert(self, value: Any) -> Any:
return self._validate_and_convert_impl(value)

@abstractmethod
def _validate_and_convert_impl(self, value: Any) -> Any:
...
def _validate_and_convert_impl(self, value: Any) -> Any: ...

def __str__(self) -> str:
return str(self._val)
Expand Down
16 changes: 6 additions & 10 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""OmegaConf module"""

import copy
import inspect
import io
Expand Down Expand Up @@ -129,44 +130,39 @@ def create(
obj: str,
parent: Optional[BaseContainer] = None,
flags: Optional[Dict[str, bool]] = None,
) -> Union[DictConfig, ListConfig]:
...
) -> Union[DictConfig, ListConfig]: ...

@staticmethod
@overload
def create(
obj: Union[List[Any], Tuple[Any, ...]],
parent: Optional[BaseContainer] = None,
flags: Optional[Dict[str, bool]] = None,
) -> ListConfig:
...
) -> ListConfig: ...

@staticmethod
@overload
def create(
obj: DictConfig,
parent: Optional[BaseContainer] = None,
flags: Optional[Dict[str, bool]] = None,
) -> DictConfig:
...
) -> DictConfig: ...

@staticmethod
@overload
def create(
obj: ListConfig,
parent: Optional[BaseContainer] = None,
flags: Optional[Dict[str, bool]] = None,
) -> ListConfig:
...
) -> ListConfig: ...

@staticmethod
@overload
def create(
obj: Optional[Dict[Any, Any]] = None,
parent: Optional[BaseContainer] = None,
flags: Optional[Dict[str, bool]] = None,
) -> DictConfig:
...
) -> DictConfig: ...

@staticmethod
def create( # noqa F811
Expand Down
7 changes: 3 additions & 4 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
-r base.txt
-r docs.txt
attrs
black==23.7.0
black==24.2.0
build
coveralls
flake8==6.0.0
isort==5.12.0
mypy==1.4.1
isort==5.13.2
mypy==1.8.0
nox
pre-commit
pyflakes
pytest
pytest-benchmark
pytest-lazy-fixture
pytest-mock
towncrier
types-setuptools # makes mypy happy
Expand Down
3 changes: 1 addition & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __eq__(self, other: Any) -> bool:
T = TypeVar("T")


class IllegalTypeGeneric(Generic[T]):
...
class IllegalTypeGeneric(Generic[T]): ...


class NonCopyableIllegalType:
Expand Down
1 change: 1 addition & 0 deletions tests/test_compare_dictconfig_vs_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TestPrimitiveTypeDunderMethods: for DictConfig where key_type is primitive
TestEnumTypeDunderMethods: for DictConfig where key_type is Enum
"""

from copy import deepcopy
from enum import Enum
from typing import Any, Dict, Optional
Expand Down
1 change: 1 addition & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Testing for OmegaConf"""

import platform
import re
import sys
Expand Down
16 changes: 10 additions & 6 deletions tests/test_omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,11 @@ def test_is_interpolation(fac: Any) -> Any:
({"foo": FloatNode(None)}, type(None)),
(
{"foo": Path("hello.txt")},
pathlib.WindowsPath
if platform.system() == "Windows"
else pathlib.PosixPath,
(
pathlib.WindowsPath
if platform.system() == "Windows"
else pathlib.PosixPath
),
),
({"foo": "bar"}, str),
({"foo": None}, type(None)),
Expand All @@ -420,9 +422,11 @@ def test_get_type(cfg: Any, type_: Any) -> None:
(b"123", bytes),
(
Path("hello.txt"),
pathlib.WindowsPath
if platform.system() == "Windows"
else pathlib.PosixPath,
(
pathlib.WindowsPath
if platform.system() == "Windows"
else pathlib.PosixPath
),
),
("foo", str),
(DictConfig(content={}), dict),
Expand Down
13 changes: 9 additions & 4 deletions tests/test_to_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
({b"abc": "bytes key"}, "? !!binary |\n YWJj\n: bytes key\n"),
(
{"path_value": Path("hello.txt")},
"path_value: !!python/object/apply:pathlib.WindowsPath\n- hello.txt\n"
if platform.system() == "Windows"
else "path_value: !!python/object/apply:pathlib.PosixPath\n- hello.txt\n",
(
"path_value: !!python/object/apply:pathlib.WindowsPath\n- hello.txt\n"
if platform.system() == "Windows"
else "path_value: !!python/object/apply:pathlib.PosixPath\n- hello.txt\n"
),
),
({123: "int key"}, "123: int key\n"),
({123.45: "float key"}, "123.45: float key\n"),
Expand All @@ -39,7 +41,10 @@ def test_to_yaml(input_: Any, expected: str) -> None:
@mark.parametrize(
"input_, expected",
[
(["item一", "item二", dict(key三="value三")], "- item一\n- item二\n- key三: value三\n"),
(
["item一", "item二", dict(key三="value三")],
"- item一\n- item二\n- key三: value三\n",
),
(dict(你好="世界", list=[1, 2]), "你好: 世界\nlist:\n- 1\n- 2\n"),
],
)
Expand Down

0 comments on commit ed96010

Please sign in to comment.