From ed5a0a3ff93346f8ecc9bc2d680ce79b98cfa7fb Mon Sep 17 00:00:00 2001 From: Victory Omole Date: Thu, 3 Mar 2022 17:41:50 -0600 Subject: [PATCH] Add type annotations to method overrides in child classes (#5051) Fixes: https://github.com/quantumlib/Cirq/issues/554 --- cirq/study/sweeps.py | 4 ++-- cirq/value/abc_alt_test.py | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cirq/study/sweeps.py b/cirq/study/sweeps.py index 880ee18c082..c4c41016d4e 100644 --- a/cirq/study/sweeps.py +++ b/cirq/study/sweeps.py @@ -98,10 +98,10 @@ def __add__(self, other: 'Sweep') -> 'Sweep': return Zip(*sweeps) @abc.abstractmethod - def __eq__(self, other): + def __eq__(self, other) -> bool: pass - def __ne__(self, other): + def __ne__(self, other) -> bool: return not self == other @property diff --git a/cirq/value/abc_alt_test.py b/cirq/value/abc_alt_test.py index f59947b000e..33ff9c4ce52 100644 --- a/cirq/value/abc_alt_test.py +++ b/cirq/value/abc_alt_test.py @@ -13,6 +13,8 @@ # limitations under the License. import abc +from typing import NoReturn, Optional + import pytest from cirq import ABCMetaImplementAnyOneOf, alternative @@ -21,7 +23,7 @@ def test_regular_abstract(): class RegularAbc(metaclass=ABCMetaImplementAnyOneOf): @abc.abstractmethod - def my_method(self): + def my_method(self) -> str: """Docstring.""" with pytest.raises(TypeError, match='abstract'): @@ -38,11 +40,11 @@ def my_method(self, arg, kw=99): """my_method doc.""" @abc.abstractmethod - def alt(self): + def alt(self) -> str: pass class SingleAlternativeChild(SingleAlternative): - def alt(self): + def alt(self) -> str: return 'alt' class SingleAlternativeOverride(SingleAlternative): @@ -80,22 +82,22 @@ def _default_impl(self, arg, kw=99): """Default implementation.""" @alternative(requires='alt', implementation=_default_impl) - def my_method(self, arg, kw=99): + def my_method(self, arg, kw=99) -> None: """my_method doc.""" @abc.abstractmethod - def alt(self): + def alt(self) -> None: pass class SingleAlternativeChild(SingleAlternative): - def alt(self): + def alt(self) -> None: """Alternative method.""" class SingleAlternativeOverride(SingleAlternative): - def my_method(self, arg, kw=99): + def my_method(self, arg, kw=99) -> None: """my_method override.""" - def alt(self): + def alt(self) -> None: """Unneeded alternative method.""" assert SingleAlternative.my_method.__doc__ == 'my_method doc.' @@ -151,32 +153,32 @@ def _default_impl2(self, arg, kw=99): @alternative(requires='alt1', implementation=_default_impl1) @alternative(requires='alt2', implementation=_default_impl2) - def my_method(self, arg, kw=99): + def my_method(self, arg, kw=99) -> str: """Docstring.""" @abc.abstractmethod - def alt1(self): + def alt1(self) -> Optional[str]: pass @abc.abstractmethod - def alt2(self): + def alt2(self) -> Optional[str]: pass class TwoAlternativesChild(TwoAlternatives): - def alt1(self): + def alt1(self) -> str: return 'alt1' - def alt2(self): + def alt2(self) -> NoReturn: raise RuntimeError # coverage: ignore class TwoAlternativesOverride(TwoAlternatives): - def my_method(self, arg, kw=99): + def my_method(self, arg, kw=99) -> str: return 'override' - def alt1(self): + def alt1(self) -> NoReturn: raise RuntimeError # coverage: ignore - def alt2(self): + def alt2(self) -> NoReturn: raise RuntimeError # coverage: ignore class TwoAlternativesForceSecond(TwoAlternatives):