Skip to content

no-matching-overload with list/sorted and map/filter #1970

Closed as duplicate

Description

@AA-Turner

Summary

Error is new in ty 0.0.2, not present in 0.0.1a35. Seems to occur when map / filter are put into list/sorted. Oddly, the first error goes away when directly returning the function call instead of using the temp variable.

A

Playground link: https://play.ty.dev/10c125c1-9480-475a-a076-861ade84c805

Reproducer:

import re
from collections.abc import Iterable
from typing import Any

def func(obj: Any) -> list[str | Any]:
    if isinstance(obj, (list, tuple, set, frozenset)):
        lst = sorted(map(func, obj), key=str) # No overload of function `sorted` matches arguments (no-matching-overload) [Ln 7, Col 15]
        return lst
    return [obj]

def patfilter(names: Iterable[str], pat: str) -> list[str]:
    match = re.compile(pat).match
    return list(filter(match, names))  # No overload of function `__new__` matches arguments (no-matching-overload) [Ln 13, Col 17]

Original output:

error[no-matching-overload]: No overload of function `sorted` matches arguments
  --> sphinx\util\_serialise.py:49:16
   |
47 |     if isinstance(obj, (list, tuple, set, frozenset)):
48 |         # Convert to a sorted list
49 |         return sorted(map(_stable_str_prep, obj), key=str)
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50 |     if isinstance(obj, (type, types.FunctionType)):
51 |         # The default repr() of functions includes the ID, which is not ideal.
   |
info: First overload defined here
    --> stdlib\builtins.pyi:4312:5
     |
4311 |   @overload
4312 |   def sorted(
     |  _____^
4313 | |     iterable: Iterable[SupportsRichComparisonT], /, *, key: None = None, reverse: bool = False
4314 | | ) -> list[SupportsRichComparisonT]:
     | |__________________________________^
4315 |       """Return a new list containing all items from the iterable in ascending order.
     |
info: Possible overloads for function `sorted`:
info:   (iterable: Iterable[SupportsRichComparisonT@sorted], *, /, key: None = None, reverse: bool = False) -> list[SupportsRichComparisonT@sorted]
info:   (iterable: Iterable[_T@sorted], *, /, key: (_T@sorted, /) -> SupportsDunderLT[Any] | SupportsDunderGT[Any], reverse: bool = False) -> list[_T@sorted]
info: rule `no-matching-overload` is enabled by default

error[no-matching-overload]: No overload of function `__new__` matches arguments
   --> sphinx\util\matching.py:112:17
    |
110 |         _pat_cache[pat] = re.compile(_translate_pattern(pat))
111 |     match = _pat_cache[pat].match
112 |     return list(filter(match, names))
    |                 ^^^^^^^^^^^^^^^^^^^^
    |
info: First overload defined here
    --> stdlib\builtins.pyi:3623:9
     |
3622 |     @overload
3623 |     def __new__(cls, function: None, iterable: Iterable[_T | None], /) -> Self: ...
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3624 |     @overload
3625 |     def __new__(cls, function: Callable[[_S], TypeGuard[_T]], iterable: Iterable[_S], /) -> Self: ...
     |
info: Possible overloads for function `__new__`:
info:   (cls, function: None, iterable: Iterable[_T@filter | None], /) -> Self@__new__
info:   (cls, function: (_S@__new__, /) -> @Todo, iterable: Iterable[_S@__new__], /) -> Self@__new__
info:   (cls, function: (_S@__new__, /) -> TypeIs[_T@filter], iterable: Iterable[_S@__new__], /) -> Self@__new__
info:   (cls, function: (_T@filter, /) -> Any, iterable: Iterable[_T@filter], /) -> Self@__new__
info: rule `no-matching-overload` is enabled by default

Found 2 diagnostics

Version

ty 0.0.2 (4283557 2025-12-16)

Activity

  1. added
    generics
    on Dec 17, 2025
  2. mnannan-t commented on Dec 17, 2025

    @mnannan-t

    I got a similar issue in 0.0.2 so posting in a comment rather than creating a new issue for the moment .
    I don't know if it's 100% the same thing but it looks related to overload and was not present 0.0.1a35 but is in 0.0.2.

    https://play.ty.dev/774b7ff6-b62c-4aa6-a8fe-4669261aa0de

    import re
    
    
    def main():
        string_list = ['abc12345', 'def67890']
        sorted_markers_with_key = sorted(string_list, key=len)
        # Type checker error on this line
        _ = '|'.join(re.escape(marker) for marker in sorted_markers_with_key)
    
        sorted_markers_without_key = sorted(string_list, reverse=True)
        _ = '|'.join(re.escape(marker) for marker in sorted_markers_without_key)
    Argument to function `escape` is incorrect: Argument type `Unknown | Sized` does not satisfy constraints (`str`, `bytes`) of type variable `AnyStr` (invalid-argument-type) [Ln 8, Col 28]
    

    Adding key breaks the typing and typing looks to be inferred from the key rather than the iterable.

  3. added this to the M1 milestone on Dec 23, 2025
  4. changed the title [-]ty 0.0.2: `no-matching-overload` with list/sorted and `map`/`filter`[/-] [+]`no-matching-overload` with list/sorted and `map`/`filter`[/+] on Dec 23, 2025
  5. carljm commented on Dec 23, 2025

    @carljm
    Contributor

    Yes, the common factor in these cases looks like solving a generic call where the same typevar appears twice, once inside a Callable type and also elsewhere in the signature.

  6. self-assigned this
    on Jan 9, 2026
  7. pawamoy commented on Jan 30, 2026

    @pawamoy

    Another possible example:

    import re
    
    _changelog_version_re = re.compile(r"^## \[(\d+\.\d+\.\d+)\].*$")
    
    with open("CHANGELOG.md", "r", encoding="utf8") as file:
        match = next(filter(None, map(_changelog_version_re.match, file)))
    % uvx ty check t.py 
    Installed 1 package in 1ms
    error[invalid-argument-type]: Argument to function `__new__` is incorrect
     --> t.py:6:35
      |
    5 | with open("CHANGELOG.md", "r", encoding="utf8") as file:
    6 |     match = next(filter(None, map(_changelog_version_re.match, file)))
      |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected `(str | Buffer, /) -> Match[str] | None | Match[bytes]`, found `Overload[(string: str, pos: int = 0, endpos: int = ...) -> Match[str] | None, (string: Buffer, pos: int = 0, endpos: int = ...) -> Match[bytes] | None, (string: str, pos: int = 0, endpos: int = ...) -> Match[str] | None]`
      |
    info: Matching overload defined here
        --> stdlib/builtins.pyi:3866:13
         |
    3864 |     else:
    3865 |         @overload
    3866 |         def __new__(cls, func: Callable[[_T1], _S], iterable: Iterable[_T1], /) -> Self: ...
         |             ^^^^^^^      ------------------------- Parameter declared here
    3867 |         @overload
    3868 |         def __new__(cls, func: Callable[[_T1, _T2], _S], iterable: Iterable[_T1], iter2: Iterable[_T2], /) -> Self: ...
         |
    info: Non-matching overloads for function `__new__`:
    info:   [_T1, _T2, Self](cls, func: (_T1, _T2, /) -> _S@map, iterable: Iterable[_T1], iter2: Iterable[_T2], /) -> Self
    info:   [_T1, _T2, _T3, Self](cls, func: (_T1, _T2, _T3, /) -> _S@map, iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /) -> Self
    info:   [_T1, _T2, _T3, _T4, Self](cls, func: (_T1, _T2, _T3, _T4, /) -> _S@map, iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], /) -> Self
    info:   [_T1, _T2, _T3, _T4, _T5, Self](cls, func: (_T1, _T2, _T3, _T4, _T5, /) -> _S@map, iterable: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], iter5: Iterable[_T5], /) -> Self
    info:   [Self](cls, func: (...) -> _S@map, iterable: Iterable[Any], iter2: Iterable[Any], iter3: Iterable[Any], iter4: Iterable[Any], iter5: Iterable[Any], iter6: Iterable[Any], /, *iterables: Iterable[Any]) -> Self
    info: rule `invalid-argument-type` is enabled by default
    
    Found 1 diagnostic
  8. dcreager commented on Feb 12, 2026

    @dcreager
    Member

    I opened #2799 with a deeper explanation of what's going wrong with this issue (and several others).

  9. AA-Turner commented on Feb 13, 2026

    @AA-Turner
    Author

    Thanks @dcreager, great write-up!

    A

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

Metadata

Metadata

Assignees

Labels

generics

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions