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

Generalise attribute __func__ of class MethodType from FunctionType to Callable #11201

Merged
merged 1 commit into from
Jan 31, 2024

Conversation

tyralla
Copy link
Contributor

@tyralla tyralla commented Dec 26, 2023

Fixes #11200

…ny]` instead of `_StaticFunctionType` and remove the latter.
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

The comment of _StaticFunctionType hints at an old (2017) mypy hack, although it seems the implementation also uses a member_descriptor class. Did you try just changing the return type of __get__ to a Callable?

@tyralla
Copy link
Contributor Author

tyralla commented Dec 28, 2023

No, I did not because _StaticFunctionType seems unnecessary when returning Callable instead of FunctionType. However, if you think my change could cause unexpected trouble, we could also follow your suggestion, which leads to the same result in the following example (MethodType2 vs. MethodType4):

from types import FunctionType
from typing import Any, Callable

class MethodType1:
    @property
    def __func__(self) -> FunctionType: ...

class MethodType2:
    @property
    def __func__(self) -> Callable[[...], Any]: ...

class Descriptor3:
    def __get__(self, obj: object, type: type | None) -> FunctionType: ...

class MethodType3:
    @property
    def __func__(self) -> Descriptor3: ...

class Descriptor4:
    def __get__(self, obj: object, type: type | None) -> Callable[[...], Any]: ...

class MethodType4:
    @property
    def __func__(self) -> Descriptor4: ...

reveal_type(MethodType1().__func__)  # note: Revealed type is "types.MethodType"
reveal_type(MethodType2().__func__)  # note: Revealed type is "def (Any) -> Any"
reveal_type(MethodType3().__func__)  # note: Revealed type is "types.FunctionType"
reveal_type(MethodType4().__func__)  # note: Revealed type is "def (Any) -> Any"

@JelleZijlstra JelleZijlstra merged commit 1ce82b8 into python:main Jan 31, 2024
60 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.

MethodType uses FunctionLike types inconsistently
3 participants