Skip to content

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

Merged
JelleZijlstra merged 1 commit into
python:mainfrom
tyralla:fix/methodtype_func
Jan 31, 2024
Merged

Generalise attribute __func__ of class MethodType from FunctionType to Callable#11201
JelleZijlstra merged 1 commit into
python:mainfrom
tyralla:fix/methodtype_func

Conversation

@tyralla

@tyralla tyralla commented Dec 26, 2023

Copy link
Copy Markdown
Contributor

Fixes #11200

…ny]` instead of `_StaticFunctionType` and remove the latter.
@github-actions

Copy link
Copy Markdown
Contributor

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

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

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

tyralla commented Dec 28, 2023

Copy link
Copy Markdown
Contributor Author

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
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