Skip to content

Disallow assignment of protocol or abstract classes to Callable#18347

Open
hauntsaninja wants to merge 2 commits intopython:masterfrom
hauntsaninja:abstract-callable
Open

Disallow assignment of protocol or abstract classes to Callable#18347
hauntsaninja wants to merge 2 commits intopython:masterfrom
hauntsaninja:abstract-callable

Conversation

@hauntsaninja
Copy link
Copy Markdown
Collaborator

@github-actions

This comment has been minimized.

@hauntsaninja
Copy link
Copy Markdown
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
Copy Markdown
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
@github-actions

This comment has been minimized.

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

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

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]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/interchange/dataframe_protocol.py:368: error: Value of type variable "F" of function cannot be "type[DataFrame]"  [type-var]
+ pandas/io/sas/sasreader.py:36: error: Value of type variable "F" of function cannot be "type[SASReader]"  [type-var]
+ pandas/core/apply.py:83: error: Value of type variable "F" of function cannot be "type[BaseExecutionEngine]"  [type-var]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant