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

Fix/callable protocols 2 #599

Merged
merged 11 commits into from
Nov 7, 2024

Conversation

MiguelMonteiro
Copy link
Contributor

@MiguelMonteiro MiguelMonteiro commented Oct 15, 2024

What does this PR do?

Fixes callable protocol inheritance by allowing a list of private methods to be considered when matching signatures for inheritance of subclasses. The set of allowed_dunder_methods currently only contains the __call__ method.

Fixes #595

Before submitting

  • Did you read the contributing guideline?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (442f9bf) to head (bd03440).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #599   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         6491      6493    +2     
=========================================
+ Hits          6491      6493    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a unit test for a protocol with __call__.

CHANGELOG.rst Outdated Show resolved Hide resolved
jsonargparse/_typehints.py Outdated Show resolved Hide resolved
jsonargparse_tests/test_subclasses.py Dismissed Show dismissed Hide dismissed
@MiguelMonteiro
Copy link
Contributor Author

Please add a unit test for a protocol with __call__.

I added some tests for callable protocols. One question that came up is whether functions should be checked against the protocol. Currently that test is failing, here is the example:

class CallableInterface(Protocol):
    def __call__(self, items: List[float]) -> List[float]: ...


class ImplementsCallableInterface1:
    def __init__(self, batch_size: int):
        self.batch_size = batch_size

    def __call__(self, items: List[float]) -> List[float]:
        return items

def implements_callable_interface2(items: List[float]) -> List[float]:
    return items

The result is:

implements_protocol(ImplementsCallableInterface1, CallableInterface) = True
implements_protocol(implements_callable_interface2, CallableInterface) = False

jsonargparse/_typehints.py Outdated Show resolved Hide resolved
jsonargparse/_typehints.py Outdated Show resolved Hide resolved
jsonargparse/_typehints.py Outdated Show resolved Hide resolved
@MiguelMonteiro
Copy link
Contributor Author

Thanks for the feedback, sorry it's been really busy at work, I will try to get to this next week

@MiguelMonteiro
Copy link
Contributor Author

I made the necessary fixes, currently only one subtest is failing but I don't fully understand why.

@pytest.mark.parametrize(
    "expected, value",
    [
        (True, ImplementsCallableInterface1),
        (False, ImplementsCallableInterface1(1)),
        (
            False,
            implements_callable_interface2,
        ),  # TODO: switch to True once functions that implement protocol are checked correctly
        (False, NotImplementsCallableInterface1),
        (False, NotImplementsCallableInterface2),
        (False, NotImplementsCallableInterface3),
        (False, not_implements_callable_interface4),
        (False, object),
    ],
)
def test_implements_callable_protocol(expected, value):
    assert implements_protocol(value, CallableInterface) is expected

The last subtest (False, object) raises the following error instead of returning False

FAILED jsonargparse_tests/test_subclasses.py::test_implements_callable_protocol[False-object] - ValueError: Invalid or unsupported input: class=<class 'object'>, method_or_property=__call__

It does not happen in the normal (not callable) protocol tests.

Copy link

sonarqubecloud bot commented Nov 1, 2024

Copy link
Member

@mauvilsa mauvilsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. But please remove the TODOs as commented.

jsonargparse/_typehints.py Outdated Show resolved Hide resolved
jsonargparse/_typehints.py Outdated Show resolved Hide resolved
jsonargparse_tests/test_subclasses.py Outdated Show resolved Hide resolved
jsonargparse_tests/test_subclasses.py Outdated Show resolved Hide resolved
@MiguelMonteiro
Copy link
Contributor Author

all should be good now

@mauvilsa mauvilsa merged commit 006d9c4 into omni-us:main Nov 7, 2024
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Callable protocols inheritance broken
2 participants