diff --git a/src/openfermion/_compat.py b/src/openfermion/_compat.py index bc90c8c98..e7527537f 100644 --- a/src/openfermion/_compat.py +++ b/src/openfermion/_compat.py @@ -9,12 +9,15 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Tuple + +from typing import Any, Dict, Tuple from types import ModuleType import warnings -def wrap_module(module: ModuleType, deprecated_attributes: Dict[str, Tuple[str, str]]): +def wrap_module( + module: ModuleType, deprecated_attributes: Dict[str, Tuple[str, str]] +) -> ModuleType: """Wrap a module with deprecated attributes. Args: @@ -31,7 +34,7 @@ def wrap_module(module: ModuleType, deprecated_attributes: Dict[str, Tuple[str, class Wrapped(ModuleType): __dict__ = module.__dict__ - def __getattr__(self, name): + def __getattr__(self, name: str) -> Any: if name in deprecated_attributes: version, fix = deprecated_attributes[name] warnings.warn( diff --git a/src/openfermion/_compat_test.py b/src/openfermion/_compat_test.py index 68cdd4db6..a53724863 100644 --- a/src/openfermion/_compat_test.py +++ b/src/openfermion/_compat_test.py @@ -9,6 +9,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from typing import Any, Callable import functools import warnings @@ -17,7 +18,6 @@ import deprecation from cirq._compat import deprecated -from cirq.testing import assert_deprecated import openfermion from openfermion._compat import wrap_module @@ -36,7 +36,7 @@ def deprecated_test(test: Callable) -> Callable: """ @functools.wraps(test) - def decorated_test(*args, **kwargs) -> Any: + def decorated_test(*args: Any, **kwargs: Any) -> Any: with pytest.deprecated_call(): test(*args, **kwargs) @@ -44,13 +44,13 @@ def decorated_test(*args, **kwargs) -> Any: @deprecation.deprecated() -def f(): +def f() -> None: pass -def test_deprecated_test(): +def test_deprecated_test() -> None: @deprecated_test - def test(): + def test() -> None: f() with warnings.catch_warnings(record=True) as w: @@ -60,16 +60,16 @@ def test(): assert len(w) == 0 -def test_wrap_module(): - openfermion.deprecated_attribute = None +def test_wrap_module() -> None: + openfermion.deprecated_attribute = None # type: ignore[attr-defined] wrapped_openfermion = wrap_module(openfermion, {'deprecated_attribute': ('', '')}) with pytest.deprecated_call(): - _ = wrapped_openfermion.deprecated_attribute + _ = wrapped_openfermion.deprecated_attribute # type: ignore[attr-defined] -def test_cirq_deprecations(): +def test_cirq_deprecations() -> None: @deprecated(deadline="v0.12", fix="use new_func") - def old_func(): + def old_func() -> None: pass with pytest.raises(ValueError, match="deprecated .* not allowed"): diff --git a/src/openfermion/conftest.py b/src/openfermion/conftest.py index ff9a716d6..104edd1a1 100644 --- a/src/openfermion/conftest.py +++ b/src/openfermion/conftest.py @@ -12,11 +12,13 @@ import os import random -import pytest +from typing import Any + import numpy as np +import pytest -def pytest_configure(config): +def pytest_configure(config: Any) -> None: # Set seeds for collection-time parameterization. random.seed(0) np.random.seed(0) @@ -25,7 +27,7 @@ def pytest_configure(config): @pytest.fixture(autouse=True) -def set_random_seed(): +def set_random_seed() -> None: """Set a fixed random seed when testing.""" random.seed(0) np.random.seed(0)