Skip to content

Callable[[Any], None]" has no attribute "__defaults__" #11896

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
jpy-git opened this issue Jan 3, 2022 · 4 comments
Closed

Callable[[Any], None]" has no attribute "__defaults__" #11896

jpy-git opened this issue Jan 3, 2022 · 4 comments
Labels
bug mypy got something wrong

Comments

@jpy-git
Copy link

jpy-git commented Jan 3, 2022

Bug Report

mypy produces the false positive Callable[[Any], None]" has no attribute "__defaults__" when code uses the __defaults__ attribute of a function.

To Reproduce

Consider this code:

>>> def foo(a) -> None: pass
>>> print(foo.__defaults__)  # None if no default arguments.
None
>>> def bar(a="abc") -> None: pass
>>> print(bar.__defaults__)  # tuple of default arguments.
("abc",)

Run mypy on a script containing this code and get the false-positive:

test.py:2: error: "Callable[[Any], None]" has no attribute "__defaults__"
test.py:4: error: "Callable[[Any], None]" has no attribute "__defaults__"
Found 2 errors in 1 file (checked 1 source file)

Expected Behavior

No mypy errors since it's a valid attribute of a function.

Actual Behavior

mypy errors shown above.

Your Environment

  • Mypy version used: mypy 0.930
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.9.9
  • Operating system and version: MacOS Big Sur
@jpy-git jpy-git added the bug mypy got something wrong label Jan 3, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 3, 2022

It looks to me like the issue is that the definition for the (fictional) function class in typeshed/stdlib/builtins.pyi has not got many of the attributes that the (non-fictional) FunctionType class in typeshed/stdlib/types.pyi has.

You can get similar mypy errors for the following attributes, which are also defined on types.FunctionType, but not on builtins.function:

  • foo.__closure__
  • foo.__kwdefaults__
  • foo.__get__

@jpy-git
Copy link
Author

jpy-git commented Jan 3, 2022

@AlexWaygood Is there any reason we can't just have builtins.function be a type alias for types.FunctionType since they're the same?
i.e. function = types.FunctionType

If not then I'm happy to just copy the attributes over from FuntionType 😄

@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 3, 2022

There have been previous attempts to delete builtins.function, which have (sadly) gone badly (see python/typeshed#5799), but I'm not aware of any previous attempts to simply have builtins.function be a type alias for types.FunctionType. Try it, see what happens! 😀

@jpy-git
Copy link
Author

jpy-git commented Jan 4, 2022

Closing as we've fixed this in typeshed 👍

@jpy-git jpy-git closed this as completed Jan 4, 2022
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

2 participants