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

Disallow assignment of protocol or abstract classes to Callable #18347

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

This comment has been minimized.

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Dec 27, 2024

Prefect crash minimised is:

from typing import Protocol

class CP(Protocol):
    def __call__(self, parameters: str) -> str: ...

class NotSet: ...

class Task:
    def with_opt(self, trn: CP | type[NotSet] | None):
        if trn is not NotSet:
            reveal_type(trn)

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Dec 27, 2024

CPython is a true positive, you get a TypeError with:

from libclinic.cli import *
parse_file("x.py", limited_capi=True)

cloud-init is also arguably a true positive, since they're testing that it raises TypeError

The diagnostics could be much better though.

@python python deleted a comment from github-actions bot Dec 27, 2024

This comment has been minimized.

@hauntsaninja hauntsaninja marked this pull request as ready for review December 28, 2024 02:52

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

CPython (Argument Clinic) (https://github.com/python/cpython)
+ Tools/clinic/libclinic/cli.py:46: error: Incompatible types in assignment (expression has type "type[PythonLanguage]", target has type "Callable[[str], Language]")  [assignment]

pytest (https://github.com/pytest-dev/pytest)
+ testing/test_monkeypatch.py:418: error: Unused "type: ignore" comment  [unused-ignore]

cloud-init (https://github.com/canonical/cloud-init)
+ tests/unittests/reporting/test_reporting.py:181: error: Argument 3 to "assertRaisesRegex" of "TestCase" has incompatible type "type[ReportingHandler]"; expected "Callable[..., object]"  [arg-type]

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.

1 participant