Skip to content

Incorrect type inference when overriding zip and other builtins #2385

@jakevdp

Description

@jakevdp

Describe the Bug

Here is an example of code that leads pyrefly to a false positive error:

def zip(*args):
  return []

def f(a, b):
  return reversed(zip(a, b))
ERROR No matching overload found for function `reversed.__new__` called with arguments: (type[reversed[_T]], zip[tuple[@_, @_]] | Unknown) [no-matching-overload]
 --> test.py:5:18
  |
5 |   return reversed(zip(a, b))
  |                  ^^^^^^^^^^^
  |
  Possible overloads:
  (cls: type[reversed[_T]], sequence: Reversible[_T], /) -> Iterator[_T] [closest match]
  (cls: type[reversed[_T]], sequence: SupportsLenAndGetItem[_T], /) -> Iterator[_T]
 INFO 1 error

This seems to have to do with shadowing the builtin zip name; if you replace zip with a differently-named function there is no error:

def _zip(*args):
  return []

def f(a, b):
  return reversed(_zip(a, b))
 INFO 0 errors

Sandbox Link

sandbox link

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions