Skip to content

Commit

Permalink
Add type annotations to method overrides in child classes (quantumlib…
Browse files Browse the repository at this point in the history
  • Loading branch information
vtomole authored and rht committed May 1, 2023
1 parent f3e680a commit 5791dc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cirq-core/cirq/study/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions cirq-core/cirq/value/abc_alt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

import abc
from typing import NoReturn, Optional

import pytest

from cirq import ABCMetaImplementAnyOneOf, alternative
Expand All @@ -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'):
Expand All @@ -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):
Expand Down Expand Up @@ -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.'
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion dev_tools/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def perform_check(self, env: env_tools.PreparedEnv, verbose: bool) -> Tuple[bool
A tuple containing a pass/fail boolean and then a details message.
"""

def needs_python2_env(self):
def needs_python2_env(self) -> bool:
return False

def run(
Expand Down

0 comments on commit 5791dc6

Please sign in to comment.