python / python/mypy

Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function

Open
#15,736 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-attrs
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.