Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/openfermion/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
20 changes: 10 additions & 10 deletions src/openfermion/_compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -36,21 +36,21 @@ 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)

return decorated_test


@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:
Expand All @@ -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"):
Expand Down
8 changes: 5 additions & 3 deletions src/openfermion/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Loading