Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy succeeds evaluating an attr converter that is an (Any) -> Any function, but fails to evaluate a converter that is returned by a higher-order function that returns an (Any) -> Any function.
To Reproduce
This works as expected:
from attr import define, field
def truncate(val):
return val[:40]
@define
class Person:
first_name: str = field(converter=truncate)
last_name: str = field(converter=truncate)
reveal_type(truncate)
Output:
scratch.py:14: note: Revealed type is "def (val: Any) -> Any"
Success: no issues found in 1 source file
But this does not:
from collections.abc import Callable
from typing import Any
from attr import define, field
def truncate(length) -> Callable[[Any], Any]:
def truncate_converter(val):
return val[:length]
return truncate_converter
@define
class Person:
first_name: str = field(converter=truncate(40))
last_name: str = field(converter=truncate(80))
reveal_type(truncate(40))
reveal_type(truncate(80))
Expected Behavior
Output:
scratch.py:20: note: Revealed type is "def (Any) -> Any"
scratch.py:21: note: Revealed type is "def (Any) -> Any"
Success: no issues found in 1 source file
Actual Behavior
Output:
scratch.py:16: error: Unsupported converter, only named functions, types and lambdas are currently supported [misc]
scratch.py:17: error: Unsupported converter, only named functions, types and lambdas are currently supported [misc]
scratch.py:20: note: Revealed type is "def (Any) -> Any"
scratch.py:21: note: Revealed type is "def (Any) -> Any"
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.4.1
- Mypy command-line flags: just the file name, no extra flags.
- Mypy configuration options from
mypy.ini(and other config files):[tool.mypy] platform = "linux" python_version = "3.10" - Python version used: 3.10.12
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python reproducer with mypy 1.4.1 and confirm the errors on the two attr converter uses. Trace the attr field converter handling for named functions versus the functions returned by truncate, then add a regression test covering both calls. Done means the higher-order converters are accepted without errors and the revealed callable types remain as shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100