ruff
03d8679a - [red-knot] Preliminary `NamedTuple` support (#17738)

Commit
67 days ago
[red-knot] Preliminary `NamedTuple` support (#17738) ## Summary Adds preliminary support for `NamedTuple`s, including: * No false positives when constructing a `NamedTuple` object * Correct signature for the synthesized `__new__` method, i.e. proper checking of constructor calls * A patched MRO (`NamedTuple` => `tuple`), mainly to make type inference of named attributes possible, but also to better reflect the runtime MRO. All of this works: ```py from typing import NamedTuple class Person(NamedTuple): id: int name: str age: int | None = None alice = Person(1, "Alice", 42) alice = Person(id=1, name="Alice", age=42) reveal_type(alice.id) # revealed: int reveal_type(alice.name) # revealed: str reveal_type(alice.age) # revealed: int | None # error: [missing-argument] Person(3) # error: [too-many-positional-arguments] Person(3, "Eve", 99, "extra") # error: [invalid-argument-type] Person(id="3", name="Eve") ``` Not included: * type inference for index-based access. * support for the functional `MyTuple = NamedTuple("MyTuple", […])` syntax ## Test Plan New Markdown tests ## Ecosystem analysis ``` Diagnostic Analysis Report ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━┓ ┃ Diagnostic ID ┃ Severity ┃ Removed ┃ Added ┃ Net Change ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━┩ │ lint:call-non-callable │ error │ 0 │ 3 │ +3 │ │ lint:call-possibly-unbound-method │ warning │ 0 │ 4 │ +4 │ │ lint:invalid-argument-type │ error │ 0 │ 72 │ +72 │ │ lint:invalid-context-manager │ error │ 0 │ 2 │ +2 │ │ lint:invalid-return-type │ error │ 0 │ 2 │ +2 │ │ lint:missing-argument │ error │ 0 │ 46 │ +46 │ │ lint:no-matching-overload │ error │ 19121 │ 0 │ -19121 │ │ lint:not-iterable │ error │ 0 │ 6 │ +6 │ │ lint:possibly-unbound-attribute │ warning │ 13 │ 32 │ +19 │ │ lint:redundant-cast │ warning │ 0 │ 1 │ +1 │ │ lint:unresolved-attribute │ error │ 0 │ 10 │ +10 │ │ lint:unsupported-operator │ error │ 3 │ 9 │ +6 │ │ lint:unused-ignore-comment │ warning │ 15 │ 4 │ -11 │ ├───────────────────────────────────┼──────────┼─────────┼───────┼────────────┤ │ TOTAL │ │ 19152 │ 191 │ -18961 │ └───────────────────────────────────┴──────────┴─────────┴───────┴────────────┘ Analysis complete. Found 13 unique diagnostic IDs. Total diagnostics removed: 19152 Total diagnostics added: 191 Net change: -18961 ``` I uploaded the ecosystem full diff (ignoring the 19k `no-matching-overload` diagnostics) [here](https://shark.fish/diff-namedtuple.html). * There are some new `missing-argument` false positives which come from the fact that named tuples are often created using unpacking as in `MyNamedTuple(*fields)`, which we do not understand yet. * There are some new `unresolved-attribute` false positives, because methods like `_replace` are not available. * Lots of the `invalid-argument-type` diagnostics look like true positives --------- Co-authored-by: Douglas Creager <dcreager@dcreager.net>
Author
Parents
  • crates
    • red_knot_python_semantic
      • resources/mdtest
        • File
          named_tuple.md
      • src/types
        • File
          class.rs
        • File
          class_base.rs
    • ruff_benchmark/benches
      • File
        red_knot.rs