Skip to content

calling a type with overloads on __new__ is not handled in a consistent way #5009

In review

Description

@OutSquareCapital

Describe the Bug

Opened a similar issue in ty repo here

This may be related to #2450 .

Calling a type Foo like Foo(arg) work without issues, but when the type is provided as a Callable argument to another function or method, it doesn't pick the right overload, resulting in erroneous outputs.

Example

Vec[T] implement collections::abc::MutableSequence[T], and Seq[T], spoilers ahead, collections::abc::Sequence[T].

Hence the former is/should be invariant, and the latter covariant.

Note that all the typing lives in stub. the source code comes from rust.

import operator
from typing import assert_type

from pyochain import Option, Seq, Some, Vec


def check_constructor_call_context() -> None:
    """Constructors should be handled the same, regardless of the call context.

    Checking with `Seq` and `Vec` to ensure variance doesn't affect the result.

    Warning:
        Currently only passes with `basedpyright`
    """
    x = assert_type(Seq([1]), Seq[int])

    _vec_standard = assert_type(Vec(x), Vec[int])
    _seq_standard = assert_type(Seq(x), Seq[int])

    _vec_called = assert_type(operator.call(Vec, x), Vec[int])
    _seq_called = assert_type(operator.call(Seq, x), Seq[int])

    _vec_piped = assert_type(x.pipe(Vec), Vec[int])
    _seq_piped = assert_type(x.pipe(Seq), Seq[int])

    _seq_collected = assert_type(x.iter().collect(Seq), Seq[int])
    _vec_collected = assert_type(x.iter().collect(Vec), Vec[int])

    x_opt = assert_type(Some(x), Option[Seq[int]])
    _seq_mapped = assert_type(x_opt.map(Vec).unwrap(), Vec[int])
    _vec_mapped = assert_type(x_opt.map(Seq).unwrap(), Seq[int])

Both of them have thee exact same constructor signature:

    @overload
    def __new__(cls, /) -> Self: ...
    @overload
    def __new__(cls, data: Iterable[T], /) -> Self: ...
    @overload
    def __new__(cls, data: T, /, *more: T) -> Self: ...
    def __new__(cls, data: Iterable[T] | T = (), /, *more: T) -> Self: """documentation"""

pipe uses ParamSpec, whilst collect doesn't. So it doesn't seem to be the cause.

Basedpyright report nothing, which is expected, while pyrefly seems to have the same issues as ty here.

File: https://github.com/OutSquareCapital/pyochain/blob/master/tests/test_typing/check_mixins.py

pipe method

collect method

pyrefly.toml config

Command

at the repo root:

uvx pyrefly check tests/test_typing/check_mixins.py

output:

(pyochain) PS C:\Users\stett\Documents\python\pyochain> uvx pyrefly check tests/test_typing/check_mixins.py

ERROR assert_type(Vec[GenericResidual@T] | Vec[Unknown], Vec[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:20:30
   |
20 |     _vec_called = assert_type(operator.call(Vec, x), Vec[int])
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Seq[GenericResidual@T] | Seq[Unknown], Seq[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:21:30
   |
21 |     _seq_called = assert_type(operator.call(Seq, x), Seq[int])
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Vec[Seq[int]] | Vec[int], Vec[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:23:29
   |
23 |     _vec_piped = assert_type(x.pipe(Vec), Vec[int])
   |                             ^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Seq[Seq[int]] | Seq[int], Seq[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:24:29
   |
24 |     _seq_piped = assert_type(x.pipe(Seq), Seq[int])
   |                             ^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Seq[Iterator[int]] | Seq[int], Seq[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:26:33
   |
26 |     _seq_collected = assert_type(x.iter().collect(Seq), Seq[int])
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Vec[Iterator[int]] | Vec[int], Vec[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:27:33
   |
27 |     _vec_collected = assert_type(x.iter().collect(Vec), Vec[int])
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Never, Vec[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:30:30
   |
30 |     _seq_mapped = assert_type(x_opt.map(Vec).unwrap(), Vec[int])
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR assert_type(Never, Seq[int]) failed [assert-type]
  --> tests\test_typing\check_mixins.py:31:30
   |
31 |     _vec_mapped = assert_type(x_opt.map(Seq).unwrap(), Seq[int])
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 INFO 8 errors
(pyochain) PS C:\Users\stett\Documents\python\pyochain>

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions