Skip to content

type[T] constructors don't use TypeVar bounds for parameter inference #2369

Description

@migeed-z

Describe the Bug

When calling a constructor via type[T] where T is bounded, pyrefly should use the bound's
__init__ signature to determine valid parameters. Currently it falls through to
object.__init__ and accepts any call.

def test[T](cls: type[T]) -> T:
    cls(1)  # should error: no args for object constructor
    return cls()
class A:
    def __init__(self, x: int) -> None: pass
def test2[T: A](cls: type[T]) -> T:
    a1: A = cls()  # should error: missing required arg x
    a2: A = cls(1)
    return cls()

Note: this unittest is already available in the codebase.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSImMYABAC7x0DaAKgLoAUAxlHIvaWIxWbAJQ0AtAD4aLRAB10NZTR5wOARnE0AxDTgALXAFcomGjABOl3Jf65sAKxhc6HcXVQBreDXS4aVEsAczhFFRpLGDpjSyU1d0VFHlQ4OBoAQQUlFSpaAH18iHQIOkKOOBgoMAAaGnx%2BYrpxaRoAOVx0GH5iVLD0RTz6RgAmVn4MzjV%2BOkFhdhaZOXCVVA0JmgBeVV53ZT1DEzMLa1t%2BAFsINOLgyJgAR2MIKPMg2-wV5VQRje2ErU%2BdxicR26lEihANRAxjo0DgJHIiBAegAqrCoKVSDQwMZ0K4IJ1%2BoNqNjbOdUGV0MZztgrBwGjQmot9HQ7ICosClGB5CA2tTaXYaMB8ABfHkQqFkKJgKCkQh0XDnKAUPQABVI0tl%2BgwOAIqk6kGCsQpBPQhEUegAyjAYDQDHQ6MQ%2BAB6Z1S6iywi2YLOmDoZ2YXBcODOrgGiBGywmzrO0mWQIAN1Q0FQ2Fg%2BvQhuNsM6NFwxBz6HhijIdCM6AkCascFNWxoPIAzIQNCNxegQCKoah8VWAGLQGAUNBYPBEMgdoA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions