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

Why does the protocol check enforce that instance methods cannot have mandatory keyword-only arguments? #495

Closed
2 tasks done
mrgrain opened this issue Oct 11, 2024 · 3 comments
Labels

Comments

@mrgrain
Copy link

mrgrain commented Oct 11, 2024

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.3.0

Python version

3.12.0

What happened?

v4.3.0 added extended checks for Protocols via this commit (no PR for some reason).

Part of this change is to check methods with check_callable. This check enforces that the callable cannot have mandatory keyword-only arguments in its declaration (= keyword-only arguments without default values). However PEP3102 seems to explicitly allow keyword-only arguments without defaults.

I suspect this issue might effect all callable, but for us it now surfaced for protocols.

My question/ask is if someone can provide more background information on why this rule is enforced and how it relates to my understanding of PEP3102.

If this is unintentional, we should fix it.

How can we reproduce the bug?

import typeguard
import typing_extensions

class IFoo(typing_extensions.Protocol):
    def bar(self, *, baz: str) -> None:
        pass
    
class Foo(IFoo):
    def bar(self, *, baz: str) -> None:
        pass

typeguard.check_type(value=Foo(), expected_type=IFoo, collection_check_strategy=typeguard.CollectionCheckStrategy.ALL_ITEMS)    
@agronholm
Copy link
Owner

The underlying reason was that I was shoehorning these checks into Callable[] checks which don't support the notion of keyword-only arguments. I have a fix incoming which adds proper (in theory) method signature checking.

@agronholm
Copy link
Owner

This is essentially a duplicate of #465, so I'm closing this one in its favor.

@agronholm agronholm closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2024
@mrgrain
Copy link
Author

mrgrain commented Oct 26, 2024

Thank you. Appreciate the response. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants