python / python/mypy

Unknown `DefaultNamedArg` raises type incompatibility errors

Open
#9,385 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-calls topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

🐛 Bug Report

DefaultNamedArg may cause signature compatibility errors. Observed when a function has a default keyword argument in signature and is passed to be used as a function without that particular keyword argument.

To Reproduce

from typing import List, AnyStr
from glob import glob
import itertools as it
def expand_patterns(
        pats: List[AnyStr],
        sort=True
):
    paths = it.chain(map(glob, pats))
    # Argument 1 to "map" has incompatible type "Callable[[AnyStr, DefaultNamedArg(bool, 'recursive')], List[AnyStr]]"; expected "Callable[[AnyStr], List[AnyStr]]"
    if sort:
        paths = sorted(paths)
    return paths

Expected Behavior

Not sure if this ought to happen, but I'd expect a function like foo(x: int, y='y') to be peacefully passable to a function expecting Callable[[int], Any].

Actual Behavior

# Argument 1 to "map" has incompatible type "Callable[[AnyStr, DefaultNamedArg(bool, 'recursive')], List[AnyStr]]"; expected "Callable[[AnyStr], List[AnyStr]]"

Your Environment

  • Mypy version used: 0.782
  • Mypy command-line flags: Default of VSCode
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.7.7
  • Operating system and version: Windows 10, 2004

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Python example involving glob, map, Callable compatibility, and DefaultNamedArg. Trace how mypy compares the callable signatures, then verify that a default keyword argument is handled according to the intended compatibility rule and that the reported incompatibility is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.