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

Callable testing #114

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions overrides/typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ def _is_normal_subtype(
if not right.args:
return _is_origin_subtype(left.origin, right.origin)

# Callable
if isinstance(left.origin, typing.Callable) and isinstance(right.origin, typing.Callable): # type: ignore
# Callable input arguments are contravariant
inputs_correct = _is_origin_subtype_args(right.args[0], left.args[0], forward_refs)
outputs_correct = _is_origin_subtype_args(tuple([left.args[-1]]), tuple([right.args[-1]]), forward_refs)
return inputs_correct and outputs_correct

if _is_origin_subtype(left.origin, right.origin):
return _is_origin_subtype_args(left.args, right.args, forward_refs)

Expand Down
47 changes: 47 additions & 0 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Callable

from overrides import override


class Thing:
pass


class Animal(Thing):
pass


class Dog(Animal):
pass


class AnimalHerd:
def feed_animals(self, feed: Callable[[Animal], None]) -> None:
pass

def breed_animal(self, breeder: Callable[[], Animal]) -> None:
pass

def transform_animal(self, transformer: Callable[[Animal], Animal]) -> None:
pass


def test_more_specific_function_input_is_fine():
class DogHerd(AnimalHerd):
@override
def feed_animals(self, feed: Callable[[Dog], None]) -> None:
pass


def test_more_abstract_function_output_is_fine():
class ThingsHerd(AnimalHerd):
@override
def breed_animal(self, breeder: Callable[[], Thing]) -> None:
pass


def test_function_input():
class Herd(AnimalHerd):
@override
def transform_animal(self, transformer: Callable[[Dog], Thing]) -> None:
pass