Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations to method overrides in child classes #5051

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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