Skip to content

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

Open
@ericbn

Description

@ericbn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions