mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-21 15:52:34 +00:00
![]() ## Summary This PR adds a new `Type::TypedDict` variant. Before this PR, we treated `TypedDict`-based types as dynamic Todo-types, and I originally planned to make this change a no-op. And we do in fact still treat that new variant similar to a dynamic type when it comes to type properties such as assignability and subtyping. But then I somehow tricked myself into implementing some of the things correctly, so here we are. The two main behavioral changes are: (1) we now also detect generic `TypedDict`s, which removes a few false positives in the ecosystem, and (2) we now support *attribute* access (not key-based indexing!) on these types, i.e. we infer proper types for something like `MyTypedDict.__required_keys__`. Nothing exciting yet, but gets the infrastructure into place. Note that with this PR, the type of (the type) `MyTypedDict` itself is still represented as a `Type::ClassLiteral` or `Type::GenericAlias` (in case `MyTypedDict` is generic). Only inhabitants of `MyTypedDict` (instances of `dict` at runtime) are represented by `Type::TypedDict`. We may want to revisit this decision in the future, if this turns out to be too error-prone. Right now, we need to use `.is_typed_dict(db)` in all the right places to distinguish between actual (generic) classes and `TypedDict`s. But so far, it seemed unnecessary to add additional `Type` variants for these as well. part of https://github.com/astral-sh/ty/issues/154 ## Ecosystem impact The new diagnostics on `cloud-init` look like true positives to me. ## Test Plan Updated and new Markdown tests |
||
---|---|---|
.. | ||
corpus | ||
mdtest | ||
primer | ||
README.md |
Markdown files within the mdtest/
subdirectory are tests of type inference and type checking;
executed by the tests/mdtest.rs
integration test.
See crates/ty_test/README.md
for documentation of this test format.