Skip to content

Unbound method rejected as FunctionType, Callable cast as FunctionType #9934

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

Closed
tlambert03 opened this issue Jan 21, 2021 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@tlambert03
Copy link

tlambert03 commented Jan 21, 2021

Bug Report

This feels related to #8869, but different enough to warrant a separate issue.
There, the issue was that a bound method of MethodType is incorrectly rejected as a Callable, here, I'm noticing that an unbound class method is rejected as a FunctionType:

To Reproduce

import types

class Cls:
    def meth(self):
        pass

assert isinstance(Cls.meth, types.FunctionType)  # no problem

x: types.FunctionType = Cls.meth
Incompatible types in assignment (expression has type "Callable[[Cls], Any]", variable has type "FunctionType")  [assignment]

Expected Behavior

I expected an unbound class method to work for an argument expecting FunctionType

Actual Behavior

Incompatible types in assignment

Your Environment

  • mypy 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.6
  • Operating system and version: macos 10.15.7

Additional info

(In this case, I didn't want to use x: Callable because I'd like to be able to assert that the standard function object methods will be available)

@tlambert03 tlambert03 added the bug mypy got something wrong label Jan 21, 2021
@tlambert03
Copy link
Author

Actually, I guess I'm realizing that mypy more generally interprets Callable as having many of the attributes of a typical FunctionType ... though this may not always be the case. For example. The following code will fail at runtime with AttributeError: 'Cls' object has no attribute '__name__', but mypy has no problem with it:

from typing import Callable

def w(f: Callable):
    print(f.__name__)

class Cls:
    def __call__(self) -> None:
        pass

w(Cls())

@tlambert03 tlambert03 changed the title Unbound method incorrectly rejected as FunctionType Unbound method rejected as FunctionType, Callable cast as FunctionType Jan 21, 2021
@erictraut
Copy link

This bug appears to have been fixed. I can no longer repro the problem with the latest version of mypy (1.5).

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

No branches or pull requests

3 participants