python / python/typeshed

type annotation for `{dict_keys,dict_items}.is_disjoint` argument is too narrow

Offen
#15,888 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

In the PR that added these annotations, @hauntsaninja said:

My instinct is to try it the stricter way and relax it if we ever get a real user complaint about it, but happy to go along with whatever others think is best

I'd like to provide such a complaint. In ty, we would like to infer precise key-type specializations (unions of string literals) of dict_keys and dict_items for closed TypedDicts, where all possible keys are known. We have real user requests for this, because it allows iterating over dict keys/items and passing the keys along to a function that expects a limited set of literals.

But doing this causes false positives with isdisjoint, because the following valid code will now error:

class TD(TypedDict, closed=True):
    x: int

def check(td: TD) -> None:
    # Both operations are safe at runtime but will report invalid-argument-type:
    td.keys().isdisjoint(["other"])
    td.items().isdisjoint([("other", 1)])

This is really a sub-issue of https://github.com/python/typeshed/issues/15271, the same category of issue as #6597.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Überprüfung der in PR 12309 eingeführten Annotationen und der zugehörigen Diskussion in issue #15271. Reproduzieren Sie die beiden geschlossenen TypedDict-Beispiele aus diesem issue und passen Sie dann die Typisierung des is_disjoint-Arguments so an, dass beide gültigen Aufrufe akzeptiert werden, ohne die präzisen Schlüssel- und Elementtypen zu verlieren; überprüfen Sie das resultierende Verhalten der Typprüfung.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
58/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.