python / python/mypy

Incremental mode bug for nested ParamSpecs

Open
#14,322 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-paramspec topic-self-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

This bug only occurs on the second run when using incremental mode. When using nested ParamSpecs, the outer ParamSpec (M below) is erroneously equal to the inner ParamSpec (P below).

Notably, pyright handles this correctly.

To Reproduce

from __future__ import annotations

from typing import Callable, Concatenate, Generic, ParamSpec, TypeVar


class ABase:
    ...


class ADerived(ABase):
    def __init__(self, a_param_1: float) -> None:
        super().__init__()
        self.a_1 = a_param_1


M = ParamSpec("M")
P = ParamSpec("P")
R = TypeVar("R", bound=ABase)


class B(Generic[P, R]):
    def __init__(
        self,
        a_class: Callable[P, R],
        b_param_1: str,
        *args: P.args,
        **kwargs: P.kwargs,
    ) -> None:
        self.a = a_class(*args, **kwargs)
        self.b_1 = b_param_1

    @classmethod
    def build(
        cls: Callable[Concatenate[Callable[P, R], M], B[P, R]], 
        a_class: Callable[P, R]
    ) -> Callable[M, B[P, R]]:

        def new_constructor(
            *args: M.args,
            **kwargs: M.kwargs,
        ) -> B[P, R]:
            return cls(a_class, *args, **kwargs)

        return new_constructor


BADerived = B.build(ADerived)

reveal_type(BADerived)  # note): Revealed type is "def (a_param_1: builtins.float) -> B[[a_param_1: builtins.float], ADerived]"

b_a_derived_1 = B(ADerived, "my_str", 1.0)  # OK
b_a_derived_2 = BADerived(  # [call-arg] error: Too many arguments
    "my_str", 1.0  # [arg-type] error: Argument 1 has incompatible type "str"; expected "float"
)

Expected Behavior

I expect the signature of BADerived to include b_param_1, like this:

mypy_nested_paramspec.py:49: note: Revealed type is "def (b_param_1: builtins.str, a_param_1: builtins.float) -> mypy_nested_paramspec.B[[a_param_1: builtins.float], ADerived]"

I also expect that calls to BADerived with the correct parameter count and types do not raise any errors.

Actual Behavior

mypy_nested_paramspec.py:49: note: Revealed type is "def (a_param_1: builtins.float) -> mypy_nested_paramspec.B[[a_param_1: builtins.float], mypy_nested_paramspec.ADerived]"
mypy_nested_paramspec.py:52: error: Too many arguments  [call-arg]
mypy_nested_paramspec.py:53: error: Argument 1 has incompatible type "str"; expected "float"  [arg-type]

Your Environment

  • Mypy version used: mypy 0.991 (compiled: yes)
  • Python version used: 3.10.6

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 running the supplied nested ParamSpec reproducer twice with incremental mode enabled, then inspect the type-checking path for nested ParamSpecs. Done means the second run reveals BADerived with both b_param_1 and a_param_1 in its signature and accepts the shown correctly typed call without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.