python / python/mypy

Incremental mode bug for nested ParamSpecs

Aperta
#14,322 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-paramspec topic-self-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo due volte il reproducer fornito per i ParamSpec annidati con la modalità incrementale abilitata, quindi esamina il percorso di controllo dei tipi per i ParamSpec annidati. Il lavoro è completato quando la seconda esecuzione mostra BADerived con b_param_1 e a_param_1 nella sua firma e accetta senza errori la chiamata mostrata, tipizzata correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.