Skip to content

Default argument of converter doesn't work in field specifiers of dataclass_transform #11291

@PragmaTwice

Description

@PragmaTwice

Describe the bug

In field specifiers of dataclass_transform, we can provide a converter parameter to control the parameter type in __init__. In the following example, we provide a converter=foo where def foo(x: int) -> str, so in __init__ x should be typed int instead of str.

from typing import dataclass_transform

def foo(x: int) -> str: ...

def field(*, converter):
    pass

@dataclass_transform(field_specifiers=(field,))
class A:
    @classmethod
    def __init_subclass__(cls):
        pass

class B(A):
    x: str = field(converter=foo)

# __init__ signature: B(x: int)

But if we move the converter=foo to a default argument of converter in def field(..), the effect of converter will disappear. This is not consistent with other parameters of field specifiers, as they can all be a default argument instead of being an explicit argument.

from typing import dataclass_transform

def foo(x: int) -> str: ...

def field(*, converter=foo):
    pass

@dataclass_transform(field_specifiers=(field,))
class A:
    @classmethod
    def __init_subclass__(cls):
        pass

class B(A):
    x: str = field()

# wrong __init__ signature: B(x: str). should be B(x: int) instead

VS Code extension or command-line

ms-python.vscode-pylance 2026.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions